去年的京东评论项目
Posted laowangxieboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了去年的京东评论项目相关的知识,希望对你有一定的参考价值。
学习数据挖掘挺久了,要找工作啦,把之前的项目留下了,留下源代码
#数据的导入
import pandas as pd
inputfile = ‘../data/huizong.csv‘ #评论汇总文件
outputfile = ‘../data/meidi_jd.txt‘ #评论提取后保存路径
data = pd.read_csv(inputfile, encoding = ‘utf-8‘)
data = data[[u‘评论‘]][data[u‘品牌‘] == u‘美的‘]
data.to_csv(outputfile, index = False, header = False, encoding = ‘utf-8‘)
#数据去重
import pandas as pd
inputfile = ‘../data/meidi_jd.txt‘ #评论文件
outputfile = ‘../data/meidi_jd_process_1.txt‘ #评论处理后保存路径
data = pd.read_csv(inputfile, encoding = ‘utf-8‘, header = None)
l1 = len(data)
data = pd.DataFrame(data[0].unique())
l2 = len(data)
data.to_csv(outputfile, index = False, header = False, encoding = ‘utf-8‘)
print(u‘删除了%s条评论。‘ %(l1 - l2))
#删除前缀评分
#参数初始化
inputfile1 = ‘../data/meidi_jd_process_end_Negative emotional consequences.txt‘
inputfile2 = ‘../data/meidi_jd_process_end_Positive emotional outcomes.txt‘
outputfile1 = ‘../data/meidi_jd_neg.txt‘
outputfile2 = ‘../data/meidi_jd_pos.txt‘
data1 = pd.read_csv(inputfile1, encoding = ‘utf-8‘, header = None) #读入数据
data2 = pd.read_csv(inputfile2, encoding = ‘utf-8‘, header = None)
data1 = pd.DataFrame(data1[0].str.replace(‘.*?\d+?\\t ‘, ‘‘)) #用正则表达式修改数据
data2 = pd.DataFrame(data2[0].str.replace(‘.*?\d+?\\t ‘, ‘‘))
data1.to_csv(outputfile1, index = False, header = False, encoding = ‘utf-8‘) #保存结果
data2.to_csv(outputfile2, index = False, header = False, encoding = ‘utf-8‘)
#分词处理
import pandas as pd
import jieba #导入结巴分词,需要自行下载安装
#参数初始化
inputfile1 = ‘../data/meidi_jd_neg.txt‘
inputfile2 = ‘../data/meidi_jd_pos.txt‘
outputfile1 = ‘../data/meidi_jd_neg_cut.txt‘
outputfile2 = ‘../data/meidi_jd_pos_cut.txt‘
data1 = pd.read_csv(inputfile1, encoding = ‘utf-8‘, header = None) #读入数据
data2 = pd.read_csv(inputfile2, encoding = ‘utf-8‘, header = None)
mycut = lambda s: ‘ ‘.join(jieba.cut(s)) #自定义简单分词函数
data1 = data1[0].apply(mycut) #通过“广播”形式分词,加快速度。
data2 = data2[0].apply(mycut)
data1.to_csv(outputfile1, index = False, header = False, encoding = ‘utf-8‘) #保存结果
data2.to_csv(outputfile2, index = False, header = False, encoding = ‘utf-8‘)
#LDA主题模型输出
import pandas as pd
#参数初始化
negfile = ‘../data/meidi_jd_neg_cut.txt‘
posfile = ‘../data/meidi_jd_pos_cut.txt‘
stoplist = ‘../data/stoplist.txt‘
neg = pd.read_csv(negfile, encoding = ‘utf-8‘, header = None) #读入数据
pos = pd.read_csv(posfile, encoding = ‘utf-8‘, header = None)
stop = pd.read_csv(stoplist, encoding = ‘utf-8‘, header = None, sep = ‘tipdm‘,engine=‘python‘)
#sep设置分割词,由于csv默认以半角逗号为分割词,而该词恰好在停用词表中,因此会导致读取出错
#所以解决办法是手动设置一个不存在的分割词,如tipdm。
stop = [‘ ‘, ‘‘] + list(stop[0]) #Pandas自动过滤了空格符,这里手动添加
neg[1] = neg[0].apply(lambda s: s.split(‘ ‘)) #定义一个分割函数,然后用apply广播
neg[2] = neg[1].apply(lambda x: [i for i in x if i not in stop]) #逐词判断是否停用词,思路同上
pos[1] = pos[0].apply(lambda s: s.split(‘ ‘))
pos[2] = pos[1].apply(lambda x: [i for i in x if i not in stop])
import warnings
warnings.filterwarnings(action=‘ignore‘, category=UserWarning, module=‘gensim‘)
from gensim import corpora, models
#负面主题分析
neg_dict = corpora.Dictionary(neg[2]) #建立词典
neg_corpus = [neg_dict.doc2bow(i) for i in neg[2]] #建立语料库
neg_lda = models.LdaModel(neg_corpus, num_topics = 3, id2word = neg_dict) #LDA模型训练
for i in range(3):
# neg_lda.print_topic(i) #输出每个主题
print(neg_lda.print_topic(i))
#正面主题分析
pos_dict = corpora.Dictionary(pos[2])
pos_corpus = [pos_dict.doc2bow(i) for i in pos[2]]
pos_lda = models.LdaModel(pos_corpus, num_topics = 3, id2word = pos_dict)
for i in range(3):
# pos_lda.print_topic(i) #输出每个主题
print(pos_lda.print_topic(i))
以上是关于去年的京东评论项目的主要内容,如果未能解决你的问题,请参考以下文章
Python爬虫编程思想(92):项目实战:抓取京东图书评价
Python爬虫编程思想(92):项目实战:抓取京东图书评价