Hướng dẫn remove stop words from string python without nltk - loại bỏ các từ dừng khỏi chuỗi python mà không có nltk

Trong bài viết này, bạn sẽ thấy các kỹ thuật khác nhau để loại bỏ các từ dừng khỏi các chuỗi trong Python. Các từ dừng là những từ trong ngôn ngữ tự nhiên có rất ít ý nghĩa, chẳng hạn như "là", "một", "The", v.v. các truy vấn người dùng.

Các từ dừng thường được xóa khỏi văn bản trước khi đào tạo các mô hình học tập sâu và học máy vì các từ dừng xảy ra rất nhiều, do đó cung cấp rất ít hoặc không có thông tin duy nhất có thể được sử dụng để phân loại hoặc phân cụm.

Loại bỏ các từ dừng bằng python

Với ngôn ngữ lập trình Python, bạn có vô số tùy chọn để sử dụng để loại bỏ các từ dừng khỏi chuỗi. Bạn có thể sử dụng một trong một số thư viện xử lý ngôn ngữ tự nhiên như NLTK, Spacy, Gensim, TextBlob, v.v. hoặc nếu bạn cần kiểm soát hoàn toàn các từ dừng mà bạn muốn xóa, bạn có thể viết tập lệnh tùy chỉnh của riêng mình.

Trong bài viết này, bạn sẽ thấy một số phương pháp khác nhau, tùy thuộc vào thư viện NLP bạn đang sử dụng.

  • Dừng từ với NLTK
  • Dừng lời nói với Gensim
  • Dừng từ với Spacy

Sử dụng thư viện NLTK của Python

Thư viện NLTK là một trong những thư viện Python lâu đời nhất và được sử dụng phổ biến nhất để xử lý ngôn ngữ tự nhiên. 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

print(stopwords.words('english'))
0. Để 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.

Hãy xem một ví dụ đơn giản:

from nltk.corpus import stopwords
nltk.download('stopwords')
from nltk.tokenize import word_tokenize

text = "Nick likes to play football, however he is not too fond of tennis."
text_tokens = word_tokenize(text)

tokens_without_sw = [word for word in text_tokens if not word in stopwords.words()]

print(tokens_without_sw)

Trong tập lệnh trên, trước tiên chúng tôi nhập bộ sưu tập

print(stopwords.words('english'))
1 từ mô -đun
print(stopwords.words('english'))
2. Tiếp theo, chúng tôi nhập phương thức
print(stopwords.words('english'))
3 từ lớp
print(stopwords.words('english'))
4. Sau đó, chúng tôi tạo một biến
print(stopwords.words('english'))
5, chứa một câu đơn giản. Câu trong biến
print(stopwords.words('english'))
5 được mã hóa (chia thành các từ) bằng phương pháp
print(stopwords.words('english'))
3. Tiếp theo, chúng tôi lặp lại tất cả các từ trong danh sách
print(stopwords.words('english'))
8 và kiểm tra xem từ này có tồn tại trong bộ sưu tập các từ dừng hay không. Nếu từ không tồn tại trong bộ sưu tập stopword, nó sẽ được trả lại và được thêm vào danh sách
print(stopwords.words('english'))
9. Danh sách
print(stopwords.words('english'))
9 sau đó được in.

Đây là cách câu trông mà không có các từ dừng:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']

Bạn có thể thấy rằng các từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
1,
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
2,
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
3,
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 và
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
5 đã bị xóa khỏi câu.

Bạn có thể tham gia danh sách các từ trên để tạo một câu mà không cần dừng các từ, như được hiển thị bên dưới:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)

Đây là đầu ra:

Nick likes play football , however fond tennis .

Thêm hoặc xóa các từ dừng trong danh sách từ dừng mặc định của NLTK

Bạn có thể thêm hoặc xóa các từ dừng theo lựa chọn của bạn vào bộ sưu tập các từ dừng hiện có trong NLTK. Trước khi xóa hoặc thêm các từ dừng trong NLTK, hãy xem danh sách tất cả các từ dừng tiếng Anh được NLTK hỗ trợ:

print(stopwords.words('english'))

Output:

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
Thêm các từ dừng vào mặc định NLTK Danh sách Word

Để thêm một từ vào bộ sưu tập các từ dừng NLTK, trước tiên hãy tạo một đối tượng từ danh sách

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
6. Tiếp theo, sử dụng phương thức
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
7 trong danh sách để thêm bất kỳ từ nào vào danh sách.

Tập lệnh sau đây thêm từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 vào bộ sưu tập từ dừng NLTK. Một lần nữa, chúng tôi xóa tất cả các từ khỏi biến
print(stopwords.words('english'))
5 của chúng tôi để xem từ
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 có bị xóa hay không.

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)

Output:

['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']

Đầu ra cho thấy từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 đã bị xóa.

Bạn cũng có thể thêm danh sách các từ vào danh sách

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
2 bằng phương thức
all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
3, như được hiển thị bên dưới:

sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)

Tập lệnh trên thêm hai từ

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 và
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 vào danh sách
all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
6. Trong đầu ra, bạn sẽ không thấy hai từ này như hình dưới đây:

Output:

['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
Xóa các từ dừng khỏi danh sách từ dừng NLTK mặc định

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
7 chỉ là danh sách các mục, bạn có thể xóa các mục khỏi danh sách này như bất kỳ danh sách nào khác. Cách đơn giản nhất để làm như vậy là thông qua phương thức
all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
8. Điều này rất hữu ích khi ứng dụng của bạn cần một từ dừng để không được xóa. Ví dụ: bạn có thể cần phải giữ từ
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 trong một câu để biết khi nào một tuyên bố đang bị phủ nhận.

Tập lệnh sau sẽ xóa từ dừng

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 khỏi danh sách các từ dừng mặc định trong NLTK:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
0

Output:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
1

Từ đầu ra, bạn có thể thấy rằng từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 chưa được xóa khỏi câu đầu vào.

Sử dụng thư viện Gensim của Python

Thư viện Gensim là một thư viện cực kỳ hữu ích khác để loại bỏ các từ dừng khỏi một chuỗi trong Python. Tất cả bạn phải làm là nhập phương thức

['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
2 từ mô -đun
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
3. Tiếp theo, bạn cần chuyển câu của mình mà bạn muốn xóa các từ dừng, phương thức
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
2 trả về chuỗi văn bản mà không cần các từ dừng.

Chúng ta hãy xem một ví dụ đơn giản về cách xóa các từ dừng thông qua thư viện Gensim.

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
2

Output:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
3

Điều quan trọng là phải đề cập rằng đầu ra sau khi xóa các từ dừng bằng thư viện NLTK và GENSIM là khác nhau. Ví dụ, thư viện GENSIM coi từ

['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
5 là một từ dừng trong khi NLTK không, và do đó không xóa nó. Điều này cho thấy rằng không có quy tắc cứng và nhanh nào về một từ dừng là gì và nó không phải là gì. Tất cả phụ thuộc vào nhiệm vụ mà bạn sẽ thực hiện.

Trong phần sau, bạn sẽ thấy cách thêm hoặc xóa các từ dừng vào một bộ sưu tập các từ dừng hiện có trong Gensim.

Thêm và xóa các từ dừng trong danh sách các từ dừng mặc định

Trước tiên chúng ta hãy xem các từ dừng trong Thư viện Gensim của Python:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
4

Output:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
5

Bạn có thể thấy rằng bộ sưu tập các từ dừng mặc định của Gensim được chi tiết hơn nhiều, khi so sánh với NLTK. Ngoài ra, GENSIM lưu trữ các từ dừng mặc định trong một đối tượng Set Frozen.

Thêm các từ dừng vào mặc định gensim Danh sách các từ dừng

Để truy cập danh sách các từ dừng GENSIM, bạn cần nhập bộ Frozen

['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
6 từ gói
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
7. Một bộ đông lạnh trong Python là một loại bộ là bất biến. Bạn không thể thêm hoặc loại bỏ các phần tử trong một bộ đông lạnh. Do đó, để thêm một phần tử, bạn phải áp dụng chức năng
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
8 trên bộ đóng băng và chuyển cho nó tập hợp các từ dừng mới. Phương thức
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
8 sẽ trả về một bộ mới chứa các từ dừng mới được thêm vào của bạn, như được hiển thị bên dưới.

Tập lệnh sau đây thêm

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 và
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 vào danh sách các từ dừng trong Gensim:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
6

Output:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
7

Từ đầu ra ở trên, bạn có thể thấy rằng các từ

sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
2 và
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
8 đã được coi là từ dừng và do đó đã bị xóa khỏi câu đầu vào.

Xóa các từ dừng khỏi danh sách dừng của Gensim mặc định

Để xóa các từ dừng khỏi danh sách các từ dừng của Gensim, bạn phải gọi phương thức

sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 trên đối tượng Set Frozen, chứa danh sách các từ dừng. Bạn cần chuyển một tập hợp các từ dừng mà bạn muốn loại bỏ khỏi bộ Frozen sang phương thức
sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4. Phương thức
sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 trả về một tập hợp chứa tất cả các từ dừng ngoại trừ các từ được chuyển sang phương thức
sw_list = ['likes','play']
all_stopwords.extend(sw_list)

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4.

Tập lệnh sau đây sẽ loại bỏ từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 khỏi tập hợp các từ dừng trong Gensim:

Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
8

Output:

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
9

Vì từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 hiện đã bị xóa khỏi bộ từ dừng, bạn có thể thấy rằng nó chưa được xóa khỏi câu đầu vào sau khi xóa từ dừng.

Sử dụng thư viện Spacy

Thư viện Spacy trong Python là một ngôn ngữ cực kỳ hữu ích khác để xử lý ngôn ngữ tự nhiên trong Python.

Để cài đặt Spacy, bạn phải thực thi tập lệnh sau trên thiết bị đầu cuối lệnh của mình:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
0

Khi thư viện được tải xuống, bạn cũng cần tải xuống mô hình ngôn ngữ. Một số mô hình tồn tại trong Spacy cho các ngôn ngữ khác nhau. Chúng tôi sẽ cài đặt mô hình tiếng Anh. Thực hiện lệnh sau trong thiết bị đầu cuối của bạn:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
1

Khi mô hình ngôn ngữ được tải xuống, bạn có thể xóa các từ dừng khỏi văn bản bằng Spacy. Nhìn vào kịch bản sau:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
2

Trong tập lệnh trên, trước tiên chúng tôi tải mô hình ngôn ngữ và lưu trữ nó trong biến

['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
0.
['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
1 là một tập hợp các từ dừng mặc định cho mô hình tiếng Anh trong Spacy. Tiếp theo, chúng tôi chỉ đơn giản lặp qua từng từ trong văn bản đầu vào và nếu từ tồn tại trong tập từ dừng của mô hình ngôn ngữ Spacy, từ sẽ bị xóa.

Đây là đầu ra:

Output:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
3

Thêm và xóa các từ dừng trong danh sách từ dừng mặc định của Spacy

Giống như các thư viện NLP khác, bạn cũng có thể thêm hoặc xóa các từ dừng khỏi danh sách từ dừng mặc định trong Spacy. Nhưng trước đó, chúng ta sẽ thấy một danh sách tất cả các từ dừng hiện có trong Spacy.

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
4

Output:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
5

Đầu ra cho thấy có 326 từ dừng trong danh sách các từ dừng mặc định trong thư viện Spacy.

Thêm các từ dừng vào danh sách các từ dừng mặc định

Danh sách từ dừng Spacy về cơ bản là một tập hợp các chuỗi. Bạn có thể thêm một từ mới vào bộ giống như bạn sẽ thêm bất kỳ mục mới nào vào một bộ.

Nhìn vào tập lệnh sau trong đó chúng tôi thêm từ

['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
2 vào danh sách các từ dừng hiện có trong Spacy:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
6

Output:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
7

Đầu ra cho thấy từ

['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
2 đã bị xóa khỏi câu đầu vào.

Bạn cũng có thể thêm nhiều từ vào danh sách các từ dừng trong spacy như được hiển thị bên dưới. Tập lệnh sau đây thêm

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 và
['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
2 vào danh sách các từ dừng trong Spacy:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
8

Output:

filtered_sentence = (" ").join(tokens_without_sw)
print(filtered_sentence)
9

Ouput cho thấy tha các từ

all_stopwords = stopwords.words('english')
all_stopwords.append('play')

text_tokens = word_tokenize(text)
tokens_without_sw = [word for word in text_tokens if not word in all_stopwords]

print(tokens_without_sw)
4 và
['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
2 đều đã bị xóa khỏi câu đầu vào.

Xóa các từ dừng khỏi danh sách các từ dừng Spacy mặc định

Để xóa một từ khỏi tập hợp các từ dừng trong Spacy, bạn có thể chuyển từ để xóa phương thức

['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
8 của tập hợp.

Tập lệnh sau đây loại bỏ từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 khỏi tập hợp các từ dừng trong Spacy:

Nick likes play football , however fond tennis .
0

Output:

Nick likes play football , however fond tennis .
1

Trong đầu ra, bạn có thể thấy rằng từ

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
4 chưa được xóa khỏi câu đầu vào.

Sử dụng tập lệnh tùy chỉnh để xóa các từ dừng

Trong phần trước, bạn đã thấy khác nhau về cách chúng ta có thể sử dụng các thư viện khác nhau để xóa các từ dừng khỏi một chuỗi trong Python. Nếu bạn muốn kiểm soát hoàn toàn việc xóa Word Word, bạn có thể viết tập lệnh của riêng mình để xóa các từ dừng khỏi chuỗi của bạn.

Bước đầu tiên trong vấn đề này là xác định danh sách các từ mà bạn muốn được coi là từ dừng. Hãy tạo danh sách một số từ dừng được sử dụng phổ biến nhất:

Nick likes play football , however fond tennis .
2

Tiếp theo, chúng tôi sẽ xác định một hàm sẽ chấp nhận một chuỗi dưới dạng tham số và sẽ trả về câu mà không cần các từ dừng:

Nick likes play football , however fond tennis .
3

Bây giờ chúng ta hãy cố gắng xóa các từ dừng khỏi câu mẫu:

Nick likes play football , however fond tennis .
4

Output:

Nick likes play football , however fond tennis .
5

Bạn có thể thấy rằng các từ dừng tồn tại trong danh sách

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
01 đã bị xóa khỏi câu đầu vào.

Vì danh sách

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
01 là một danh sách đơn giản các chuỗi, bạn có thể thêm hoặc xóa các từ vào nó. Ví dụ: hãy thêm một từ
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 trong danh sách
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
01 và một lần nữa xóa các từ dừng khỏi câu đầu vào:

Nick likes play football , however fond tennis .
4

Output:

Nick likes play football , however fond tennis .
5

Đầu ra bây giờ cho thấy từ

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 cũng bị xóa khỏi câu đầu vào khi chúng tôi thêm từ trong danh sách các từ dừng tùy chỉnh của chúng tôi.

Bây giờ chúng ta hãy xóa từ

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 khỏi danh sách các từ dừng và một lần nữa áp dụng xóa từ dừng vào câu đầu vào của chúng tôi:

Nick likes play football , however fond tennis .
8

Output:

Nick likes play football , however fond tennis .

Từ

['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 chưa được xóa ngay bây giờ vì chúng tôi đã xóa nó khỏi danh sách danh sách các từ dừng của chúng tôi.

Sự kết luận

Trong bài viết này, bạn đã thấy các thư viện khác nhau có thể được sử dụng để xóa các từ dừng khỏi một chuỗi trong Python. Bạn cũng đã thấy cách thêm hoặc xóa các từ dừng khỏi danh sách các từ dừng mặc định được cung cấp bởi các thư viện khác nhau. Cuối cùng, chúng tôi đã chỉ ra cách này có thể được thực hiện nếu bạn có một tập lệnh tùy chỉnh được sử dụng để xóa các từ dừng.

Làm thế nào để bạn loại bỏ những từ vô nghĩa trong Python?

1 câu trả lời..
Nhập NLTK ..
Words = set (nltk.corpus.words.words ()).
đã gửi = "io andiamo đến bãi biển với amico của tôi.".
"" .Join (w cho w trong nltk.wordpunc_tokenize (đã gửi) \.
Nếu w.lower () bằng từ hoặc không w.isalpha ()).
# 'Io đến bãi biển với của tôi'.

Làm cách nào để xóa các điểm dừng trong NLP?

Loại bỏ từ dừng sử dụng Spacy Spacy là một trong những thư viện linh hoạt và được sử dụng rộng rãi nhất trong NLP. Chúng ta có thể loại bỏ các từ dừng nhanh chóng và hiệu quả khỏi văn bản đã cho bằng cách sử dụng Spacy. Nó có một danh sách các từ dừng riêng của nó có thể được nhập dưới dạng stop_words từ spacy.using spaCy spaCy is one of the most versatile and widely used libraries in NLP. We can quickly and efficiently remove stopwords from the given text using SpaCy. It has a list of its own stopwords that can be imported as STOP_WORDS from the spacy.

CountVectorizer có loại bỏ các từ dừng không?

Các bước bao gồm loại bỏ các từ dừng, lemmatizing, xuất phát, mã thông báo và vector hóa.Vectorization là một quá trình chuyển đổi dữ liệu văn bản thành một biểu mẫu có thể đọc được bằng máy.removing stop words, lemmatizing, stemming, tokenization, and vectorization. Vectorization is a process of converting the text data into a machine-readable form.

Làm thế nào để bạn xóa các điểm dừng khỏi TextBlob?

Chỉ cần thay đổi [] s của bạn thành () như trong: (Word for in in blob. Từ nếu không có trong từ dừng. Từ ('tiếng Anh')).Bạn sẽ không bao giờ có thể truy cập lại danh sách sau khi bạn sử dụng nó nhưng dù sao bạn cũng tham gia ngay.change your [] s to () as in: (word for word in blob. words if word not in stopwords. words('english')) . You'll never be able to access the list again after you use it but you join it right away anyway.