0

This is my code, which gets the most common words in English from Wikipedia, then collects all the words on the page and filters the most common words from the list of words:

// get table data from most common words
var arr = [];
$.ajax({
   url: 'https://en.wikipedia.org/wiki/Most_common_words_in_English',
})
.done(function(html) {
   var table = $(html).find(".wikitable");
   for(var i = 1; i < 101; i++) {
       arr[i - 1] = table[0].rows[i].children[0].innerText;
   }
});

var text = document.getElementById('content').innerText;
var words = text.split(/[^a-zA-Z]/);
//filter empty strings
words = words.filter(Boolean);
//filter single characters
words = words.filter(function(word) {
    return word.length > 1;
});

words = words.filter(function(word) {
    return word !== 'was';
});

words = words.filter(function(word) {
    return word !== 'where';
});

words = words.filter(function(word) {
    return word !== 'is';
});

words = words.filter(function(word) {
    return word !== 'are';
});

// filter stopWords from 100 most common words wikipedia page
words = words.filter(function(word) {
    var isNotStopWord = true;
    var i = 0;
    for(var stopWord in arr) {
        if(word === arr[i++]) {
            isNotStopWord = false;
            break;
        }
    }
    return isNotStopWord;
});

But this last block of code doesn't seem to run:

// filter stopWords from 100 most common words wikipedia page
words = words.filter(function(word) {
    var isNotStopWord = true;
    var i = 0;
    for(var stopWord in arr) {
        if(word === arr[i++]) {
            isNotStopWord = false;
            break;
        }
    }
    return isNotStopWord;
});

Unless I paste that part onto the console and run it again?

Apple
  • 401
  • 1
  • 4
  • 7
  • Where do you store words in "content"? I see an AJAX function where the A stands for Asynchronous which means the result of the call is not available until AFTER the call, e.g. in the `done` - next time please click the `<>` and post the HTML too – mplungjan Sep 10 '17 at 07:10
  • 1
    Also `words = words.filter(function(word) { return word.length>1 && stopWords.indexOf(word) ===-1; });` – mplungjan Sep 10 '17 at 07:14
  • So your complete code is `var stopWords = []; $.get('https://en.wikipedia.org/wiki/Most_common_words_in_English',function(html) { var table = $(html).find(".wikitable"); for(var i = 0; i < 100; i++) { stopWords.push(table[0].rows[i].children[0].innerText); } var text = document.getElementById('content').innerText; var words = text.split(/[^a-zA-Z]/); words = words.filter(function(word) { return word.length>1 && stopWords.indexOf(word) ===-1; }); $("#someContainer").append(words.join('
    '); });`
    – mplungjan Sep 10 '17 at 07:23
  • If I declare the words in the function, I can't use it outside the AJAX call. I want to able to use that list of words later on. – Apple Sep 10 '17 at 07:36
  • Then define it as a global but you can NOT access it until after the Ajax finishes – mplungjan Sep 10 '17 at 08:14

1 Answers1

0

that part of code runs even on first time but as arr[] is empty for first time you may not see its effect. this code should work if you move that piece of code in ajax done method.

  var arr = [];
    $.ajax({
       url: 'https://en.wikipedia.org/wiki/Most_common_words_in_English',
    })
    .done(function(html) {
       var table = $(html).find(".wikitable");
       for(var i = 1; i < 101; i++) {
           arr[i - 1] = table[0].rows[i].children[0].innerText;
       }
     words = words.filter(function (element,index){
         return (arr.indexOf(element) === -1)
     })


    console.log(words)

    });

    var text = document.getElementById('content').innerText;
    var words = text.split(/[^a-zA-Z]/);
    //filter empty strings
    words = words.filter(Boolean);
    //filter single characters
    words = words.filter(function(word) {
        return word.length > 1;
    });

    words = words.filter(function(word) {
        return word !== 'was';
    });

    words = words.filter(function(word) {
        return word !== 'where';
    });

    words = words.filter(function(word) {
        return word !== 'is';
    });

    words = words.filter(function(word) {
        return word !== 'are';
    });
Manoj
  • 1,175
  • 7
  • 11
  • I tried moving that block of code into the done function but i'm still seeing the same result. the declaration of text and words also has to be put before the ajax call – Apple Sep 10 '17 at 07:34
  • is your ajax call completes? can you check that in network tab? – Manoj Sep 10 '17 at 07:39
  • [Here](http://imgur.com/a/zJ5gc) is my network tab. I'm not sure. – Apple Sep 10 '17 at 07:45
  • Do you get any error in console? I see No 'Access-Control-Allow-Origin' in my console – Manoj Sep 10 '17 at 07:57
  • I think you have to do the call on a wikipedia page i.e. https://en.wikipedia.com – Apple Sep 11 '17 at 20:08
  • I checked it in wiki page and it works fine.I edited one of filter method to reduce line of code. is it not working for you? – Manoj Sep 11 '17 at 20:35
  • I got it to work. I had to put the declaration of arr in the ajax call though. – Apple Sep 11 '17 at 20:36
  • ideally thats not needed but any ways if it works then nothing like it. enjoy:) – Manoj Sep 11 '17 at 20:38