Hướng dẫn remove stop words python nltk - xóa từ dừng python nltk

Quá trình chuyển đổi dữ liệu thành một cái gì đó mà máy tính có thể hiểu được gọi là xử lý trước. Một trong những hình thức chính của xử lý trước là lọc ra dữ liệu vô dụng. Trong xử lý ngôn ngữ tự nhiên, các từ vô dụng (dữ liệu), được gọi là các từ dừng. & Nbsp; & nbsp;pre-processing. One of the major forms of pre-processing is to filter out useless data. In natural language processing, useless words (data), are referred to as stop words. 
 

Những từ dừng là gì?

Các từ dừng: Một từ dừng là một từ thường được sử dụng (chẳng hạn như là The The The, một Do kết quả của một truy vấn tìm kiếm. & nbsp; chúng tôi sẽ không muốn những từ này chiếm không gian trong cơ sở dữ liệu của chúng tôi hoặc chiếm thời gian xử lý có giá trị. Đối với điều này, chúng ta có thể loại bỏ chúng một cách dễ dàng, bằng cách lưu trữ một danh sách các từ mà bạn xem xét để ngăn chặn các từ. NLTK (Bộ công cụ ngôn ngữ tự nhiên) trong Python có một danh sách các từ dừng được lưu trữ trong 16 ngôn ngữ khác nhau. Bạn có thể tìm thấy chúng trong thư mục NLTK_DATA. Trang chủ/Pratima/NLTK_DATA/Corpora/Stopwords là địa chỉ thư mục. A stop word is a commonly used word (such as “the”, “a”, “an”, “in”) that a search engine has been programmed to ignore, both when indexing entries for searching and when retrieving them as the result of a search query. 
We would not want these words to take up space in our database, or taking up valuable processing time. For this, we can remove them easily, by storing a list of words that you consider to stop words. NLTK(Natural Language Toolkit) in python has a list of stopwords stored in 16 different languages. You can find them in the nltk_data directory. home/pratima/nltk_data/corpora/stopwords is the directory address.(Do not forget to change your home directory name)

Hướng dẫn remove stop words python nltk - xóa từ dừng python nltk

Để kiểm tra danh sách các từ dừng, bạn có thể nhập các lệnh sau vào vỏ Python. & NBSP; & nbsp; 
 

import nltk
from nltk.corpus import stopwords
print(stopwords.words('english'))

s Có ',' với ',' họ ',' riêng ',' an ',' be ',' some ',' for ',' do ',' its ',' yours ',' như ',' thành 'thành' , 'của', 'hầu hết', 'chính nó', 'khác', 'off', 'là', 's', 'am', 'hoặc', 'who', 'as', 'from', '' anh ấy ',' mỗi ',' ',' chính họ ',' cho đến ',' bên dưới ',' là ',' chúng tôi ',' những 'này', 'của bạn', 'của anh ấy', 'qua', 'don' , 'cũng không', 'tôi', 'là', 'cô ấy', 'hơn', 'chính anh ấy', 'cái này', 'xuống', 'nên', 'của chúng tôi', 'của họ', 'trong khi', ' Ở trên ',' cả ',' lên ',' thành ',' của chúng tôi ',' đã ',' cô ấy ',' tất cả ',' không ',' khi ',' tại ',' bất kỳ ',' trước 'trước' , 'họ', 'giống nhau', 'và', 'đã', 'có', 'trong', 'will', 'on', 'không', 'chính mình', 'sau đó', 'đó', ',' bởi vì ',' cái gì ',' trên ',' tại sao ',' vì vậy ',' có thể ',' đã làm ',' không ',' bây giờ ',' dưới ',' anh ',' bạn ',' cô ấy ' , 'có', 'công bằng', 'ở đâu', 'quá', 'chỉ', 'bản thân mình', 'mà', 'những người', 'i', 'sau', 'vài', 'ai', '' T ',' là ',' nếu ',' của họ ',' của tôi ',' chống lại ',' a ',' bởi ',' làm ',' nó ',' làm thế nào ',' hơn nữa ',' là 'là' , 'đây', 'tha Nát} & nbsp; Lưu ý: Bạn thậm chí có thể sửa đổi danh sách bằng cách thêm các từ bạn chọn vào tiếng Anh .txt. tệp trong thư mục stopwords. & nbsp; & nbsp;
Note: You can even modify the list by adding words of your choice in the english .txt. file in the stopwords directory. 
 

Xóa các từ dừng bằng NLTK

Chương trình sau đây loại bỏ các từ dừng khỏi một đoạn văn bản: & nbsp; & nbsp;
 

Python3

from nltk.corpus import stopwords

from nltk.tokenize import word_tokenize

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
0
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
2
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
4
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
5
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
6
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
7

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
8
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 from0

from1

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 from3from4 from5from6
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
8__

from1

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 nltk.corpus 5

from4 from5from6 nltk.corpus 9

import0from8 from5from9 from6 import5

import6import7

import8import9

import8stopwords1

Output:

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

Hướng dẫn remove stop words python nltk - xóa từ dừng python nltk

Thực hiện các hoạt động stopwords trong một tệp

Trong mã bên dưới, Text.txt là tệp đầu vào gốc trong đó các từ dừng sẽ được xóa. FilteredText.txt là tệp đầu ra. Nó có thể được thực hiện bằng cách sử dụng mã sau: & nbsp; & nbsp;
 

Python3

import stopwords3

from nltk.corpus import stopwords7

from nltk.tokenize import from1

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
2
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
4
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
5
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
6from7

from8

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 nltk.tokenize 0nltk.tokenize 1nltk.tokenize 2__

nltk.tokenize 4

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 nltk.tokenize 6

nltk.tokenize 7

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
1 nltk.tokenize 9

from4 import1from6 import3

import0from8 from9 import1from6 import9

Các

import6

['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
00
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
01
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
02
['This', 'is', 'a', 'sample', 'sentence', ',', 'showing', 
'off', 'the', 'stop', 'words', 'filtration', '.']
['This', 'sample', 'sentence', ',', 'showing', 'stop',
'words', 'filtration', '.']
03

import6

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

Đây là cách chúng tôi làm cho nội dung được xử lý của chúng tôi hiệu quả hơn bằng cách loại bỏ các từ không đóng góp cho bất kỳ hoạt động nào trong tương lai. Bài viết này được đóng góp bởi Pratima Upadhyay. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác. Xin vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. & NBSP;
This article is contributed by Pratima Upadhyay. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


Làm thế nào để bạn loại bỏ các từ dừng bằng nltk?

NLTK hỗ trợ loại bỏ Word Stop và bạn có thể tìm thấy danh sách các từ dừng trong mô -đun Corpus. Để xóa các từ dừng khỏi một câu, bạn có thể chia văn bản của mình thành các từ và sau đó xóa từ nếu nó thoát trong danh sách các từ dừng do NLTK cung cấp.divide your text into words and then remove the word if it exits in the list of stop words provided by NLTK.

Làm thế nào để bạn loại bỏ các từ dừng trong python mà không có nltk?

Lặp qua từng từ trong tệp Word dừng và đính kèm nó vào danh sách, sau đó lặp qua từng từ trong tệp khác.Thực hiện danh sách hiểu và xóa từng từ xuất hiện trong danh sách từ dừng.Lưu câu trả lời này.

Làm thế nào để bạn loại bỏ các từ dừng khỏi mã thông báo trong Python?

Để xóa các từ dừng, bạn cần chia văn bản của mình thành mã thông báo (từ), sau đó kiểm tra xem mỗi mã thông báo có khớp với các từ trong danh sách các từ dừng của bạn không.Nếu mã thông báo khớp với một từ dừng, bạn bỏ qua mã thông báo.Nếu không, bạn thêm mã thông báo vào danh sách các từ hợp lệ.divide your text into tokens (words), and then check if each token matches words in your list of stop words. If the token matches a stop word, you ignore the token. Otherwise you add the token to the list of valid words.

Làm thế nào để bạn loại bỏ các điểm dừng và dấu câu Python NLTK?

Để loại bỏ các từ dừng và dấu câu bằng NLTK, chúng tôi phải tải xuống tất cả các từ dừng bằng NLTK.Tải xuống ('stopwords'), sau đó chúng tôi phải chỉ định ngôn ngữ mà chúng tôi muốn xóa các từ dừng, do đó, chúng tôi sử dụng các từ dừng.Các từ ('tiếng Anh') để chỉ định và lưu nó vào biến.