Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript code to filter out common words in a string

I'm trying to build JavaScript code that reads one string (say a sentence of English text), then outputs another string of (comma-separated) words that were "uncommon". Something like:

    var sentence="The dog ran to the other side of the field."; 

    var common_words="the, it is, we all, a, an, by, to, you, me, he, she, they, we, how, it, i, are, to, for, of"; 

--Some JavaScript code--

    var uncommon_words="dog, ran, other, side, field"; 

How can I do this?

like image 842
Test Tester Avatar asked Jul 13 '11 23:07

Test Tester


1 Answers

Here you go:

function getUncommon(sentence, common) {
    var wordArr = sentence.match(/\w+/g),
        commonObj = {},
        uncommonArr = [],
        word, i;

    common = common.split(',');
    for ( i = 0; i < common.length; i++ ) {
        commonObj[ common[i].trim() ] = true;
    }

    for ( i = 0; i < wordArr.length; i++ ) {
        word = wordArr[i].trim().toLowerCase();
        if ( !commonObj[word] ) {
            uncommonArr.push(word);
        }
    }

    return uncommonArr;
}

Live demo: http://jsfiddle.net/simevidas/knXkS/

like image 136
Šime Vidas Avatar answered Nov 15 '22 20:11

Šime Vidas