Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to implement nltk.stopwords

I am trying to remove stopwords in my data with nltk, but after several attempts I am unable to remove the stopwords. The tokenization part of my code works, but I am unable to understand why stopwords does not work.

def pre_process(text):
    
    # remove special characters and digits
    text=re.sub("(\\d|\\W|_)+"," ",text)
    text=re.split("\W+",text)
    
    return text
text = dat['text'].apply(lambda x:pre_process(x))
nltk.download('stopwords')

def remove_stopwords(text):
    for word in text:
        if word in stopwords.words('english'):
            text.remove(word)
        return text

text_stopword = text.apply(lambda x:remove_stopwords(x))

The code should remove words such as ‘the’, but after running my csv through the code, that words such as ‘the’ is still present.

Current results:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

text returns:

[tv, future, in, the, hands, of, viewers, with...

text_stopword returns:

[tv, future, in, the, hands, of, viewers, with...

>Solution :

Your return statement in remove_stopwords function is wrongly indented. Due to that function returns text right after the first iteration.

Please go with:

def remove_stopwords(text):
    for word in text:
        if word in stopwords.words('english'):
            text.remove(word)
    return text

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading