Skip to content

imran3180/flashtext

Repository files navigation

Flashtext Ruby Gem

Ruby (zero dependencies) gem for amazing Python package flashtext

This module can be used to replace keywords in sentences or extract keywords from sentences. It is based on the FlashText algorithm

More about Flashtext algorithm.

The original paper published on FlashText algorithm

The article published on Medium freeCodeCamp

Installation

$ gem install flashtext

API doc

Documentation can be found at FlashText Read the Docs

Usage

If you are using irb console

require 'flashtext'

Extract keywords

keyword_processor = Flashtext::KeywordProcessor.new
# keyword_processor.add_keyword(<unclean name>, <standardised name>)
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

keyword_processor = Flashtext::KeywordProcessor.new(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']

Test

rspec spec

Contribute

Implementation in other languages

License

This code is under MIT license.

About

A Ruby Port of the Python package FlashText @ https://github.com/vi3k6i5/flashtext

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published