Flashtext is a Python library designed for efficient keyword extraction and replacement in sentences. It employs a custom algorithm based on Aho-Corasick and Trie data structures, providing significant performance gains over regular expressions, especially for large dictionaries of keywords. The current stable version is 2.7, released in 2018, and it is largely in a maintenance state, though still widely used.
pip install flashtextVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to initialize the `KeywordProcessor`, add keywords with optional clean names, and then use it to extract or replace keywords in a given text. It also shows how to get span information for extracted keywords.
For non-Latin languages or custom boundary needs, initialize `KeywordProcessor` with a modified `non_word_boundaries` set. Example: `kp = KeywordProcessor(non_word_boundaries=set(['@', '#']))`.
Benchmark performance for your specific use case. If keyword count is low or complex pattern matching is needed, consider standard regex. If keyword count is high, Flashtext is highly optimized for speed.
Ensure that `add_keyword()` is only provided with string `clean_name` values if `replace_keywords()` functionality is intended. Tuple clean names are primarily for enhanced extraction information.
Consider migrating to `flashtext2` for improved performance and broader language support, especially if hitting performance bottlenecks or unicode issues with the original `flashtext`. Be aware of potential API differences or slight behavior changes, though the core API is similar.
Install the library using pip: `pip install flashtext`
Ensure that `case_sensitive` is set appropriately, especially for text with special characters. If the issue persists, consider pre-processing non-ASCII characters or ensuring the Flashtext version is compatible with your Python environment's string handling. One reported fix involved adjusting string length assumptions in the library's internal code.
Ensure the `KeywordProcessor` object is correctly initialized and the extraction function is applied properly across the DataFrame. For PySpark, this often involves creating the `KeywordProcessor` instance and using a UDF to apply `extract_keywords` to a column.
No dependency data recorded yet.