Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript to find most common words in string?

I have a large block of text, and I would like to find out the most common words being used (except for a few, like "the", "a", "and", etc).

How would I go about searching this block of text for its most commonly used words?

like image 491
j.s Avatar asked Jul 03 '11 20:07

j.s


People also ask

How do I check if a string contains a specific word in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check if a word is in a sentence JavaScript?

You can check if a JavaScript string contains a character or phrase using the includes() method, indexOf(), or a regular expression. includes() is the most common method for checking if a string contains a letter or series of letters, and was designed specifically for that purpose.

How do you check if a string contains a pattern in JavaScript?

To check if a substring is contained in a JavaScript string:Call the indexOf method on the string, passing it the substring as a parameter - string. indexOf(substring) Conditionally check if the returned value is not equal to -1. If the returned value is not equal to -1 , the string contains the substring.


4 Answers

You should split the string into words, then loop through the words and increment a counter for each one:

var wordCounts = { };
var words = str.split(/\b/);

for(var i = 0; i < words.length; i++)
    wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;

The "_" + allows it to process words like constructor that are already properties of the object.

You may want to write words[i].toLowerCase() to count case-insensitively.

like image 92
SLaks Avatar answered Oct 03 '22 08:10

SLaks


Here is my approach

  • First, separate the words from the string using Regular Expression.
  • Declare an object as a Map which will help you to find the occurrences of each word. (You can use Map Data Structure!)
  • Find the most repeated word from that object.

let str = 'How do you do?';
console.log(findMostRepeatedWord(str)); // Result: "do"

function findMostRepeatedWord(str) {
  let words = str.match(/\w+/g);
  console.log(words); // [ 'How', 'do', 'you', 'do' ]

  let occurances = {};

  for (let word of words) {
    if (occurances[word]) {
      occurances[word]++;
    } else {
      occurances[word] = 1;
    }
  }

  console.log(occurances); // { How: 1, do: 2, you: 1 }

  let max = 0;
  let mostRepeatedWord = '';

  for (let word of words) {
    if (occurances[word] > max) {
      max = occurances[word];
      mostRepeatedWord = word;
    }
  }

  return mostRepeatedWord;
}
like image 30
MD. Sakib Khan Avatar answered Oct 03 '22 08:10

MD. Sakib Khan


I started with Gustavo Maloste's suggestion and added filtering for sticky words.

let str = 'Delhi is a crowded city. There are very few rich people who travel by their own vehicles. The majority of the people cannot afford to hire a taxi or a three-wheeler. They have to depend on D.T.C. buses, which are the cheapest mode of conveyance. D.T.C. buses are like blood capillaries of our body spreading all over in Delhi. One day I had to go to railway station to receive my uncle. I had to reach there by 9.30 a.m. knowing the irregularity of D.T.C. bus service; I left my home at 7.30 a.m. and reached the bus stop. There was a long queue. Everybody was waiting for the bus but the buses were passing one after another without stopping. I kept waiting for about an hour. I was feeling very restless and I was afraid that I might not be able to reach the station in time. It was 8.45. Luckily a bus stopped just in front of me. It was overcrowded but somehow I managed to get into the bus. Some passengers were hanging on the footboard, so there was no question of getting a seat. It was very uncomfortable. We were feeling suffocated. All of a sudden, an old man declared that his pocket had been picked. He accused the man standing beside him. The young man took a knife out of his pocket and waved it in the air. No body dared to catch him. I thanked God when the bus stopped at the railway station. I reached there just in time.';
//console.log(findMostRepeatedWord(str)); // Result: "do"

let occur = nthMostCommon(str, 10);

console.log(occur);

function nthMostCommon(str, amount) {

  const stickyWords =[
    "the",
    "there",
    "by",
    "at",
    "and",
    "so",
    "if",
    "than",
    "but",
    "about",
    "in",
    "on",
    "the",
    "was",
    "for",
    "that",
    "said",
    "a",
    "or",
    "of",
    "to",
    "there",
    "will",
    "be",
    "what",
    "get",
    "go",
    "think",
    "just",
    "every",
    "are",
    "it",
    "were",
    "had",
    "i",
    "very",
    ];
    str= str.toLowerCase();
    var splitUp = str.split(/\s/);
    const wordsArray = splitUp.filter(function(x){
    return !stickyWords.includes(x) ;
            });
    var wordOccurrences = {}
    for (var i = 0; i < wordsArray.length; i++) {
        wordOccurrences['_'+wordsArray[i]] = ( wordOccurrences['_'+wordsArray[i]] || 0 ) + 1;
    }
    var result = Object.keys(wordOccurrences).reduce(function(acc, currentKey) {
        /* you may want to include a binary search here */
        for (var i = 0; i < amount; i++) {
            if (!acc[i]) {
                acc[i] = { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] };
                break;
            } else if (acc[i].occurences < wordOccurrences[currentKey]) {
                acc.splice(i, 0, { word: currentKey.slice(1, currentKey.length), occurences: wordOccurrences[currentKey] });
                if (acc.length > amount)
                    acc.pop();
                break;
            }
        }
        return acc;
    }, []);
 
    return result;
    }
like image 3
Daniel Lefebvre Avatar answered Oct 03 '22 08:10

Daniel Lefebvre


by this function, you can have a list of most frequent words. this function returns an array.

findMostFrequentWords = (string) => {
var wordsArray = string.split(/\s/);
var wordOccurrences = []
for (var i = 0; i < wordsArray.length; i++) {
    wordOccurrences[wordsArray[i]] = (wordOccurrences[wordsArray[i]] || 0) + 1;
}
const maximum = Object.keys(wordOccurrences).reduce(function (accomulated, current) {
    return wordOccurrences[current] >= wordOccurrences[accomulated] ? current : accomulated;
});
const result = []
Object.keys(wordOccurrences).map((word) => {
    if (wordOccurrences[word] === wordOccurrences[maximum])
        result.push(word);
})
return result
}
like image 1
Mohammad Avatar answered Oct 03 '22 09:10

Mohammad