1

If I have two strings like those

s1 = "This is a foo bar sentence ."
s2 = "This sentence is similar to a foo bar sentence ."

And I want to split the string to be in this format

x1 = ["This":1,"is":1,"a":1,"bar":1,"sentence":1,"foo":1]
x2 = ["This":1,"is":1,"a":1,"bar":1,"sentence":2,"similar":1,"to":1,"foo":1]

It split the string words and count them, to a pair of where each string represent a word and the number represent the count of this word in the string.

1
  • 2
    What code have you got so far? Note: you cannot store a value "This":1 in an array, you need either a double indexed array or an associative dictionary for that. Commented Aug 17, 2013 at 9:55

1 Answer 1

8

Remove punctuation, normalize whitespace, lowercase, split at the space, use a loop to count word occurrences into an index object.

function countWords(sentence) {
  var index = {},
      words = sentence
              .replace(/[.,?!;()"'-]/g, " ")
              .replace(/\s+/g, " ")
              .toLowerCase()
              .split(" ");

    words.forEach(function (word) {
        if (!(index.hasOwnProperty(word))) {
            index[word] = 0;
        }
        index[word]++;
    });

    return index;
}

Or, in ES6 arrow-function style:

const countWords = sentence => sentence
  .replace(/[.,?!;()"'-]/g, " ")
  .replace(/\s+/g, " ")
  .toLowerCase()
  .split(" ")
  .reduce((index, word) => {
    if (!(index.hasOwnProperty(word))) index[word] = 0;
    index[word]++;
    return index;
  }, {});
Sign up to request clarification or add additional context in comments.

3 Comments

can I ask you one more question @Tomalak, If I have two maps for those and I want to get the intersection "common words between them" how can I do that ?
for example If I have two arrays, word and temp, each of them has words and it's counts, how can i get the intersection between them regardless the counts ?
I've served you this complete solution on a silver platter; this is already more than I should have done. This problem is a) really simple and b) has been solved by other people before me. You simply preferred not to look very hard because it's so easy to let someone on SO do all the work. Show some decency and do some of the thinking yourself, the answer to your second question is right in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.