0

我在检查字典中是否存在词干词时遇到问题。这是我正在做的一些情绪分析工作。我得到的只是这里的错误:

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 查找词干的方法的代码:

def get_stem(word):
    st = SnowballStemmer("english")
    stemmed_word = st.stem(word)
    return '' if stemmed_word is None else stemmed_word   

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

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')

现在我只是得到分数。有没有办法可以将该词干作为字符串传递,以查看字典中的分数?如果我做错了什么或可以做得更好,请告诉我!谢谢!

4

1 回答 1

0

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

于 2017-03-05T06:09:44.780 回答