Code Monkey home page Code Monkey logo

flashtext.js's Introduction

Flashtext.js

Build Status Coverage Status

JavaScript (ES6+ source and zero dependencies) port of the amazing Python package flashtext

This module can be used to replace keywords in sentences or extract keywords from sentences.

Note - This is a quick experimental port. Please post issues for anything that doesn't work or can be improved

Installation

	$ npm install flashtext.js --save

or

	$ yarn add flashtext.js

Usage

Extract Keywords

const processor = new KeywordProcessor();

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');

const keywordsFound = processor.extractKeywords(
	'I love Big Apple and Bay Area.'
);
console.log(keywordsFound);
// output - ['New York', 'Bay Area']

Replace Keywords

const processor = new KeywordProcessor();

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('New Delhi', 'NCR Region');

const newSentence = processor.replaceKeywords(
	'I love Big Apple and new delhi.'
);
console.log(newSentence);
// output - 'I love New York and NCR Region.'

Case Sensitive Example

const processor = new KeywordProcessor(true);

processor.addKeyword('Big Apple', 'New York');
processor.addKeyword('Bay Area');

const keywordsFound = processor.extractKeywords(
	'I love big Apple and Bay Area.'
);
console.log(keywordsFound);
// output - ['Bay Area']

Multiple Keywords Entry using Object

const processor = new KeywordProcessor();

processor.addKeywordsFromObject({
	java: ['java_2e', 'java programming'],
	python: ['python2.7', 'python programming'],
});

const keywordsFound = processor.extractKeywords(
	'I love java_2e and python programming.'
);
console.log(keywordsFound);
// output - ['java', 'python']

const newSentence = processor.replaceKeywords(
	'I prefer python over java programming.'
);
console.log(newSentence);
// output - 'I prefer python over java.'

Multiple Keywords Entry using Arrays

const processor = new KeywordProcessor();

processor.addKeywordsFromArray(['java', 'product manager']);

const keywordsFound = processor.extractKeywords(
	'I am a product manager for a java platform'
);

console.log(keywordsFound);
// output - ['product manager', 'java']

Remove Keywords (One/Multiple)

const processor = new KeywordProcessor();

processor.addKeywordsFromArray(['react', 'angular', 'vue', 'javascript']);
processor.removeKeywordsFromArray(['angular', 'vue']);
processor.removeKeyword('javascript');

const keywordsFound = processor.extractKeywords(
	'I know React, Angular and Vue. I love JavaScript.'
);
console.log(keywordsFound);
// output - ['react'];

Add characters as a part of word

const processor = new KeywordProcessor();

processor.addKeyword('Big Apple');

const before = processor.extractKeywords('I love Big Apple/Bay Area.');
console.log(before);
// output - ['Big Apple']

processor.addNonWordBoundaries('/');
const after = processor.extractKeywords('I love Big Apple/Bay Area.');
console.log(after);
// output - []

References

The original paper published on FlashText algorithm

The article published on Medium freeCodeCamp

License

This code is under MIT license.

flashtext.js's People

Contributors

drenther avatar dheerajavaamo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.