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

说明

需要相关文件的,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

得到的高频词展示如下:
正向
自制情感种子词分析数据集筛选正向和负向情感词【社会计算大作业】
负向
自制情感种子词分析数据集筛选正向和负向情感词【社会计算大作业】

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

结果展示:

这是正向情感的词语:

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

这是负向情感的词语:

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

完整代码展示:

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()
上一篇:查询导出AD域所有用户创建日期


下一篇:jmeter中的CSV 数据文件设置中线程共享模式