Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- function signCleaner(str) {
- /*'arrlett' is an array all of characters from input string.
- This function divides input string and fulfill array 'newtext' by letters and spaces into characters.
- After this it join all characters in the new string and divides by spaces into words.*/
- let arrlett = str.toLowerCase().trim().split("");
- let signs = ['.', ',', '!', '?', ';', ':', '(', ')', '–', '"', '\''];
- let newtext = [];
- for (let i = 0; i < arrlett.length; i++) {
- if (signs.indexOf(arrlett[i]) == -1) {
- newtext.push(arrlett[i]);
- }
- }
- return newtext.join('').split(" ");
- }
- //Менять код ниже этого комментария.
- function unifier(wordsArr) {
- /*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.*/
- let resObj = new Object();
- for (let i = 0; i < wordsArr.length; i++) {
- if (!resObj.hasOwnProperty(wordsArr[i])) {
- // Object.defineProperty(resObj, wordsArr[i], {
- // configurable: true,
- // writable: true,
- // enumerable: true,
- // value: 1
- // });
- resObj.wordsArr[i] = 1;
- } else {
- resObj.wordsArr[i] += 1;
- }
- }
- //console.log(resObj);
- return resObj;
- }
- //Менять код выше этого комментария.
- function topThreeWords(text) {
- //let unqwords = unifier(signCleaner(text));
- //console.log(unqwords);
- const size = Object.keys(unqwords).length;
- let top = [];
- let i = 0;
- switch (size) {
- case 0:
- return [];
- case 1:
- return [Object.keys(unqwords)[0]];
- case 2:
- if (unqwords[0] > unqwords[1]) {
- return [Object.keys(unqwords)[0], Object.keys(unqwords)[1]];
- } else {
- return [Object.keys(unqwords)[1], Object.keys(unqwords)[0]];
- }
- default:
- while (i < 3) {
- function comparer(a, b) {
- return unqwords[a] > unqwords[b] ? a : b;
- }
- let key = Object.keys(unqwords).reduce(comparer, '');
- if (key != '') {
- top.push(key);
- }
- unqwords[key] = 0;
- i++;
- }
- }
- return top;
- }
- 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.`];
- const results = [['e', 'd', 'a'], ['a', 'b', 'c'], ['e', 'ddd', 'aa'], ["won't", "wont"], ["e"], [], [], ['a', 'of', 'on']];
- function testAssertEquals(inputData, expResult) {
- return topThreeWords(inputData) === expResult;
- }
- // for (let i = 0; i < testValues.length; i++) {
- // console.log(testAssertEquals(testValues[i], results[i]));
- // }
- console.log(signCleaner(testValues[7]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement