nltk NaiveBayesClassifier情绪分析培训

nltk NaiveBayesClassifier情绪分析培训,第1张

nltk NaiveBayesClassifier情绪分析培训

您需要更改数据结构。这是

train
目前的清单:

>>> train = [('I love this sandwich.', 'pos'),('This is an amazing place!', 'pos'),('I feel very good about these beers.', 'pos'),('This is my best work.', 'pos'),("What an awesome view", 'pos'),('I do not like this restaurant', 'neg'),('I am tired of this stuff.', 'neg'),("I can't deal with this", 'neg'),('He is my sworn enemy!', 'neg'),('My boss is horrible.', 'neg')]

但问题在于,每个元组的第一个元素应该是要素字典。因此,我将您的列表更改为分类器可以使用的数据结构:

>>> from nltk.tokenize import word_tokenize # or use some other tokenizer>>> all_words = set(word.lower() for passage in train for word in word_tokenize(passage[0]))>>> t = [({word: (word in word_tokenize(x[0])) for word in all_words}, x[1]) for x in train]

您的数据现在应如下所示:

>>> t[({'this': True, 'love': True, 'deal': False, 'tired': False, 'feel': False, 'is': False, 'am': False, 'an': False, 'sandwich': True, 'ca': False, 'best': False, '!': False, 'what': False, '.': True, 'amazing': False, 'horrible': False, 'sworn': False, 'awesome': False, 'do': False, 'good': False, 'very': False, 'boss': False, 'beers': False, 'not': False, 'with': False, 'he': False, 'enemy': False, 'about': False, 'like': False, 'restaurant': False, 'these': False, 'of': False, 'work': False, "n't": False, 'i': False, 'stuff': False, 'place': False, 'my': False, 'view': False}, 'pos'), . . .]

注意,每个元组的第一个元素现在是字典。现在您的数据已经到位,每个元组的第一个元素是字典,您可以像这样训练分类器:

>>> import nltk>>> classifier = nltk.NaiveBayesClassifier.train(t)>>> classifier.show_most_informative_features()Most Informative Features         this = True   neg : pos    =      2.3 : 1.0         this = False  pos : neg    =      1.8 : 1.0an = False  neg : pos    =      1.6 : 1.0 . = True   pos : neg    =      1.4 : 1.0 . = False  neg : pos    =      1.4 : 1.0      awesome = False  neg : pos    =      1.2 : 1.0of = False  pos : neg    =      1.2 : 1.0         feel = False  neg : pos    =      1.2 : 1.0        place = False  neg : pos    =      1.2 : 1.0     horrible = False  pos : neg    =      1.2 : 1.0

如果要使用分类器,可以这样进行。首先,从一个测试句子开始:

>>> test_sentence = "This is the best band I've ever heard!"

然后,您标记该句子并找出该句子与all_words共享的单词。这些构成了句子的特征。

>>> test_sent_features = {word: (word in word_tokenize(test_sentence.lower())) for word in all_words}

现在,您的功能将如下所示:

>>> test_sent_features{'love': False, 'deal': False, 'tired': False, 'feel': False, 'is': True, 'am': False, 'an': False, 'sandwich': False, 'ca': False, 'best': True, '!': True, 'what': False, 'i': True, '.': False, 'amazing': False, 'horrible': False, 'sworn': False, 'awesome': False, 'do': False, 'good': False, 'very': False, 'boss': False, 'beers': False, 'not': False, 'with': False, 'he': False, 'enemy': False, 'about': False, 'like': False, 'restaurant': False, 'this': True, 'of': False, 'work': False, "n't": False, 'these': False, 'stuff': False, 'place': False, 'my': False, 'view': False}

然后,您只需对这些功能进行分类:

>>> classifier.classify(test_sent_features)'pos' # note 'best' == True in the sentence features above

这个测试句子似乎是肯定的。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5648493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存