如何使用 spacy 找到最常用的单词?

新手上路,请多包涵

我将 spacy 与 python 一起使用,它可以很好地标记每个单词,但我想知道是否有可能在字符串中找到最常见的单词。还有最常见的名词、动词、副词等是否可以得到?

包含一个 count_by 函数,但我似乎无法让它以任何有意义的方式运行。

原文由 Harry Loyd 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 377
1 个回答

我最近不得不计算文本文件中所有标记的频率。您可以使用 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)

原文由 Paras Dahal 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题