gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:15:59 24 4
gpt4 key购买 nike

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

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

最佳答案

我最近不得不计算文本文件中所有标记的频率。您可以使用 pos_ 属性过滤掉单词以获得您喜欢的 POS token 。这是一个简单的例子:

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)

关于python - 如何使用 spacy 找到最常用的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37253326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com