Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const stopWords = ["ipsum", "sit", "non", "eu", "in", "ut", "id", "vel"];
- const countTopWords = (text) => {
- text = text.replace(/(~|\n|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|>|\?|\/|\\|\||-|_|\+|=)/g," ")
- text = text.toLowerCase();
- let filteredWordsArray = text.split(' ').filter((word) => !stopWords.includes(word));
- filteredWordsArray = filteredWordsArray.filter((word) => word.length > 1)
- const uniqueWords = [...new Set(filteredWordsArray)];
- const countedWords = [];
- uniqueWords.map((mapWord) => {
- const wordRepetitions = [];
- const repeatingWordArray = filteredWordsArray.filter((filterWord) => mapWord === filterWord);
- wordRepetitions.push(mapWord);
- wordRepetitions.push(repeatingWordArray.length);
- countedWords.push(wordRepetitions);
- })
- console.log(countedWords);
- let topWords = [];
- let i;
- for (i = 0; i < 5; i++) {
- let topWord = ['', 0, 0];
- countedWords.map((pair, index) => {
- if (pair[1] > topWord[1]) {
- topWord[0] = pair[0];
- topWord[1] = pair[1];
- topWord[2] = index;
- }
- });
- countedWords.splice(topWord[2], 1);
- topWord.splice(2, 1);
- topWords.push(topWord);
- console.log(topWords);
- // 0: (2) ["amet", 10]
- // 1: (2) ["tincidunt", 9]
- // 2: (2) ["justo", 7]
- // 3: (2) ["lorem", 6]
- // 4: (2) ["nibh", 6]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement