flashtext.js
Extract Keywords from sentence or Replace keywords in sentences.
flashtext's implementation via typescript.
read regex-was-taking-5-days-flashtext-does-it-in-15-minutes to learn more.
if you should match over 500 keywords, flashtext will better tha regexp, otherwise you may not need it
Usage
npm install flashtext2js
yarn add flashtext2js
then
import FlashText, { flashtext } from 'flashtextjs';
const target = 'I love Big Apple and Bay Area.'
ft.addKeyWord('Big Apple', 'New York')
ft.addKeyWord('Bay Area')
ft.extractKeywords(target)
const target = 'I love Big Apple and New Delhi.'
ft.addKeyWord('New Delhi', 'NCR region');
ft.replaceKeyWords(target)
API
- ft.addKeyWord(keyword: string, mapped?: string)
add the keyword in trie structure. when the keyword matched in the target string, will return secode param mapped.
mapped default is equal to keyword string.
ft.addKeyWord('New Delhi', 'NCR region');
ft.get('New Delhi');
ft.extractKeywords('I love Big Apple and Bay Area.')
- addKeyWordsFromArray(keywords: string[], mapped?: string)
as the name
ft.addKeyWordsFromArray(['jack rose'])
- ft.addKeyWordsFromObject(object)
the object must like this structure: { 'mapped': [] }
ft.addKeyWordsFromObject({'jack rose': ['jack roses', 'jack ros']});
- ft.removeKeyWord(keyword: string)
will remove the keyword in the trie structure
-ft.removeKeyWordsFromArray(keywords: string[])
just forEach(() => removeKeyWord())
will return a keyword-mapped mapper.
ft.addKeyword('abc', 'ABC');
ft.addKeyword('abd', 'ABC');
ft.toMap();
- ft.extractKeywords(target: string)
will return all the matched keywords in an arry
const target = 'I love Big Apple and Bay Area.'
ft.addKeyWord('Big Apple', 'New York')
ft.addKeyWord('Bay Area')
ft.extractKeywords(target)
- ft.replaceKeyWords(target: string)
will replace the target string with keywords and return it.
const target = 'I love Big Apple and New Delhi.'
ft.addKeyWord('New Delhi', 'NCR region');
ft.replaceKeyWords(target)
more api, just go here
License
MIT