栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

nltk NaiveBayesClassifier情绪分析培训

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号