ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自制情感种子词分析数据集筛选正向和负向情感词【社会计算大作业】

2021-11-21 23:00:49  阅读:319  来源: 互联网

标签:txt neg 自制 pos 负向 write 情感 file csv


说明

需要相关文件的,github 自取。如果能顺手点个 star 我将感激不尽。

步骤

读取评论文件,按照正向和负向两个分类把评论分别写入两个新的文件(正向的评论和负向的评论)。读取前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

得到的高频词展示如下:
正向
image-20211029164215251
负向
image-20211029164234008

接下来就是发挥人类的主观能动性的时候了,我们从里面分别选取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')

结果展示:

这是正向情感的词语:

image-20211029165637002

这是负向情感的词语:

image-20211029165723860

完整代码展示:

import pandas as pd
from jieba.analyse import *
import jieba
import csv
from wordexpansion import ChineseSoPmi

file = "review.csv"


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


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


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')


sopmier = ChineseSoPmi(inputtext_file='comments.txt',
                       seedword_txtfile='seed_words.txt',
                       pos_candi_txt_file='pos_candi.txt',
                       neg_candi_txtfile='neg_candi.txt')


if __name__ == '__main__':
    separate_csv(file)
    jieba_get_high_frequency_words()
    get_thesaurus(file)
    make_seed_words()
    sopmier.sopmi()

标签:txt,neg,自制,pos,负向,write,情感,file,csv
来源: https://blog.csdn.net/m0_52775920/article/details/121462237

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有