我正在使用 spacy 和 python,它可以很好地标记每个单词,但我想知道是否有可能找到字符串中最常见的单词。还有可能得到最常见的名词、动词、副词等吗?
包含一个 count_by 函数,但我似乎无法让它以任何有意义的方式运行。
我最近不得不计算文本文件中所有标记的频率。您可以使用 pos_ 属性过滤掉单词以获得您喜欢的 POS 令牌。这是一个简单的例子:
import spacy
from collections import Counter
nlp = spacy.load('en')
doc = nlp(u'Your text here')
# all tokens that arent stop words or punctuations
words = [token.text
for token in doc
if not token.is_stop and not token.is_punct]
# noun tokens that arent stop words or punctuations
nouns = [token.text
for token in doc
if (not token.is_stop and
not token.is_punct and
token.pos_ == "NOUN")]
# five most common tokens
word_freq = Counter(words)
common_words = word_freq.most_common(5)
# five most common noun tokens
noun_freq = Counter(nouns)
common_nouns = noun_freq.most_common(5)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)