gpt4 book ai didi

python - 打印包含和排除停用词的文本中 10 个最常出现的词

转载 作者:太空狗 更新时间:2023-10-29 19:35:53 26 4
gpt4 key购买 nike

我从 here 得到了问题随着我的改变。我有以下代码:

from nltk.corpus import stopwords
def content_text(text):
stopwords = nltk.corpus.stopwords.words('english')
content = [w for w in text if w.lower() in stopwords]
return content

如何打印文本中 1)包括 和 2)排除 停用词的 10 个最常出现的词?

最佳答案

nltk中有一个FreqDist函数

import nltk
allWords = nltk.tokenize.word_tokenize(text)
allWordDist = nltk.FreqDist(w.lower() for w in allWords)

stopwords = nltk.corpus.stopwords.words('english')
allWordExceptStopDist = nltk.FreqDist(w.lower() for w in allWords if w not in stopwords)

提取 10 个最常见的:

mostCommon= allWordDist.most_common(10).keys()

关于python - 打印包含和排除停用词的文本中 10 个最常出现的词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28392860/

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