Đọc mọi câu chuyện của George Pipis [và hàng nghìn nhà văn khác trên Medium]. Phí thành viên của bạn hỗ trợ trực tiếp…
jorgepit-14189. trung bình. com
Trong nhiều nhiệm vụ NLP, cần phải xóa “từ dừng” khỏi văn bản. Thông thường, "từ dừng" có nghĩa là những từ xuất hiện thường xuyên và không đóng góp nhiều vào ý nghĩa tổng thể của câu. Một số ví dụ về từ dừng là {"a", "an", "the", "this", "that", "is", "it", "to", "and"}
, v.v.
Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách xóa các điểm dừng trong Python bằng thư viện NLTK
Hãy tải các thư viện
import nltk
nltk.download['stopwords']
nltk.download['punkt']
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
Các từ dừng tiếng Anh được đưa ra bởi danh sách
stopwords.words['english']
Tuy nhiên, ai đó có thể tạo danh sách từ dừng của riêng họ như
stop_words = ["a", "an", "the", "this", "that", "is", "it", "to", "and"]
Cách thêm từ dừng vào danh sách từ dừng NLTKHoặc bạn có thể thêm từ dừng tùy chỉnh của mình vào danh sách từ dừng NLTK. Ví dụ
# stopwords from NLTKCách xóa từ dừng khỏi danh sách từ dừng NLTK
my_stopwords = nltk.corpus.stopwords.words['english']# my new custom stopwords
my_extra = ['abc', 'google', 'apple']# add the new custom stopwrds to my stopwords
my_stopwords.extend[my_extra]
Tương tự, bạn có thể xóa một số từ khỏi “danh sách từ dừng” bằng cách sử dụng khả năng hiểu danh sách. Ví dụ
# remove these words from stop wordsCách xóa từ dừng khỏi văn bản
my_lst = ['have', 'few']# update the stopwords list without the words above
my_stopwords = [el for el in my_stopwords if el not in my_lst]
Bây giờ, chúng tôi đã sẵn sàng để xóa các từ dừng khỏi văn bản. Hãy xem xét văn bản vô nghĩa sau đây cho mục đích triển lãm
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 chuỗi trong Python. 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ư "is", "an", "the", v.v. Các công cụ tìm kiếm và các nền tảng lập chỉ mục doanh nghiệp khác thường lọc các từ dừng trong khi tìm nạp kết quả từ cơ sở dữ liệu theo truy vấn của người dùng
Các từ dừng thường bị xóa khỏi văn bản trước khi đào tạo các mô hình học sâu và học máy vì các từ dừng xuất hiện rất nhiều, do đó cung cấp í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ỏ Stop Words 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 nhằm loại bỏ các từ dừng khỏi chuỗi. Bạn có thể sử dụng một trong số các 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 toàn quyền kiểm soát 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ố cách tiếp cận khác nhau, tùy thuộc vào thư viện NLP mà bạn đang sử dụng.
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ỏ từ dừng và bạn có thể tìm thấy danh sách từ dừng trong mô-đun
print[stopwords.words['english']]
0. Để loại bỏ các từ dừng trong câu, bạn có thể chia văn bản của mình thành các từ và sau đó loại bỏ từ đó nếu nó thoát ra khỏi danh sách các từ dừng do NLTK cung cấpHã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 ta 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 cách sử dụng phương pháp print[stopwords.words['english']]
3. Tiếp theo, chúng tôi lặp qua tất cả các từ trong danh sách print[stopwords.words['english']]
8 và kiểm tra xem từ đó có tồn tại trong bộ sưu tập từ dừng hay không. Nếu từ không tồn tại trong bộ sưu tập từ dừng, nó sẽ được trả lại và thêm vào danh sách print[stopwords.words['english']]
9. Danh sách print[stopwords.words['english']]
9 sau đó được in raĐây là cách câu trông không 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âuBạn có thể nối danh sách các từ trên để tạo thành câu không có từ dừng, như hình 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 từ dừng trong danh sách từ dừng mặc định của NLTK
Bạn có thể thêm hoặc bớt các từ dừng theo lựa chọn của mình vào bộ sưu tập các từ dừng hiện có trong NLTK. Trước khi loại bỏ 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']]
đầu ra
['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 từ dừng vào danh sách từ dừng NLTK mặc địnhĐể thêm một từ vào bộ sưu tập 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 pháp ['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 trên danh sách để thêm bất kỳ từ nào vào danh sáchĐoạn script sau 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 liệu 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ôngall_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]
đầu ra
['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óaBạ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 cách sử dụng phương pháp 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ư minh họa bên dướisw_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]
Đoạn script 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. Ở đầu ra, bạn sẽ không thấy 2 từ này như hình bên dướiđầu ra
['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
Xóa các từ dừng khỏi danh sách từ dừng NLTK mặc địnhVì
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à một danh sách các mục nên bạn có thể xóa các mục khỏi danh sách này giống 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 pháp 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 hữu ích khi ứng dụng của bạn cần một từ dừng để không bị xóa. Ví dụ, bạn có thể cần 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 câu bị phủ địnhTập lệnh sau 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đầu ra
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
1Từ đầ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 bị xóa khỏi câu đầu vàoSử 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 để xóa các từ dừng khỏi một chuỗi trong Python. Tất cả những gì 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 mà bạn muốn loại bỏ các từ dừng sang phương thức ['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
2 trả về chuỗi văn bản không có các từ dừngHãy xem một ví dụ đơn giản về cách loại bỏ các từ dừng thông qua thư viện Gensim
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
2đầu ra
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
3Điều quan trọng cần đề cập là đầu ra sau khi loại bỏ 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à từ dừng trong khi NLTK thì không và do đó đã không xóa từ đó. Điều này cho thấy rằng không có quy tắc cứng nhắc nào về việc từ dừng là gì và từ nào không. Tất cả phụ thuộc vào nhiệm vụ mà bạn sẽ thực hiệnTrong phần sau, bạn sẽ thấy cách thêm hoặc xóa từ dừng vào bộ sưu tập từ dừng hiện có trong Gensim
Thêm và xóa Stop Words trong Danh sách Stop Words mặc định của Gensim
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đầu ra
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
5Bạn có thể thấy rằng bộ sưu tập từ dừng mặc định của Gensim chi tiết hơn nhiều so với NLTK. Ngoài ra, Gensim lưu trữ các từ dừng mặc định trong một đối tượng được cố định
Thêm từ dừng vào danh sách từ dừng Gensim mặc địnhĐể truy cập danh sách các từ dừng Gensim, bạn cần nhập bộ đóng băng
['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
6 từ gói ['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
7. Tập hợp cố định trong Python là một loại tập hợp không thay đổi. Bạn không thể thêm hoặc bớt phần tử trong tập hợp đã đóng băng. Do đó, để thêm một phần tử, bạn phải áp dụng hàm ['Nick', 'likes', 'football', ',', 'however', 'fond', 'tennis', '.']
8 trên tập hợp đã đó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 tập hợp 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Đoạn script sau 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đầu ra
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
7Từ đầ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àoXóa các từ dừng khỏi Danh sách từ dừng 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 tập hợp đã đóng băng, 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 xóa khỏi tập hợp đã đóng băng 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ừ những từ được truyền cho 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Đoạn script sau xóa 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 GensimHãy xem hướng dẫn thực hành, thực tế của chúng tôi để học Git, với các phương pháp hay nhất, tiêu chuẩn được ngành chấp nhận và bao gồm bảng gian lận. Dừng các lệnh Git trên Google và thực sự tìm hiểu nó
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
8đầu ra
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
9Vì 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ó không bị xóa khỏi câu đầu vào sau khi loại bỏ từ dừngSử 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]
0Khi 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 ngôn ngữ 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]
1Sau 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 đoạn script sau
filtered_sentence = [" "].join[tokens_without_sw]
print[filtered_sentence]
2Trong đoạn mã 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 ngôn ngữ tiếng Anh trong SpaCy. Tiếp theo, chúng tôi chỉ cần lặp qua từng từ trong văn bản đầu vào và nếu từ đó tồn tại trong bộ từ dừng của mô hình ngôn ngữ SpaCy, thì từ đó sẽ bị xóaĐây là đầu ra
đầu ra
filtered_sentence = [" "].join[tokens_without_sw]
print[filtered_sentence]
3Thêm và xóa 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 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đầu ra
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 từ dừng vào danh sách từ dừng SpaCy mặc địnhDanh 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 tập hợp giống như bạn sẽ thêm bất kỳ mục mới nào vào một tập hợp
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 Spacyfiltered_sentence = [" "].join[tokens_without_sw]
print[filtered_sentence]
6đầu ra
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àoBạn cũng có thể thêm nhiều từ vào danh sách từ dừng trong SpaCy như hình bên dưới. Đoạn script sau 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 SpaCyfiltered_sentence = [" "].join[tokens_without_sw]
print[filtered_sentence]
8đầu ra
filtered_sentence = [" "].join[tokens_without_sw]
print[filtered_sentence]
9Đầu ra cho thấy cả 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à ['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
2 đều đã bị xóa khỏi câu đầu vàoXóa Stop Words khỏi Danh sách Stop Words mặc định của SpaCyĐể 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ừ cần xóa sang phương thức
['Nick', 'football', ',', 'however', 'fond', 'tennis', '.']
8 của tập hợpTập lệnh sau xóa 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 SpaCyNick likes play football , however fond tennis .
0đầu ra
Nick likes play football , however fond tennis .
1Ở đầ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 bị xóa khỏi câu đầu vàoSử dụng tập lệnh tùy chỉnh để xóa 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 toàn quyền kiểm soát việc xóa từ dừng, bạn có thể viết tập lệnh của riêng mình để xóa 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 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 .
2Tiếp theo, chúng ta sẽ định nghĩa một hàm chấp nhận một chuỗi làm tham số và sẽ trả về câu không có từ dừng
Nick likes play football , however fond tennis .
3Bây giờ chúng ta hãy thử loại bỏ các từ dừng khỏi một câu mẫu
Nick likes play football , however fond tennis .
4đầu ra
Nick likes play football , however fond tennis .
5Bạ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àoVì danh sách
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
01 là một danh sách đơn giản gồm các chuỗi nên bạn có thể thêm hoặc bớt các từ trong đó. Ví dụ: hãy thêm một từ ['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 vào 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àoNick likes play football , however fond tennis .
4đầu ra
Nick likes play football , however fond tennis .
5Đầu ra bây giờ cho thấy rằng 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ừ này vào danh sách các từ dừng tùy chỉnh của chúng tôiBây giờ, hãy xóa từ
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 khỏi danh sách từ dừng và một lần nữa áp dụng loại bỏ từ dừng cho câu đầu vào của chúng taNick likes play football , however fond tennis .
8đầu ra
Nick likes play football , however fond tennis .
Từ
['Nick', 'likes', 'play', 'football', ',', 'however', 'fond', 'tennis', '.']
03 hiện chưa bị xóa vì chúng tôi đã xóa nó khỏi danh sách từ dừng của chúng tôiPhần 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 đã xem 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 do các thư viện khác nhau cung cấp. Cuối cùng, chúng tôi đã chỉ ra cách thực hiện điều này 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