Python 如何在词典中搜索nltk词干?
我在检查字典中是否有词干词时遇到了一个问题。这是我正在做的一些情绪分析工作。我得到的只是这个错误:Python 如何在词典中搜索nltk词干?,python,dictionary,nltk,stem,Python,Dictionary,Nltk,Stem,我在检查字典中是否有词干词时遇到了一个问题。这是我正在做的一些情绪分析工作。我得到的只是这个错误: 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 'NoneT
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'
以下是根据字典检查该单词的代码:
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')
现在我只是得到了分数。有没有一种方法可以让我把这个词干单词作为字符串传递,看看字典里的分数是多少?如果有什么我做错了或可以做得更好,请告诉我!谢谢 检查
word
是否在senti\u word\u dict
中。也许是这样。但是,您可以将其作为词干(它会变成一个不同的单词!),并尝试使用senti\u word\u dict.get
从字典中检索词干。如果词干不在字典中(为什么应该在字典中?),则get()
返回None
。因此,这是一个错误。解决方案:先将单词词干,然后再查找。stem\u单词
永远不会是None
,因为这是函数的名称。您必须使用带词干的单词。教训:不要使用类似的标识符。废话,谢谢。仍然抛出相同的错误。请更新您的代码,因为您现在基本上得到了不同的问题。如前所述。我仍然得到同样的错误。我会更新帖子的。啊,真漂亮!现在工作。我想我需要第二双眼睛才能看到我愚蠢的错误。谢谢你的意见!深夜编码对我来说可能不是最好的,哈哈。
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')