Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     const stopWords = ["ipsum", "sit", "non", "eu", "in", "ut", "id", "vel"];
  2.  
  3.     const countTopWords = (text) => {
  4.         text = text.replace(/(~|\n|`|!|@|#|$|%|^|&|\*|\(|\)|{|}|\[|\]|;|:|\"|'|<|,|\.|>|\?|\/|\\|\||-|_|\+|=)/g," ")
  5.         text = text.toLowerCase();
  6.         let filteredWordsArray = text.split(' ').filter((word) => !stopWords.includes(word));
  7.         filteredWordsArray = filteredWordsArray.filter((word) => word.length > 1)
  8.        
  9.         const uniqueWords = [...new Set(filteredWordsArray)];
  10.  
  11.         const countedWords = [];
  12.         uniqueWords.map((mapWord) => {
  13.             const wordRepetitions = [];
  14.             const repeatingWordArray = filteredWordsArray.filter((filterWord) => mapWord === filterWord);
  15.             wordRepetitions.push(mapWord);
  16.             wordRepetitions.push(repeatingWordArray.length);
  17.             countedWords.push(wordRepetitions);
  18.         })
  19.         console.log(countedWords);
  20.  
  21.         let topWords = [];
  22.  
  23.         let i;
  24.  
  25.         for (i = 0; i < 5; i++) {
  26.             let topWord = ['', 0, 0];
  27.  
  28.             countedWords.map((pair, index) => {
  29.                 if (pair[1] > topWord[1]) {
  30.                     topWord[0] = pair[0];
  31.                     topWord[1] = pair[1];
  32.                     topWord[2] = index;
  33.                 }
  34.             });
  35.    
  36.             countedWords.splice(topWord[2], 1);
  37.             topWord.splice(2, 1);
  38.             topWords.push(topWord);
  39.  
  40.            
  41.             console.log(topWords);
  42.  
  43.             // 0: (2) ["amet", 10]
  44.             // 1: (2) ["tincidunt", 9]
  45.             // 2: (2) ["justo", 7]
  46.             // 3: (2) ["lorem", 6]
  47.             // 4: (2) ["nibh", 6]                  
  48.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement