中文手机评论情感分类系列(一)
生活随笔
收集整理的這篇文章主要介紹了
中文手机评论情感分类系列(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
因為寫論文需要,準備做手機評論的情感分析,依據現有的工具可以很容易中文評論進行去重復評論,分詞,去停用詞,向量化,然后用sklearn中的一款分類器來對數據進行情感分類。但是,由于本人打算依據手機不同的屬性對評論文本進行情感分析,如“電池”,‘相機’,‘處理器’等屬性的評論。很難過的是,這些針對手機不同屬性的評論從互聯網上爬取不到,只能找到綜合的評論,即不根據手機特征分類的評論。天貓盒京東上的都只是綜合評論。所以這里針對這些文本要進行多分類,即按屬性分類。我主要把手機分為7個比較重要的特征。分別是,‘相機’,‘處理器’,‘價格’,‘售后服務’,‘續航’,‘外觀’和‘性能’。這里涉及到了對評論進行多分類到了,我才用基于屬性詞典的分類方式,目前市面上沒有關于手機各屬性的詞典,所以涉及到自己構建屬性詞典。本文分三個部分:
1.詞典構建
2.文本基于屬性詞典的分類
3.評論文本的情感分析
本文介紹第一部分,屬性詞典的構建。直接放代碼。
'''字典構建''' import re,os,pyltp import pandas as pd import gensim import time from gensim.models.word2vec import PathLineSentences import numpy as np from sklearn.cluster import KMeansclass BulidDict():def __init__(self):self.a=1'''計算相似度大于0.8的詞放入集合'''def similarity_word(self,list,p=0.8):model = gensim.models.Word2Vec.load('D:/machinelearning data/word2vec/phone_comment_vec_mini_count_5')word_list = []for w in list:listA=model.most_similar(w,topn=2000)print(w,listA)for i,j in listA:if j > p:print(i)word_list.append(i)#word_list+=word_listreturn set(word_list)'''將詞存儲起來'''def save(self,list,save_path):f = open(save_path, 'w', encoding='utf-8')for i in list:f.write(i + '\n')def openFile(self,path):with open(path,'r',encoding='utf-8') as f:for word in f.readlines():yield word.strip()'''構建屬性字典集合'''def build_dict(self,loadPath,savePath,p):wordSet = list(self.openFile(loadPath))simWord=self.similarity_word(wordSet,p)self.save(simWord,savePath)'''去除字典中的重復詞,并保存'''def del_repetition(self,file_path,save_path):file=self.openFile(file_path)self.save(set(list(file)),save_path)if __name__=='__main__' :path1 = 'D:/machinelearning data/buildDict/camera.txt'path2 = 'D:/machinelearning data/buildDict/processor.txt'path3 = 'D:/machinelearning data/buildDict/pricemin.txt'path4 = 'D:/machinelearning data/buildDict/performance.txt'path5= 'D:/machinelearning data/buildDict/serve.txt'path6 = 'D:/machinelearning data/buildDict/appearance.txt'path7 = 'D:/machinelearning data/buildDict/endurance.txt'demo=BulidDict()# set1=list(demo.openFile(path1))# list1=demo.similarity_word(set1,p=0.92)# demo.save(list1,'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\相機0.9.txt')savePath1='D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\相機0.85.txt'savePath2 = 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\處理器0.89.txt'savePath3= 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\價格.txt'savePath4 = 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\性能0.93.txt'savePath5 = 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\售后0.85.txt'savePath6 = 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\外觀0.9.txt'savePath7 = 'D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\續航電池0.7.txt'#demo.build_dict(path2,savePath2,0.89)#0.89最優#demo.build_dict(path3, savePath3, 0.6)#demo.build_dict(path4, savePath4, 0.93)#0.89+0.9+0.93最優#demo.build_dict(path5, savePath5, 0.85)#0.85最優#demo.build_dict(path6, savePath6, 0.9)#0.9最優#demo.build_dict(path1, savePath1, 0.85)#0.85和基礎詞典已經最優#demo.build_dict(path7, savePath7, 0.7)#電池0.7其他0.85最優'''字典的去重復工作和統一存儲工作'''abs_path='D:\\論文文件\\閱讀論文\\寫論文準備\\字典構建\\手機屬性詞典\\dictionary_0_2\\'save_name=['相機.txt','處理器.txt','價格.txt','性能.txt','售后.txt','外觀.txt','續航.txt']open_name=['相機0.85.txt','處理器0.89(完美).txt','價格0.9+0.95+0.6(完美).txt','性能0.89+0.9+0.93(完美).txt','售后0.85(最優).txt','外觀0.9(最優).txt','續航0.85+電池0.7(完美).txt']'''去除字典的重復詞'''for i in range(len(save_name)):#這個打開txt文件需要open_File()的編碼方式改為'utf-8'demo.del_repetition(abs_path+open_name[i],abs_path+save_name[i])
?
轉載于:https://www.cnblogs.com/zz22--/p/9773924.html
總結
以上是生活随笔為你收集整理的中文手机评论情感分类系列(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript pop()函数弹出
- 下一篇: pandas,apply并行计算的一个d