gpt4 book ai didi

python - 使用 NLTK 的独特词频

转载 作者:行者123 更新时间:2023-12-01 09:31:25 25 4
gpt4 key购买 nike

使用 NLTK 获取以下内容的唯一词频的代码。

序列句子
1 让我们努力做好人。
2 做好人是没有意义的。
3 好的永远是好的。

输出:
{'good':3、'let':1、'try':1、'to':1、'be':1、'being':1、'doesn':1、't': 1, 'make':1, 'sense':1, 'is':1, 'always':1, '.':3, ''':2, 's':1}

最佳答案

如果您对使用 nltk 非常挑剔,请引用以下代码片段

import nltk

text1 = '''Seq Sentence
1 Let's try to be Good.
2 Being good doesn't make sense.
3 Good is always good.'''

words = nltk.tokenize.word_tokenize(text1)
fdist1 = nltk.FreqDist(words)

filtered_word_freq = dict((word, freq) for word, freq in fdist1.items() if not word.isdigit())

print(filtered_word_freq)

希望有帮助。

引用了一些部分:

How to check if string input is a number?

Dropping specific words out of an NLTK distribution beyond stopwords

关于python - 使用 NLTK 的独特词频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49939450/

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