Skip to content

Instantly share code, notes, and snippets.

@rsaryev
Last active April 20, 2023 10:50
Show Gist options
  • Save rsaryev/e2edd58b9bb7c202822dd192b0a56c0c to your computer and use it in GitHub Desktop.
Save rsaryev/e2edd58b9bb7c202822dd192b0a56c0c to your computer and use it in GitHub Desktop.
use Intl.Segmenter
function topWords(text, count = 3, lang = 'en') {
const wordMap = new Map();
const segmenter = new Intl.Segmenter(lang, { granularity: 'word' });
for (let { segment, index, isWordLike } of segmenter.segment(text)) {
if (!isWordLike) continue;
const word = segment;
wordMap.set(word, (wordMap.get(word) || 0) + 1);
}
return Array.from(wordMap)
.sort(([, countA], [, countB]) => countB - countA)
.slice(0, count)
.map(([word]) => word);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment