nltk中作为特性的列表顺序:Python

2024-04-25 19:42:31 发布

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

如何使用列表的顺序作为python中nltk中naivebayes分类器的特性。我得到这个错误:

Traceback (most recent call last):
  File "/Users/noahchalifour/Desktop/paper-writing-tool/sent_analysis.py", line 57, in <module>
    classifier = NaiveBayesClassifier.train(train_data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/classify/naivebayes.py", line 198, in train
    feature_freqdist[label, fname][fval] += 1
TypeError: unhashable type: 'list'

代码如下:

import nltk

data = [("Hello how are you", 1),
        ("How is your day going", 1),
        ("This is very cool", 0),
        ("Machine learning is awesome", 0)]

def get_features(text):
    return {'order': text.split(' ')}

if __name__ == '__main__':
    train_data = [(get_features(text), cls) for text, cls in data]
    classifier = nltk.NaiveBayesClassifier.train(train_data)

我希望能够根据单词的顺序对文本进行分类。例如:

"Hello, how are you today?" --> ["Hello", "how", "are", "you", "today", "?"] --> 1  
"This is awesome!" --> ["This", "is", "awesome", "!"] --> 0

Tags: textinyouhellodata顺序istrain

热门问题