Search code examples
pythonnltktext-analysis

How do I remove stop words from an arraylist of strings in python?


I would like to remove stop words from the arraylist named arrayList1, which is stored in the data variable. I try the below method but it does not work. Please help me check the below codes and improve the codes.Thanks.

import Retrieve_ED_Notes
from nltk.corpus import stopwords

data = Retrieve_ED_Notes.arrayList1

stop_words = set(stopwords.words('english'))


def remove_stopwords(data):
 data = [word for word in data if word not in stop_words]
 return data

for i in range(0, len(remove_stopwords(data))):
  print(remove_stopwords(data[i]))

Console output of the arrayList1:

1|I really love writing journals
2|The mat is very comfortable and I will buy it again likes
3|The mousepad is smooth
1|I really love writing journals
4|This pen is very special to me.
4|This pencil is very special to me.
5|Meaningful novels
4|It brights up my day like a lighter and makes me higher.
6|School foolscap
7|As soft as my heart.lovey

Solution

  • convert the word to lower and check with stopwords.

    from nltk.corpus import stopwords
    stopwords=set(stopwords.words('english'))
    
    data =['I really love writing journals','The mat is very comfortable and I will buy it again likes','The mousepad is smooth']
    
    def remove_stopwords(data):
        output_array=[]
        for sentence in data:
            temp_list=[]
            for word in sentence.split():
                if word.lower() not in stopwords:
                    temp_list.append(word)
            output_array.append(' '.join(temp_list))
        return output_array
    
    output=remove_stopwords(data)
    
    print(output)
    ['really love writing journals','mat comfortable buy likes', 'mousepad smooth']