返回的是字典类型,这是nltk 处理情感分类的一个标准情势。
2. 把双词搭配(bigrams)作为特点
- mport nltk
- from nltk.collocations import BigramCollocationFinder
- from nltk.metrics import BigramAssocMeasures
- def bigram(words, score_fn=BigramAssocMeasures.chi_sq, n=1000):
- bigram_finder = BigramCollocationFinder.from_words(words) #把文本变成双词搭配的情势
- bigrams = bigram_finder.nbest(score_fn, n) #应用了卡方统计的办法,选择排名前1000的双词
- return bag_of_words(bigrams)
除了可以应用卡方统计来选择信息量丰富的双词搭配,还可以应用其它的办法,比如互信息(PMI)。而排名前1000也执偾人工选择的阈值,可以随便选择其它值,可经由测试一步步找到最优值。
3. 把所有词和双词搭配一路作为特点
- def bigram_words(words, score_fn=BigramAssocMeasures.chi_sq, n=1000):
- bigram_finder = BigramCollocationFinder.from_words(words)
- bigrams = bigram_finder.nbest(score_fn, n)
- return bag_of_words(words + bigrams) #所有词和(信息量大年夜的)双词搭配一路作为特点
二、特点选择办法
有了提取特点的办法后,我们就可以提取特点来进行分类进修了。但一般来说,太多的特点会降低分类的精确度,所以须要应用必定的办法,来“选择”出信息量最丰富的特点,再应用这些特点来分类。
特点选择遵守如下步调:
- 计算出全部语料琅绫擎每个词典信息量
- 根据信息量进行倒序排序,选择排名靠前的信息量的词
- 把这些词作为特点
1. 计算出全部语料琅绫擎每个词典信息量
1.1 计算全部语料琅绫擎每个词典信息量
- from nltk.probability import FreqDist, ConditionalFreqDist
- def create_word_scores(): posWords = pickle.load(open('D:/code/sentiment_test/pos_review.pkl','r')) ..... return word_scores #包含了每个词和这个词典信息量
1.2 计算全部语料琅绫擎每个词和双词搭配的信息量
- def create_word_bigram_scores():
- posdata = pickle.load(open('D:/code/sentiment_test/pos_review.pkl','r')) negdata = pickle.load(open('D:/code/sentiment_test/neg_review.pkl','r')) ..... return word_scores
2. 根据信息量进行倒序排序,选择排名靠前的信息量的词
- def find_best_words(word_scores, number):
- best_vals = sorted(word_scores.iteritems(), key=lambda (w, s): s, reverse=True)[:number] #把词按信息量倒序排序。number是特点的维度,是可以赓续调剂直至最优的
推荐阅读
进修怎么在保护 root 暗码的安然性的同时,为可托用户付与所治理的收集功能和特定办事的权限。我比来写了一个简短的 Bash 法度榜样来将 MP3 文件大年夜一台收集主机的 USB 盘中拷贝到另一台收集主机上去。拷贝出来的>>>详细阅读
本文标题:使用python+机器学习方法进行情感分析(详细步骤)
地址:http://www.17bianji.com/lsqh/40229.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示