8

HI I am looking for a library that'll remove stop words from text in Javascript, my end goal is to calculate tf-idf and then convert the given document into vector space, and all of this is Javascript. Can anyone point me to a library that'll help me do that.Just a library to remove the stop words would also be great.

Sangeet Menon
  • 9,555
  • 8
  • 40
  • 58
dhaval2025
  • 317
  • 2
  • 5
  • 12

4 Answers4

11

Use the stopwords provided by the NLTK library:

stopwords = ['i','me','my','myself','we','our','ours','ourselves','you','your','yours','yourself','yourselves','he','him','his','himself','she','her','hers','herself','it','its','itself','they','them','their','theirs','themselves','what','which','who','whom','this','that','these','those','am','is','are','was','were','be','been','being','have','has','had','having','do','does','did','doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about','against','between','into','through','during','before','after','above','below','to','from','up','down','in','out','on','off','over','under','again','further','then','once','here','there','when','where','why','how','all','any','both','each','few','more','most','other','some','such','no','nor','not','only','own','same','so','than','too','very','s','t','can','will','just','don','should','now']

Then simply pass your string into the following function:

function remove_stopwords(str) {
    res = []
    words = str.split(' ')
    for(i=0;i<words.length;i++) {
       word_clean = words[i].split(".").join("")
       if(!stopwords.includes(word_clean)) {
           res.push(word_clean)
       }
    }
    return(res.join(' '))
}  

Example:

remove_stopwords("I will go to the place where there are things for me.")

Result:

I go place things

Just add any words to your NLTK array that aren't covered already.

Cybernetic
  • 12,628
  • 16
  • 93
  • 132
  • ??? I mena `me` is in your stop words list, why was it showed up in result? – tim Feb 21 '20 at 03:59
  • 1
    Wasn’t checking for periods. The period on my sentence messed it up. Fixed the function to now handle periods properly. Cheers. – Cybernetic Feb 21 '20 at 04:25
6

I think there are no libraries for such thing, you need to download those words from https://www.ranks.nl/stopwords.

And then do replace the words as follows:

text = text.replace(stopword, "")
emen
  • 6,050
  • 11
  • 57
  • 94
yura
  • 14,489
  • 21
  • 77
  • 126
2

Here's an array with english stopwords. Hope it helps. From http://www.ranks.nl/stopwords (mentioned in previous answer).

Also, this could be a helpful resource for you.

https://github.com/shiffman/A2Z-F16/tree/gh-pages/week5-analysis

http://shiffman.net/a2z/text-analysis/

var stopwords = ["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any","are","aren't","as","at","be","because","been","before","being","below","between","both","but","by","can't","cannot","could","couldn't","did","didn't","do","does","doesn't","doing","don't","down","during","each","few","for","from","further","had","hadn't","has","hasn't","have","haven't","having","he","he'd","he'll","he's","her","here","here's","hers","herself","him","himself","his","how","how's","i","i'd","i'll","i'm","i've","if","in","into","is","isn't","it","it's","its","itself","let's","me","more","most","mustn't","my","myself","no","nor","not","of","off","on","once","only","or","other","ought","our","ours","ourselves","out","over","own","same","shan't","she","she'd","she'll","she's","should","shouldn't","so","some","such","than","that","that's","the","their","theirs","them","themselves","then","there","there's","these","they","they'd","they'll","they're","they've","this","those","through","to","too","under","until","up","very","was","wasn't","we","we'd","we'll","we're","we've","were","weren't","what","what's","when","when's","where","where's","which","while","who","who's","whom","why","why's","with","won't","would","wouldn't","you","you'd","you'll","you're","you've","your","yours","yourself","yourselves"];
tim
  • 1,454
  • 1
  • 25
  • 45
Tom Hall
  • 283
  • 3
  • 5
1

There's a Javascript Library for removing stopwords here: http://geeklad.com/remove-stop-words-in-javascript

Sam Joseph
  • 4,584
  • 4
  • 31
  • 47