Các chữ cái trong bảng chữ cái là các khối xây dựng cho các từ trong tiếng Anh. Những từ này nhóm lại với nhau để tạo thành một câu bằng cách tuân theo các quy tắc ngữ pháp. Vì lý do ngữ pháp, một số từ xuất hiện thường xuyên hơn những từ khác. Mục tiêu chính của những từ này là kết nối các từ khác nhau trong một câu. Những từ này được gọi là Stopwords. Nói chung, Stopwords không mang ý nghĩa từ điển
Các từ khóa thường được sử dụng cùng với Xử lý ngôn ngữ tự nhiên [NLP]. Cố gắng xóa các từ dừng trước khi xử lý dữ liệu của bạn cho tác vụ NLP
Các thư viện khác nhau có số lượng từ khác nhau trong bộ sưu tập từ dừng của họ. Không có tiêu chuẩn nào theo dõi bộ sưu tập các từ dừng. Điều này dẫn đến việc các thư viện có bộ sưu tập từ khóa của họ
Trong bài viết này, chúng ta sẽ đi qua các thư viện này
1. Bộ công cụ ngôn ngữ tự nhiên [NLTK]NLTK là một công cụ python hàng đầu để xử lý trước văn bản. Loại bỏ các từ dừng bằng thư viện NLTK
from nltk.corpus import stopwordsnltk_stopwords = set[stopwords.words[‘english’]]text = f”The first time I saw Catherine she was wearing a vivid crimson dress and was nervously “ \
f”leafing through a magazine in my waiting room.”text_without_stopword = [word for word in text.split[] if word.lower[] not in nltk_stopwords]print[f”Original Text : {text}”]
print[f”Text without stopwords : {‘ ‘.join[text_without_stopword]}”]
print[f”Total count of stopwords in NLTK is {len[nltk_stopwords]}”]
Thư viện NLTK có 179 từ trong bộ sưu tập từ dừng. Như bạn có thể quan sát, hầu hết các từ như was, the, và I đều bị loại bỏ khỏi câu
Ghi chú. Tất cả các từ trong danh sách từ dừng của thư viện mặc định đều ở dạng chữ thường. Để có kết quả tốt hơn, hãy chuyển đổi các từ trong tài liệu/câu thành chữ thường. Mặt khác, từ dừng không bị xóa khỏi dữ liệu của bạn
2. SpaCySpacy cũng sử dụng rộng rãi các thư viện trong NLP. Loại bỏ Stopwords bằng thư viện spaCy
import spacysp = spacy.load[‘en_core_web_sm’]
spacy_stopwords = sp.Defaults.stop_wordstext = f”The first time I saw Catherine she was wearing a vivid crimson dress and was nervously “ \
f”leafing through a magazine in my waiting room.”text_without_stopword = [word for word in text.split[] if word not in spacy_stopwords]print[f”Original Text : {text}”]
print[f”Text without stopwords : {‘ ‘.join[text_without_stopword]}”]
print[f”Total count of stopwords in SpaCy is {len[spacy_stopwords]}”]
SpaCy có 326 từ trong bộ sưu tập mật khẩu của họ, gấp đôi so với mật khẩu NLTK. Spacy và NLTK hiển thị đầu ra khác nhau sau khi xóa từ dừng. Kết quả của nó là các thư viện có định nghĩa về từ dừng, điều này thúc đẩy số lượng từ của họ trong từ dừng. Chúng bao gồm thứ nhất, thứ hai, mười hai, v.v. từ số. Danh sách của họ cũng bao gồm các động từ xuất hiện thường xuyên như đi, tìm, v.v.
3. gensimXóa Stopwords bằng thư viện genism
from gensim.parsing.preprocessing import remove_stopwords
import gensimgensim_stopwords = gensim.parsing.preprocessing.STOPWORDStext = f”The first time I saw Catherine she was wearing a vivid crimson dress and was nervously “ \
f”leafing through a magazine in my waiting room.”print[f”Original Text : {text}”]
print[f”Text without stopwords : {remove_stopwords[text.lower[]]}”]
print[f”Total count of stopwords in Gensim is {len[list[gensim_stopwords]]}”]
Gensim có 337 từ trong bộ sưu tập từ khóa của họ. Bộ sưu tập từ khóa của họ tương tự như Spacy. Hàm remove_stopwords loại bỏ các từ dừng trực tiếp khỏi câu
4. Scikit-họcScikit-learning rất phổ biến trong Mô hình hóa dữ liệu. Loại bỏ Stopwords bằng thư viện Scikit-learning
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDStext = f”The first time I saw Catherine she was wearing a vivid crimson dress and was nervously “ \
f”leafing through a magazine in my waiting room.”text_without_stopword = [word for word in text.split[] if word not in ENGLISH_STOP_WORDS]print[f”Original Text : {text}”]
print[f”Text without stopwords : {‘ ‘.join[text_without_stopword]}”]
print[f”Total count of stopwords in Sklearn is {len[ENGLISH_STOP_WORDS]}”]
Scikit-learning có 318 từ trong bộ sưu tập từ khóa của họ. Bộ sưu tập từ khóa của họ tương tự như Spacy và Gensim
Ghi chú. Đôi khi, các từ có thể phân loại là từ dừng không có sẵn trong danh sách từ dừng mặc định của thư viện trên. Bạn có thể sửa đổi bộ sưu tập từ khóa hiện có theo lựa chọn của mình. Sử dụng append để thêm hoặc xóa để xóa các từ khỏi bộ sưu tập từ dừng
Sửa đổi danh sách từ dừng mặc định của thư viện
from nltk.corpus import stopwords
nltk_stopwords = stopwords.words[‘english’]text = f”The first time I saw Catherine she was wearing a vivid crimson dress and was nervously “ \
f”leafing through a magazine in my waiting room.”text_without_stopword = [word for word in text.split[] if word.lower[] not in nltk_stopwords]print[f”Original Text : {text}”]
print[f”Text without stopwords : {‘ ‘.join[text_without_stopword]}”]# ‘wearing’ added as a stopwords in nltk stopwords collectionnltk_stopwords.append[‘wearing’]
text_without_stopword = [word for word in text.split[] if word.lower[] not in nltk_stopwords]print[f”Text with wearing stopwords added : {‘ ‘.join[text_without_stopword]}”]# ‘my’ removed as a stopwords in nltk stopwords collectionnltk_stopwords.remove[‘my’]text_without_stopword = [word for word in text.split[] if word.lower[] not in nltk_stopwords]print[f”Text with my stopwords removed : {‘ ‘.join[text_without_stopword]}”]
Sự kết luận
Việc lựa chọn thư viện python cho mật khẩu chỉ phụ thuộc vào nhiệm vụ NLP. Nếu bạn sử dụng thư viện NLTK để xử lý văn bản thì không nên sử dụng thư viện Gensim cho từ dừng. Loại bỏ từ dừng làm giảm thời gian xử lý và dung lượng ổ đĩa và tăng độ chính xác. Vì vậy, hãy làm sạch dữ liệu của bạn bằng cách xóa từ khóa trước khi đào tạo mô hình của bạn