簡體   English   中英

在計算文本中單詞准確性的頻率時,如何忽略某些單詞?

[英]How to ignore some words when counting the frequency of a word accuracy in a text?

在計算文本中單詞准確性的頻率時,如何忽略諸如“ a”,“ the”之類的單詞?

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer

df= pd.DataFrame({'phrase': pd.Series('The large distance between cities. The small distance. The')})
f = CountVectorizer().build_tokenizer()(str(df['phrase']))

result = collections.Counter(f).most_common(1)

print result

答案是。 但我想把距離當作最常用的詞。

最好避免像這樣開始計數條目。

ignore = {'the','a','if','in','it','of','or'}
result = collections.Counter(x for x in f if x not in ignore).most_common(1)

另一種選擇是使用stop_words的參數CountVectorizer
這些是您不感興趣的詞,分析器將舍棄這些詞。

f = CountVectorizer(stop_words={'the','a','if','in','it','of','or'}).build_analyzer()(str(df['phrase']))
result = collections.Counter(f).most_common(1)
print result
[(u'distance', 1)]

請注意, tokenizer器不會執行預處理(小寫,重音符號去除)或刪除停用詞,因此您需要在此處使用分析器。

您還可以使用stop_words='english'自動刪除英語停用詞(完整列表請參見sklearn.feature_extraction.text.ENGLISH_STOP_WORDS )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM