0

I've got an array of words I need to sort by frequency. Before I do that, I need to strip out words like 'the,' 'it,' etc (anything less than three letters, really), as well as all numerals and any words beginning with # (the array of words is pulled from Twitter, although the example below is just a random paragraph from Wikipedia).

I can remove one word, but have been going crazy trying to remove more than one, or a range. Any suggestions? Thank you!

http://jsfiddle.net/9NzAC/6/

HTML:

<div id="text" style="background-color:Teal;position:absolute;left:100px;top:10px;height:500px;width:500px;">
Phrenology is a pseudoscience primarily focused on measurements of the human skull, based on the concept that the brain is the organ of the mind, and that certain brain areas have localized, specific functions or modules. The distinguishing feature of phrenology is the idea that the sizes of brain areas were meaningful and could be inferred by examining the skull of an individual.
</div>

JS:

//this is the function to remove words
<script type="text/javascript">
    function removeA(arr){
        var what, a= arguments, L= a.length, ax;
        while(L> 1 && arr.length){
            what= a[--L];
            while((ax= arr.indexOf(what))!= -1){
                arr.splice(ax, 1);
            }
        }
            return arr;
        }
</script>

//and this does the sorting & counting
<script type="text/javascript">
    var getMostFrequentWords = function(words) {
        var freq={}, freqArr=[], i;

        // Map each word to its frequency in "freq".
            for (i=0; i<words.length; i++) {
            freq[words[i]] = (freq[words[i]]||0) + 1;
        }

        // Sort from most to least frequent.
            for (i in freq) freqArr.push([i, freq[i]]);
            return freqArr.sort(function(a,b) { return b[1] - a[1]; });
        };

        var words = $('#text').get(0).innerText.split(/\s+/);

        //Remove articles & words we don't care about.
        var badWords = "the";
            removeA(words,badWords);
        var mostUsed = getMostFrequentWords(words);
        alert(words);

</script>
3
  • I recommend you to do array[i] = null (or "") and then just clean up your arrays empty nodes. You can easily achieve that using Array#filter Commented Aug 1, 2012 at 3:24
  • 1
    If you have any trouble then check this out for help. jsfiddle.net/n2jj4/1 Commented Aug 1, 2012 at 5:00
  • What a terrifically useful & comprehensive piece of code. Thank you so much. This is so helpful. Commented Aug 1, 2012 at 5:48

3 Answers 3

2

Instead of removing from the original array, just push to a new one, it's simpler, and it'll make your code shorter and more readable.

var words = ['the', 'it', '12', '#twit', 'aloha', 'hello', 'bye']
var filteredWords = []

for (var i = 0, l = words.length, w; i < l; i++) {
    w = words[i]
    if (!/^(#|\d+)/.test(w) && w.length > 3)
        filteredWords.push(w)
}

console.log(filteredWords) // ['aloha', 'hello']

Demo: http://jsfiddle.net/VcfvU/

Sign up to request clarification or add additional context in comments.

2 Comments

Wow. That's exactly it. Thank you so, so much.
It's highly not recommended to hide the brackets, and also recommended to put the semicolon D:
1

I recommend you to do array[i] = null (or "") and then just clean up your arrays empty nodes. You can easily achieve that using Array#filter

Test: http://jsfiddle.net/6LPep/ Code:

var FORGETABLE_WORDS = ',the,of,an,and,that,which,is,was,';

var words = text.innerText.split(" ");

for(var i = 0, word; word = words[i++]; ) {
    if (FORGETABLE_WORDS.indexOf(',' + word + ',') > -1 || word.length < 3) {
      words[i-1] = "";
    }
}

// falsy will get deleted
words.filter(function(e){return e});
// as example
output.innerHTML = words.join(" ");

// just continue doing your stuff with "words" array.
// ...​

I think it's cleaner than the way you're doing it currently. If you need anything else I will update this answer.

1 Comment

Thank you very much for your help with this! Learned a new technique there - thanks!
0

console.log(
  ['🍇','🍈','🍌','🍉','🍊','🍋'].filter(a => !['🍌','🍊'].includes(a))
)

Comments

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.