Từ dừng là những từ thường được sử dụng nhưng thường bị công cụ tìm kiếm bỏ qua, chẳng hạn như
import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
7, import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
8, import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
9, v.v. Những từ này được loại bỏ để tiết kiệm không gian trong cơ sở dữ liệu và thời gian xử lý. Câu, import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
0 không có từ dừng sẽ chỉ là import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
1Trong hướng dẫn này, chúng ta sẽ thảo luận về cách loại bỏ các từ dừng trong Python
Sử dụng Gói import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
0 để xóa các từ dừng trong Python
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
Gói
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
1 [Xử lý ngôn ngữ tự nhiên] có thể được sử dụng để xóa các từ dừng khỏi văn bản trong Python. Gói này chứa các từ dừng từ nhiều ngôn ngữ khác nhauChúng tôi có thể lặp qua một danh sách và kiểm tra xem một từ có phải là từ dừng hay không bằng cách sử dụng danh sách từ thư viện này
Ví dụ,
import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
đầu ra
Đoạn mã sau sẽ hiển thị danh sách các từ dừng trong Python
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
đầu ra
{'ourselves', 'hers', 'between', 'yourself', 'but', 'again', 'there', 'about', 'once', 'during', 'out', 'very', 'having', 'with', 'they', 'own', 'an', 'be', 'some', 'for', 'do', 'its', 'yours', 'such', 'into', 'of', 'most', 'itself', 'other', 'off', 'is', 's', 'am', 'or', 'who', 'as', 'from', 'him', 'each', 'the', 'themselves', 'until', 'below', 'are', 'we', 'these', 'your', 'his', 'through', 'don', 'nor', 'me', 'were', 'her', 'more', 'himself', 'this', 'down', 'should', 'our', 'their', 'while', 'above', 'both', 'up', 'to', 'ours', 'had', 'she', 'all', 'no', 'when', 'at', 'any', 'before', 'them', 'same', 'and', 'been', 'have', 'in', 'will', 'on', 'does', 'yourselves', 'then', 'that', 'because', 'what', 'over', 'why', 'so', 'can', 'did', 'not', 'now', 'under', 'he', 'you', 'herself', 'has', 'just', 'where', 'too', 'only', 'myself', 'which', 'those', 'i', 'after', 'few', 'whom', 't', 'being', 'if', 'theirs', 'my', 'against', 'a', 'by', 'doing', 'it', 'how', 'further', 'was', 'here', 'than'}
Sử dụng Gói import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
2 để xóa các từ dừng trong Python
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
Gói
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
2 được sử dụng để xóa các từ dừng khỏi văn bản trong Python. Gói này chứa các từ dừng từ nhiều ngôn ngữ như tiếng Anh, tiếng Đan Mạch, tiếng Pháp, tiếng Tây Ban Nha, v.v.Ví dụ,
from stop_words import get_stop_words
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in get_stop_words['english']]
print[A]
đầu ra
Đoạn mã trên sẽ lọc tập dữ liệu bằng cách xóa tất cả các từ dừng được sử dụng trong ngôn ngữ tiếng Anh
Sử dụng Phương pháp import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
4 trong Thư viện import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
5 để xóa các từ dừng trong Python
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
Phương thức
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
6 trong thư viện import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
5 được sử dụng để xóa các từ dừng khỏi văn bản trong Python Từ dừng là những từ phổ biến không đóng vai trò lớn trong việc phân loại văn bản. Các công cụ tìm kiếm thường bỏ qua chúng vì chúng không thực sự giúp thu hẹp kết quả cho một cụm từ tìm kiếm nhất định. A, the, it, he, she, and an là những từ dừng phổ biến trong tiếng Anh
Bởi vì chúng không cung cấp nhiều giá trị, nên loại bỏ các từ dừng trước khi xử lý văn bản cho các tác vụ xử lý ngôn ngữ tự nhiên [NLP] sẽ có ích. Hãy tưởng tượng cơ sở dữ liệu của bạn sẽ lớn hơn bao nhiêu nếu bao gồm tất cả những từ phổ biến này?
Thư viện Python NLTK chứa danh sách các từ dừng mặc định. Để xóa các từ dừng, bạn cần chia văn bản của mình thành các 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 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ệ
Trong hướng dẫn này, chúng tôi sẽ hướng dẫn bạn cách xóa các từ dừng khỏi văn bản bằng thư viện NLTK cho Python
Cài đặt thư viện cần thiết
Để chạy các tập lệnh Python trong hướng dẫn này, bạn cần cài đặt Thư viện NLTK. Thực thi tập lệnh sau trên thiết bị đầu cuối lệnh của bạn để cài đặt Thư viện NLTK
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
2Dừng loại bỏ từ
Thư viện NLTK hỗ trợ loại bỏ từ dừng từ nhiều ngôn ngữ. Để xem danh sách các ngôn ngữ được NLTK hỗ trợ để loại bỏ từ dừng, hãy gọi hàm
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
37 từ lớp từ dừng của thenltk. corpusmodule như được hiển thị trong đoạn script sauimport nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
3Dưới đây là danh sách tất cả các ngôn ngữ được thư viện NLKT hỗ trợ để loại bỏ từ dừng
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
5Trước tiên, chúng tôi sẽ làm việc với các từ dừng tiếng Anh, sau đó chúng tôi sẽ chỉ cho bạn một ví dụ tiếng Pháp trong trường hợp bạn tình cờ phát triển một công cụ NLP đa ngôn ngữ
Xóa các từ dừng tiếng Anh
Để lấy danh sách các từ dừng tiếng Anh, bạn phải truyền'english'làm tham số cho hàm
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
38 như hình bên dướiimport nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
7Dưới đây là danh sách tất cả các từ dừng tiếng Anh có trong thư viện NLTK
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
8Để xóa các từ dừng khỏi chuỗi văn bản, bạn cần chia văn bản của mình thành các mã thông báo [từ]. Tiếp theo, bạn sẽ lặp qua danh sách mã thông báo và chỉ giữ lại những mã thông báo không có trong danh sách từ dừng. Đây là một ví dụ
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
9Đầu ra cho thấy các từ dừng như you, do, not, to, a và with bị xóa khỏi văn bản như hình bên dưới
import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
0Hãy nhớ rằng, bạn có thể sử dụng RegexpTokenizer để xóa dấu chấm câu khỏi danh sách mã thông báo của mình
Xóa các từ dừng tiếng Pháp
Để có danh sách các từ dừng tiếng Pháp, bạn phải chuyển'french'as một tham số cho hàm
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
38 như được hiển thị bên dướiimport nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
2Dưới đây là danh sách tất cả các từ dừng tiếng Pháp
import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
3Ví dụ sau đây cho thấy cách xóa các từ dừng khỏi văn bản tiếng Pháp. Quá trình này tương tự như loại bỏ các từ dừng tiếng Anh; . Bạn bỏ qua các từ tồn tại trong danh sách các từ dừng và trả về các từ còn lại. Cuối cùng, bạn có thể nối danh sách các từ không có từ dừng để xây dựng lại chuỗi của bạn mà không có từ dừng bằng cách sử dụng hàm
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
50Đây là một ví dụ
import nltk
from nltk.corpus import stopwords
dataset = ['This','is','just', 'a', 'snake']
A = [word for word in dataset if word not in stopwords.words['english']]
print[A]
5Ở đầu ra, bạn sẽ thấy các từ dừng tiếng Pháp i. e. suis, un và en bị xóa khỏi văn bản
import nltk
from nltk.corpus import stopwords
print[stopwords.words['english']]
30Nhận miễn phí Bộ công cụ dành cho nhà phát triển Python của chúng tôi
Tôi đã tập hợp Bộ công cụ dành cho nhà phát triển Python với hơn 100 tập lệnh Python dựng sẵn bao gồm cấu trúc dữ liệu, Pandas, NumPy, Seaborn, máy học, xử lý tệp, quét web và nhiều thứ khác - và tôi muốn bạn có bộ công cụ này miễn phí. Nhập địa chỉ email của bạn dưới đây và tôi sẽ gửi một bản sao theo cách của bạn