资源下载地址:https://download.csdn.net/download/sheziqiong/86871277
资源下载地址:https://download.csdn.net/download/sheziqiong/86871277
步骤
读取评论文件,按照正向和负向两个分类把评论分别写入两个新的文件(正向的评论和负向的评论)。读取前4000条写入正向的评论文件,后8000条写入负向的评论文件。
def separate_csv(file):
""" 将评论按照正向或者负向分别写入两个文件 """
a = 1
with open(file, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
if a < 4002:
a += 1
continue
with open("comments_0.csv", "a", encoding="utf-8", newline="") as f2:
wirter = csv.writer(f2)
wirter.writerow(row)
a += 1
a = 1
with open(file, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
if a == 1:
a += 1
continue
with open("comments_1.csv", "a", encoding="utf-8", newline="") as f2:
wirter = csv.writer(f2)
wirter.writerow(row)
a += 1
if a > 4001:
break
然后我们用jieba
分词工具分别获取正向和负向评价中出现频率最高的50个词,分别写入对应的文件。
def jieba_get_high_frequency_words():
""" 用jieba分词分别提取出正向和负向的高频词 """
col_name = [
'ID',
'comment'
]
csvpd = pd.read_csv("comments_1.csv", names=col_name)['comment']
data = ''.join(csvpd)
with open("high_frequency_word_1.csv", "w", encoding="utf-8", newline="") as f:
csvwriter = csv.writer(f)
i = 1
for keyword, weight in textrank(data, topK=50, withWeight=True):
csvwriter.writerow([i, keyword])
i += 1
csvpd = pd.read_csv("comments_0.csv", names=col_name)['comment']
data = ''.join(csvpd)
with open("high_frequency_word_0.csv", "w", encoding="utf-8", newline="") as f:
csvwriter = csv.writer(f)
i = 1
for keyword, weight in textrank(data, topK=50, withWeight=True):
csvwriter.writerow([i, keyword])
i += 1
得到的高频词展示如下:
正向
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QicixisZ-1652069698838)(res/正向高频.png)]
负向
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Mrfz4vuo-1652069698840)(res/负向高频.png)]
接下来就是发挥人类的主观能动性的时候了,我们从里面分别选取5-10个有代表性的情感种子词按照下面情感分析库的要求写入我们的种子词文件。
def make_seed_words():
""" 从提取出来的高频词中选取一些种子词,写入txt文件,用去情感分析 """
with open('seed_words.txt', 'w', encoding='utf-8') as f:
f.write('好吃' + '\t' + 'pos' + '\n')
f.write('速度快' + '\t' + 'pos' + '\n')
f.write('好评' + '\t' + 'pos' + '\n')
f.write('喜欢' + '\t' + 'pos' + '\n')
f.write('实惠' + '\t' + 'pos' + '\n')
f.write('回购' + '\t' + 'pos' + '\n')
f.write('准时' + '\t' + 'pos' + '\n')
f.write('满意' + '\t' + 'pos' + '\n')
f.write('没有' + '\t' + 'neg' + '\n')
f.write('不会' + '\t' + 'neg' + '\n')
f.write('取消' + '\t' + 'neg' + '\n')
f.write('垃圾' + '\t' + 'neg' + '\n')
f.write('送错' + '\t' + 'neg' + '\n')
f.write('客服' + '\t' + 'neg' + '\n')
因为题目并没有要求我们正向负向的评论分开分析,所以我们先把价评论文件里的评论都提取出来,逐行写入纯评论的txt文件。
def get_thesaurus(file):
""" 将所有的评论都写入一个txt文件,用于后面的情感分析 """
csvpd = pd.read_csv(file)['review']
data = '\n'.join(csvpd)
f = open('comments.txt', 'w', encoding='utf-8')
f.write(data)
f.close()
最后就是最重要的情感分析。这里我找了github
上一个大佬写好的库。具体参考链接。按照他说的操作,配置好文件就行了。
sopmier = ChineseSoPmi(inputtext_file='comments.txt',
seedword_txtfile='seed_words.txt',
pos_candi_txt_file='pos_candi.txt',
neg_candi_txtfile='neg_candi.txt')
部分代码展示:
def get_thesaurus(file):
“”" 将所有的评论都写入一个txt文件,用于后面的情感分析 “”"
csvpd = pd.read_csv(file)[‘review’]
data = ‘\n’.join(csvpd)
f = open(‘comments.txt’, ‘w’, encoding=‘utf-8’)
f.write(data)
f.close()
def make_seed_words():
“”" 从提取出来的高频词中选取一些种子词,写入txt文件,用去情感分析 “”"
with open(‘seed_words.txt’, ‘w’, encoding=‘utf-8’) as f:
f.write(‘好吃’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘速度快’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘好评’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘喜欢’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘实惠’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘回购’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘准时’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘满意’ + ‘\t’ + ‘pos’ + ‘\n’)
f.write(‘没有’ + ‘\t’ + ‘neg’ + ‘\n’)
f.write(‘不会’ + ‘\t’ + ‘neg’ + ‘\n’)
f.write(‘取消’ + ‘\t’ + ‘neg’ + ‘\n’)
f.write(‘垃圾’ + ‘\t’ + ‘neg’ + ‘\n’)
f.write(‘送错’ + ‘\t’ + ‘neg’ + ‘\n’)
f.write(‘客服’ + ‘\t’ + ‘neg’ + ‘\n’)
资源下载地址:https://download.csdn.net/download/sheziqiong/86871277
资源下载地址:https://download.csdn.net/download/sheziqiong/86871277