This module can be used to replace keywords in sentences or extract keywords from sentences.
Installation
$ pip install flashtext
Usage
- Simple example
>>> from flashtext.keyword import KeywordProcessor >>> keyword_processor = KeywordProcessor() >>> keyword_processor.add_keyword('Big Apple', 'New York') >>> keyword_processor.add_keyword('Bay Area') >>> keywords_found = keyword_processor.extract_keywords('I love Big Apple and Bay Area.') >>> keywords_found >>> ['New York', 'Bay Area']- Replace keywords
>>> keyword_processor.add_keyword('New Delhi', 'NCR region') >>> new_sentence = keyword_processor.replace_keywords('I love Big Apple and new delhi.') >>> new_sentence >>> 'I love New York and NCR region.'- Case Sensitive example
>>> from flashtext.keyword import KeywordProcessor >>> keyword_processor = KeywordProcessor(case_sensitive=True) >>> keyword_processor.add_keyword('Big Apple', 'New York') >>> keyword_processor.add_keyword('Bay Area') >>> keywords_found = keyword_processor.extract_keywords('I love big Apple and Bay Area.') >>> keywords_found >>> ['Bay Area']- No clean name for Keywords
>>> from flashtext.keyword import KeywordProcessor >>> keyword_processor = KeywordProcessor() >>> keyword_processor.add_keyword('Big Apple') >>> keyword_processor.add_keyword('Bay Area') >>> keywords_found = keyword_processor.extract_keywords('I love big Apple and Bay Area.') >>> keywords_found >>> ['Big Apple', 'Bay Area']
API doc
Documentation can be found at FlashText Read the Docs.
Why not Regex?
It’s a custom algorithm based on Aho-Corasick algorithm and Trie Dictionary.
To do the same with regex it will take a lot of time:
Docs count |
# Keywords |
: |
Regex |
flashtext |
|---|---|---|---|---|
1.5 million |
2K |
: |
16 hours |
NA |
2.5 million |
10K |
: |
15 days |
15 mins |
The idea for this library came from the following StackOverflow question.
Contribute
Issue Tracker: https://github.com/vi3k6i5/flashtext/issues
Source Code: https://github.com/vi3k6i5/flashtext/
License
The project is licensed under the MIT license.
Metadata
Release files for flashtext 1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flashtext-1.4.tar.gz | 6.3 kB | Details |
Release files / flashtext-1.4.tar.gz
| Download URL | flashtext-1.4.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
261ffc5b9181e714061a06cda8d70029b835b3993fe3436d537468b0b0f247df
|
|
BLAKE2b-256 checksum How to use checksums |
57d31d6cebf7a237870166e1cd91af53acfb26f7c4dc45e9c7a9386cca1b51e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |