Change language

Removing stopwords with NLTK in Python

|

The process of converting data into something a computer can understand is preprocessing. One of the main forms of preprocessing is filtering out unnecessary data. In natural language processing, useless words (data) are called stop words.

What are stop words?

Stop- words. Stop word — it is a commonly used word (eg, "the", "a", "an", "in") that the search engine has been programmed to ignore both when indexing records for search and when retrieving them. as a result of a search query.

We would not want these words to take up space in our database or take up precious processing time. To do this, we can easily remove them by keeping a list of words that you think are stop words. The NLTK (Natural Language Toolkit) in python contains a list of stop words stored in 16 different languages. You can find them in the nltk_data directory.  home / pratima / nltk_data / corpora / stopwords — this is the address of the directory (don’t forget to change the name of your home directory)

To check the stop word list, you can enter the following commands in the python shell.

 import nltk from nltk.corpus import stopwords set (stopwords.words (’english’)) 

{& # 39 ; we & # 39 ;, & # 39; her & # 39;, & # 39; between & # 39;, & # 39; ourselves & # 39;, & # 39; but & # 39;, & # 39; again & # 39; , & # 39; there & # 39;, & # 39; o & # 39;, & # 39; once & # 39;, & # 39; during & # 39;, & # 39; outside & # 39;, & # 39 ; very & # 39 ;, & # 39; have & # 39;, & # 39; with & # 39;, & # 39; they are & # 39;, & # 39; your & # 39;, & # 39; an & # 39;, & # 39; be & # 39;, & # 39; some & # 39;, & # 39; for & # 39;, & # 39; do & # 39;, & # 39; its & # 39;, & # 39; yours & # 39;, & # 39; such & # 39;, & # 39; into & # 39; , & # 39; of & # 39 ;, & # 39; most & # 39;, & # 39; most & # 39;, & # 39; other & # 39;, & # 39; off & # 39;, & # 39; is & # 39 ;, & # 39; s & # 39 ;, & # 39; am & # 39 ;, & # 39; or & # 39 ;, & # 39; who & # 39 ;, & # 39; as & # 39 ;, & # 39; from & # 39 ;, & # 39; to him, to everyone, to that, to ourselves, to, below, we, we, these, yours, him, to, not , "Neither", "I", "were", "her", "more", "he himself", "this", "down", "must", "our", "them", "bye", "Above & # 39;, & # 39; both & # 39;, & # 39; up & # 39;, & # 39; up to & # 39;, & # 39; our & # 39;, & # 39; had & # 39;, & # 39; she & # 39;, & # 39; all & # 39;, & # 39; no & # 39;, & # 39; when & # 39;, & # 39; in & # 39;, & # 39; any & # 39;, & # 39; up to & # 39; , & # 39; im & # 39;, & # 39; the same & # 39;, & # 39; and & # 39;, & # 39; was & # 39;, & # 39; have & # 39;, & # 39; in & # 39 ;, & # 39; will be & # 39 ;, & # 39; at & # 39;, & # 39; does & # 39 ;, & # 39; you & # 39;, & # 39; then & # 39 ;, & # 39; that & # 39;, & # 39; because “what”, “above”, “why”, “so”, “maybe”, “did”, “not”, “now”, “under”, “he”, “you”, “herself”, “Has”, “just”, “where”, “too”, “only”, “I”, “which”, “those”, “I”, “after”, “several”, “whom”, “t & # 39;, & # 39; being & # 39;, & # 39; if & # 39;, & # 39; their & # 39;, & # 39; my & # 39;, & # 39; against & # 39;, & # 39; a & # 39;, & # 39; by & # 39;, & # 39; do & # 39;, & # 39; this & # 39;, & # 39; like & # 39;, & # 39; further & # 39;, & # 39; was & # 39; here then n & # 39;}

Note. You can even change the list by adding words of your choice in English .txt. file in the stopwords directory.

Deleting stop words with NLTK

The following program removes stop words from a piece of text:

from nltk.corpus import stopwords

from nltk.tokenize import word_tokenize

 

example_sent = "This is a sample sentence, showing off the stop words filtration."

 

stop_words = set (stopwords.words ( < code class = "string"> ’english’ ))

  

word_tokens = word_tokenize (example_sent)

 

filtered_sentence = [w for w in word_tokens if not w in stop_words]

 

filtered_sentence = []

 

for w in word_tokens:

if w not in stop_words:

filtered_sentence.append (w)

 

print (word_tokens)

print (filtered_sentence)

Output:

 [’This’,’ is’, ’a’,’ sample’, ’sentence’,’, ’,’ showing’, ’off’,’ the’, ’stop’,’ words’, ’filtration’,’ .’] [’This’,’ sample’, ’sentence’,’, ’,’ showing’ , ’stop’,’ words’, ’filtration’,’ .’] 

Executing stop words in the file

In the below code text.txt is the original input file, which should be remove stop words. Filtertext.txt is the output file. This can be done with the following code:

import io

from nltk.corpus import stopwords

from nltk.tokenize import word_tokenize

# word_tokenize takes a string as input, not a file.

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

file1 = open ( "tex t.txt " )

line = file1.read () # Use this to read the contents of a file as a stream:

words = line.split ()

for r in words:

if not r in stop_words:

appendFile = open ( ’filteredtext.txt’ , ’a’ )

  appendFile.write ( "" + r)

appendFile.close ()

In this way, we improve the efficiency of the processed content by removing words that do not affect future operations.

This article is provided by Pratima Upadhyay . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically