法律辞書 - 法律辞書評判分析の数値演算およびcsvファイルに出力します

準備

import jieba
import numpy as np
import pandas as pd
  1. 関数のリストへの感情の辞書の定義を読みます
#读取情感词典到列表中的函数
def read_dic(dic_type):
    with open(dic_type+'_extended.txt',encoding="utf-8") as f:
        positive_extended = f.readlines()
    temp=[]
    for row in positive_extended:
        temp.append(row.split()) 
    pos_dic = []
    for row in temp:
        for column in row:
            pos_dic.append(column)
    return pos_dic
  1. どの機能の多くの感情的な語彙とカウントして、文書内の単語の定義を決定します
#判断文档中的词语有多少在情感词典中,并计数
def count_words(file,dictionary):
    count = 0
    for word in file:
        if word in dictionary:
            count+=1
    return count
  1. 定義された関数は、感情的な単語が表示されますから、ドキュメントの数のリストを返し、この機能はcount_words上記()関数を使用しています
#返回文档从情感词出现的次数列表
#cutWords_list:此形参应该传入已经分好词并去除停用词的嵌套列表(见上),type_list应该传入不同的情感词列表
def count_list(cutWords_list,type_list):   #type_list形参传入不同的情感词列表
    negative_count=[]
    for each_file in cutWords_list:
        negative_count.append(count_words(each_file,type_list))
    return negative_count

主な機能

メイン関数を入力するための準備の関数定義完了:

with open('cutWords_list.txt',encoding="utf-8") as file:  #读入了已经分词并去停用词的文件
    cutWords_list = [k.split() for k in file.readlines()]

ここで私は行きませんし、言葉のない言葉休止運転、すでに言葉を読んでの直接の結果。

#1.首先生成六个词典。
positive_list=read_dic("positive")
negative_list=read_dic("negative")
modalstrong_list=read_dic("modalstrong")
modalweak_list=read_dic("modalweak")
uncertainty_list=read_dic("uncertainty")
litigious_list=read_dic("litigious")

#2.然后生成六个结果列表。
positive_count=count_list(cutWords_list,positive_list)
negative_count=count_list(cutWords_list,negative_list)
modalstrong_count=count_list(cutWords_list,modalstrong_list)
modalweak_count=count_list(cutWords_list,modalweak_list)
uncertainty_count=count_list(cutWords_list,uncertainty_list)
litigious_count=count_list(cutWords_list,litigious_list)

#3.最后将结果输出到csv文件中
dataframe = pd.DataFrame({'positive_count':positive_count,
                          'negative_count':negative_count,
                          'modalstrong_count':modalstrong_count,
                          'modalweak_count':modalweak_count,
                          'uncertainty_count':uncertainty_count,
                          'litigious_count':litigious_count
                         })
#将DataFrame存储为csv,index表示是否显示行名,default=True
dataframe.to_csv("emotion_count.csv",index=False,sep=',')       #写入文件并保存

その後、ファイルのemotion_count.csvを開くことができます。
次のようにスクリーンショットの例を示します。
ここに画像を挿入説明

公開された27元の記事 ウォンの賞賛1 ビュー1175

おすすめ

転載: blog.csdn.net/weixin_43919570/article/details/104316043