Advertisement
Guest User

Top-3 most used words in the text.

a guest
Feb 28th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2. function signCleaner(str) {
  3.     /*'arrlett' is an array all of characters from input string.
  4.         This function divides input string and fulfill array 'newtext' by letters and spaces into characters.
  5.         After this it join all characters in the new string and divides by spaces into words.*/
  6.     let arrlett = str.toLowerCase().trim().split("");
  7.     let signs = ['.', ',', '!', '?', ';', ':', '(', ')', '–', '"', '\''];
  8.     let newtext = [];
  9.     for (let i = 0; i < arrlett.length; i++) {
  10.         if (signs.indexOf(arrlett[i]) == -1) {
  11.             newtext.push(arrlett[i]);
  12.         }
  13.     }
  14.     return newtext.join('').split(" ");
  15. }
  16. //Менять код ниже этого комментария.
  17. function unifier(wordsArr) {
  18.     /*The function 'unifier' calculates the number of occurences of each word in an array and stores the value in an result object property (resobj) with the same name.*/
  19.     let resObj = new Object();
  20.     for (let i = 0; i < wordsArr.length; i++) {
  21.         if (!resObj.hasOwnProperty(wordsArr[i])) {
  22.             // Object.defineProperty(resObj, wordsArr[i], {
  23.             //  configurable: true,
  24.             //  writable: true,
  25.             //  enumerable: true,
  26.             //  value: 1
  27.             // });
  28.             resObj.wordsArr[i] = 1;
  29.         } else {
  30.             resObj.wordsArr[i] += 1;
  31.         }
  32.     }
  33.     //console.log(resObj);
  34.     return resObj;
  35. }
  36. //Менять код выше этого комментария.
  37. function topThreeWords(text) {
  38.     //let unqwords = unifier(signCleaner(text));
  39.     //console.log(unqwords);
  40.     const size = Object.keys(unqwords).length;
  41.     let top = [];
  42.     let i = 0;
  43.     switch (size) {
  44.         case 0:
  45.             return [];
  46.         case 1:
  47.             return [Object.keys(unqwords)[0]];
  48.         case 2:
  49.             if (unqwords[0] > unqwords[1]) {
  50.                 return [Object.keys(unqwords)[0], Object.keys(unqwords)[1]];
  51.             } else {
  52.                 return [Object.keys(unqwords)[1], Object.keys(unqwords)[0]];
  53.             }
  54.         default:
  55.             while (i < 3) {
  56.                 function comparer(a, b) {
  57.                     return unqwords[a] > unqwords[b] ? a : b;
  58.                 }
  59.                 let key = Object.keys(unqwords).reduce(comparer, '');
  60.                 if (key != '') {
  61.                     top.push(key);
  62.                 }
  63.                 unqwords[key] = 0;
  64.                 i++;
  65.             }
  66.     }
  67.     return top;
  68. }
  69. const testValues = ["a a a  b  c c  d d d d  e e e e e", "a a c b b", "e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e", "  //wont won't won't ", "  , e   .. ", "  ...  ", "  '  ", `In a village of La Mancha, the name of which I have no desire to call to mind, there lived not long since one of those gentlemen that keep a lance in the lance-rack, an old buckler, a lean hack, and a greyhound for coursing. An olla of rather more beef than mutton, a salad on most nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra on Sundays, made away with three-quarters of his income.`];
  70. const results = [['e', 'd', 'a'], ['a', 'b', 'c'], ['e', 'ddd', 'aa'], ["won't", "wont"], ["e"], [], [], ['a', 'of', 'on']];
  71. function testAssertEquals(inputData, expResult) {
  72.     return topThreeWords(inputData) === expResult;
  73. }
  74. // for (let i = 0; i < testValues.length; i++) {
  75. //  console.log(testAssertEquals(testValues[i], results[i]));
  76. // }
  77. console.log(signCleaner(testValues[7]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement