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 flashtext2jsyarn add flashtext2js
then
// FlashText is the Class, flashtext is the instance with default option;// if you want to ignore case, use let ft = new FlashText({ignore: true}); // extract keywords from a target stringconst target = 'I love Big Apple and Bay Area.'ftftft // output: ['New York', 'Bay Area'] // replace the string via keywordsconst target = 'I love Big Apple and New Delhi.'ft;ft // output: 'I love Big Apple and NCR region.'
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;ft; // output 'NCR region' ft // output: ['NCR region']
- addKeyWordsFromArray(keywords: string[], mapped?: string)
as the name
ft
- ft.addKeyWordsFromObject(object)
the object must like this structure: { 'mapped': [] }
ft; // means when matched 'jack roses' or 'jack ros', will return 'jack rose'
- ft.removeKeyWord(keyword: string)
will remove the keyword in the trie structure
-ft.removeKeyWordsFromArray(keywords: string[])
just forEach(() => removeKeyWord())
- ft.toMap()
will return a keyword-mapped mapper.
ft;ft; ft; // output {abc: 'ABC', abd: 'ABC'}
- ft.extractKeywords(target: string)
will return all the matched keywords in an arry
// extract keywords from a target stringconst target = 'I love Big Apple and Bay Area.'ftftft // output: ['New York', 'Bay Area']
- ft.replaceKeyWords(target: string)
will replace the target string with keywords and return it.
// replace the string via keywordsconst target = 'I love Big Apple and New Delhi.'ft;ft // output: 'I love Big Apple and NCR region.'
more api, just go here