如何在字典中搜索nltk词干?

2024-09-26 04:56:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在检查字典中是否存在词干为词干的单词。这是我正在做的一些情绪分析工作。我要回来的只是这个错误:

Traceback (most recent call last):
File "sentiment.py", line 369, in <module>
score += int(senti_word_dict.get(get_stem(word)))
TypeError: int() argument must be a string or a number, not 'NoneType'

下面是通过NLTK查找词干词的方法的代码:

^{pr2}$

下面是根据字典检查该单词的代码:

for comment in all_comments:
    score = 0
    tokens = tokenize(comment)
    for word in tokens:
      if word in senti_word_dict:
        score += int(senti_word_dict.get(get_stem(word)))
    print(str(score)+" "+comment)
    print('\n')

现在我只是得到分数。有没有一种方法可以让我把这个词干作为一个字符串来传递,看看字典里的分数是多少?如果有什么我做错了或可以做得更好的,让我知道!谢谢!在


Tags: 方法代码inforget字典comment单词
1条回答
网友
1楼 · 发布于 2024-09-26 04:56:46

检查word是否在senti_word_dict中。也许是的。但你却阻止了它(它变成了另一个词!)并尝试使用senti_word_dict.get从字典中检索词干。如果词干不在字典里(为什么要在字典里?),get()返回None。因此,错误。解决方法:先把这个词词干,然后再查。在

相关问题 更多 >