2.3 ?去除停用詞(Stopword Removal)
去除停用詞(Stop Words)是自然語言處理中的一個常見任務(wù),它旨在去除文本中的常見、無實(shí)際語義的詞語,以便更準(zhǔn)確地進(jìn)行文本分析和處理。停用詞通常包括像“a”、“an”、“the”、“in”、“on”等常見的詞匯。
2.3.1 ?什么是停用詞
停用詞(Stop Words)是自然語言處理中的一類常見詞匯,通常是一些在文本中頻繁出現(xiàn)但通常被認(rèn)為沒有實(shí)際語義或信息價值的詞匯。這些詞匯通常包括常見的連接詞、介詞、冠詞、代詞和一些常見的動詞等。
停用詞的存在是因?yàn)樗鼈冊谖谋局袕V泛出現(xiàn),但通常對文本分析和處理任務(wù)沒有太多的信息價值,因?yàn)樗鼈冊诓煌奈谋局卸紩霈F(xiàn)。因此,去除這些停用詞可以減少文本中的噪聲,使文本處理更加準(zhǔn)確和有效。
在現(xiàn)實(shí)應(yīng)用中,一些常見的停用詞包括:
- 冠詞:a, an, the
- 介詞:in, on, at, by
- 連接詞:and, or, but
- 代詞:I, you, he, she, it
- 助動詞:is, am, are, have, has, do, does
停用詞的具體列表可以根據(jù)不同的自然語言處理任務(wù)和語言而有所不同。去除停用詞通常是文本預(yù)處理的一部分,以凈化文本并減少在文本分析中的干擾。去除停用詞后,文本分析算法可以更關(guān)注那些具有更高信息價值的詞匯,從而提高文本處理的效率和準(zhǔn)確性。
2.3.2??基于詞匯列表的去除
最簡單的去除停用詞方法是使用預(yù)定義的停用詞列表,將文本中包含在列表中的詞匯去除。這些列表通常包括常見的連接詞、介詞、冠詞等。例如下面是一個基于詞匯列表的去除停用詞例子。
實(shí)例2-13:基于詞匯列表的去除停用詞(源碼路徑:daima/2/qu01.py)
(1)首先,準(zhǔn)備一個包含停用詞的列表,例如:
stop_words = ["a", "an", "the", "in", "on", "at", "by", "and", "or", "but"]
(2)編寫實(shí)例文件qu01.py,使用上面的停用詞列表來去除文本中的停用詞,具體實(shí)現(xiàn)代碼如下所示。
# 待處理的文本
text = "This is an example sentence with some stop words that we want to remove."
# 將文本分詞
words = text.split()
# 去除停用詞
filtered_words = [word for word in words if word.lower() not in stop_words]
# 將處理后的單詞列表重建為文本
filtered_text = " ".join(filtered_words)
# 顯示原始文本和去除停用詞后的文本
print(f"原始文本: {text}")
print(f"去除停用詞后: {filtered_text}")
在上述代碼中,首先定義了停用詞列表 stop_words,然后將文本分詞,并使用列表推導(dǎo)式去除其中包含在停用詞列表中的詞匯。最后,我們將處理后的單詞列表重新組合成文本。執(zhí)行后會輸出:
原始文本: This is an example sentence with some stop words that we want to remove.
去除停用詞后: This is example sentence with some stop words that we want to remove.
2.3.3 ?基于詞頻的去除
基于詞頻的停用詞去除方法旨在去除在文本中頻率最高的詞匯,因?yàn)檫@些詞匯通常是停用詞,對文本分析任務(wù)沒有太多的信息價值。通常,文本中出現(xiàn)頻率最高的詞匯很可能是停用詞,因此去除它們可以降低文本中的噪聲。請看下面的例子,演示了使用基于詞頻的方法去除停用詞的過程。
實(shí)例2-14:使用基于詞頻的方法去除停用詞(源碼路徑:daima/2/qu02.py)
實(shí)例文件qu02.py的具體實(shí)現(xiàn)代碼如下所示。
from collections import Counter
# 待處理的文本
text = "This is an example sentence with some stop words that we want to remove. This is a simple example."
# 將文本分詞
words = text.split()
# 計(jì)算詞匯的詞頻
word_freq = Counter(words)
# 按詞頻降序排序
sorted_word_freq = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
# 確定頻率最高的詞匯
most_common_words = [word for word, freq in sorted_word_freq[:5]] # 假設(shè)保留前5個頻率最高的詞匯
# 去除頻率最高的詞匯
filtered_words = [word for word in words if word not in most_common_words]
# 將處理后的單詞列表重建為文本
filtered_text = " ".join(filtered_words)
# 顯示原始文本和去除停用詞后的文本
print(f"原始文本: {text}")
print(f"去除停用詞后: {filtered_text}")
在上述代碼中,首先將文本分詞并計(jì)算詞匯的詞頻。然后,我們按詞頻降序排序詞匯,并選擇保留前5個頻率最高的詞匯作為停用詞。最后,我們使用列表推導(dǎo)式去除文本中包含在停用詞列表中的詞匯,然后將處理后的單詞列表重新組合成文本。執(zhí)行后會輸出:
原始文本: This is an example sentence with some stop words that we want to remove. This is a simple example.
去除停用詞后: with some stop words that we want to remove. a simple example.
2.3.4 ?TF-IDF算法去除
使用TF-IDF(Term Frequency-Inverse Document Frequency)算法來確定文本中詞匯的重要性。根據(jù)TF-IDF值,可以去除在多個文檔中頻繁出現(xiàn)的詞匯,因?yàn)檫@些詞匯可能是停用詞。請看下面的例子,演示了scikit-learn庫使用TF-IDF算法去除停用詞的過程。
實(shí)例2-15:使用TF-IDF算法去除停用詞(源碼路徑:daima/2/qu03.py)
實(shí)例文件qu03.py的具體實(shí)現(xiàn)代碼如下所示。
from sklearn.feature_extraction.text import TfidfVectorizer
# 假設(shè)這是一個文檔集合,每個文檔是一個字符串
documents = [
"This is an example document with some stop words that we want to remove.",
"Another document with stop words.",
"One more example document.",
]
# 定義停用詞列表
stop_words = ["this", "is", "an", "with", "some", "that", "we", "to", "and", "one", "more"]
# 使用TF-IDF向量化器
tfidf_vectorizer = TfidfVectorizer(stop_words=stop_words)
# 訓(xùn)練TF-IDF模型并進(jìn)行轉(zhuǎn)換
tfidf_matrix = tfidf_vectorizer.fit_transform(documents)
# 獲取特征詞匯
feature_names = tfidf_vectorizer.get_feature_names()
# 將TF-IDF矩陣轉(zhuǎn)換為文本
filtered_text = []
for i, doc in enumerate(documents):
tfidf_scores = list(zip(feature_names, tfidf_matrix[i].toarray()[0]))
filtered_words = [word for word, tfidf in tfidf_scores if tfidf > 0.2] # 通過閾值選擇要保留的詞匯
filtered_text.append(" ".join(filtered_words))
# 顯示原始文本和去除停用詞后的文本
for i, (original, filtered) in enumerate(zip(documents, filtered_text)):
print(f"原始文本 {i+1}: {original}")
print(f"去除停用詞后 {i+1}: {filtered}")
print()
在上述代碼中,使用scikit-learn的TF-IDF向量化器來將文檔集合轉(zhuǎn)化為TF-IDF特征矩陣。我們定義了一個停用詞列表 stop_words,并在TF-IDF向量化器中使用它。然后,我們通過設(shè)置一個TF-IDF閾值來選擇要保留的詞匯,這可以根據(jù)文本特性進(jìn)行調(diào)整。執(zhí)行后會輸出:
原始文本 1: This is an example document with some stop words that we want to remove.
去除停用詞后 1: document example remove stop want words
原始文本 2: Another document with stop words.
去除停用詞后 2: another document stop words
原始文本 3: One more example document.
去除停用詞后 3: document example
同上面的執(zhí)行結(jié)果可知,已成功去除了停用詞。原始文本中的停用詞已被去除,留下了具有較高TF-IDF值的詞匯。這個過程可以幫助減少文本中的噪聲,提高文本分析的準(zhǔn)確性。
2.3.5 ?機(jī)器學(xué)習(xí)方法去除
利用機(jī)器學(xué)習(xí)技術(shù),可以訓(xùn)練模型來自動識別和去除停用詞。這種方法需要標(biāo)記文本中哪些詞匯是停用詞,然后使用分類器或聚類算法進(jìn)行去除。使用機(jī)器學(xué)習(xí)方法去除停用詞通常涉及訓(xùn)練一個二元分類器(停用詞 vs. 非停用詞),然后使用訓(xùn)練好的模型來預(yù)測文本中的詞匯是否為停用詞。下面是一個使用scikit-learn庫的一個簡單例子,使用樸素貝葉斯分類器來去除停用詞。
實(shí)例2-16:使用機(jī)器學(xué)習(xí)方法去除停用詞(源碼路徑:daima/2/qu04.py)
實(shí)例文件qu04.py的具體實(shí)現(xiàn)代碼如下所示。文章來源:http://www.zghlxwxcb.cn/news/detail-855234.html
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
# 準(zhǔn)備訓(xùn)練集
training_samples = [
"this is a stop word",
"machine learning is fun",
"remove these stop words",
"text analysis with ML",
"use ML to remove stopwords",
]
# 對應(yīng)的標(biāo)簽,0表示停用詞,1表示非停用詞
training_labels = [0, 1, 0, 1, 0]
stop_words = ["this", "is", "an", "with", "some", "that", "we", "to", "and", "one", "more"]
# 待處理的文本
text = "this is an example text with some stop words that we want to remove using ML."
# 使用TF-IDF向量化器
tfidf_vectorizer = TfidfVectorizer()
X_train = tfidf_vectorizer.fit_transform(training_samples)
# 訓(xùn)練樸素貝葉斯分類器
classifier = MultinomialNB()
classifier.fit(X_train, training_labels)
# 將待處理文本轉(zhuǎn)化為TF-IDF特征向量
X_test = tfidf_vectorizer.transform([text])
# 使用分類器來預(yù)測詞匯是否為停用詞
predicted_label = classifier.predict(X_test)
# 如果預(yù)測標(biāo)簽為1(非停用詞),則保留詞匯
if predicted_label == 1:
print("Original Text:", text)
print("Processed Text:", text)
else:
print("Original Text:", text)
print("Processed Text:", " ".join([word for word in text.split() if word.lower() not in stop_words]))
在上述代碼中,使用了一個簡單的訓(xùn)練集,包括一些標(biāo)記的停用詞和非停用詞樣本。使用TF-IDF向量化器將文本轉(zhuǎn)化為特征向量,然后使用樸素貝葉斯分類器進(jìn)行訓(xùn)練。最后,我們使用訓(xùn)練好的分類器來預(yù)測待處理文本中的詞匯是否為停用詞,如果預(yù)測為停用詞,則從文本中去除。執(zhí)行后會輸出:文章來源地址http://www.zghlxwxcb.cn/news/detail-855234.html
Original Text: this is an example text with some stop words that we want to remove using ML.
Processed Text: example text stop words want remove using ML.
未完待續(xù)
到了這里,關(guān)于(2-3)文本預(yù)處理算法:去除停用詞(Stopword Removal)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!