Skip to main content
 首页 » 编程设计

python之如何在计算文本中单词准确度的频率时忽略某些单词

2025年05月04日1352php

在计算文本中单词准确度的频率时,如何忽略诸如“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 

答案将是The。但我想将 distance 作为最常用的词。

请您参考如下方法:

最好避免像这样开始计数条目。

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