vi3k6i5 / flashtext

Extract Keywords from sentence or Replace keywords in sentences.
MIT License
5.57k stars 599 forks source link

Priority Rank of Keyword Replacements #76

Open SamShowalter opened 5 years ago

SamShowalter commented 5 years ago

Is there a way to set the priority of string replacements? Could I do something that could preference longer replacements (e.g. "I Love New York" -> "ILNY" v. "New York" -> "NYC") over shorter ones? How can I incorporate this flexibility?

SamShowalter commented 5 years ago

EDIT: The system appears to do this already. I just want to verify this is the case.

`from flashtext import KeywordProcessor keyword_processor = KeywordProcessor() keyword_processor.add_keyword('I Love New York', 'ILNY') keyword_processor.add_keyword("New York", "NYC") keyword_processor.add_keyword("I Love", "ILY")

keyword_processor.replace_keywords("This is true, I Love I Love New York New York and everything else")`

Output: 'This is true, ILY ILNY NYC and everything else'

vi3k6i5 commented 5 years ago

Yes so longest match is preferred.

On Mon, Mar 18, 2019, 10:02 PM Sam Showalter notifications@github.com wrote:

EDIT: The system appears to do this already. I just want to verify this is the case.

`from flashtext import KeywordProcessor keyword_processor = KeywordProcessor() keyword_processor.add_keyword('I Love New York', 'ILNY') keyword_processor.add_keyword("New York", "NYC") keyword_processor.add_keyword("I Love", "ILY")

keyword_processor.replace_keywords("This is true, I Love I Love New York New York and everything else")`

Output: 'This is true, ILY ILNY NYC and everything else'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/vi3k6i5/flashtext/issues/76#issuecomment-473992484, or mute the thread https://github.com/notifications/unsubscribe-auth/AC-Nwsb47H4utmDgmY2ZOpq9I2e5JMBIks5vX79_gaJpZM4b6Ox- .