Python ProgrammingPython Programming

How to remove stop words using NLTK?

Removing Stop Words from text data

Stop words are very common words that carry no meaning or less meaning compared to other keywords. In this code snippet, we are going to remove stop words by using the NLTK library.

from nltk.corpus import stopwords


data = ['Stuning even for the non-gamer: This sound track was beautiful!\
It paints the senery in your mind so well I would recomend\
it even to people who hate vid. game music! I have played the game Chrono \
Cross but out of all of the games I have ever played it has the best music! \
It backs away from crude keyboarding and takes a fresher step with grate\
guitars and soulful orchestras.\
It would impress anyone who cares to listen!']

# Remove stop words
stopwords = set(stopwords.words('english'))

output = []
for sentence in data:
    temp_list = []
    for word in sentence.split():
        if word.lower() not in stopwords:
            temp_list.append(word)
    output.append(' '.join(temp_list))


print(output)

['Stuning even non-gamer: sound track beautiful!It paints senery mind well would recomendit even people hate vid. game music! played game Chrono Cross games ever played best music! backs away crude keyboarding takes fresher step grateguitars soulful orchestras.It would impress anyone cares listen!']