Python情感分析

2023-12-13 03:31:37
# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: JHC000abc@gmail.com
@file: test.py
@time: 2023/12/6 13:45 
@desc: 

"""
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB


class SklearnEmotion(object):
    """
    基于 Sklearn 的情感分析器
    """
    def __init__(self,file_pos,file_neg):
        self.vectorizer = None
        self.train_vectors = None
        self.clf = None
        self.file_pos = file_pos
        self.file_neg = file_neg
        self.process()

    def read_train_data(self):
        """
        读取训练数据,拼接 x_train y_train
        :param file_pos:
        :param file_neg:
        :return:
        """
        pos_data = []
        neg_data = []
        with open(self.file_pos, "r", encoding='utf-8')as pos, \
                open(self.file_neg, "r", encoding='utf-8')as neg:
            for i, j in zip(pos, neg):
                # print(i,j)
                pos_data.append(i.strip())
                neg_data.append(j.strip())

        x_train = pos_data + neg_data
        pos_lis = ["积极" for _ in pos_data]
        neg_lis = ["消极" for _ in neg_data]
        y_train = pos_lis + neg_lis

        return x_train,y_train

    def train_data(self,x_train,y_train):
        """
        训练模型
        :param x_train:
        :param y_train:
        :return:
        """
        self.vectorizer = CountVectorizer()
        self.train_vectors = self.vectorizer.fit_transform(x_train)

        # 训练朴素贝叶斯分类器
        self.clf = MultinomialNB()
        self.clf.fit(self.train_vectors, y_train)

    def process(self):
        """
        读取数据 训练模型
        :return:
        """
        x_train, y_train = self.read_train_data()
        self.train_data(x_train, y_train)
        
    def get_result(self,sentens_list):
        """
        获取结果
        :param sentens_list:
        :return:
        """
        test_vectors = self.vectorizer.transform(sentens_list)
        predictions = self.clf.predict(test_vectors)
        for i, prediction in enumerate(predictions):
            yield {sentens_list[i]: prediction}




if __name__ == '__main__':
    file_pos = R"C:\Users\v_jiaohaicheng\Downloads\pos_all_dict.txt"
    file_neg = R"C:\Users\v_jiaohaicheng\Downloads\neg_all_dict.txt"
    se = SklearnEmotion(file_pos,file_neg)
    for i in se.get_result(["我喜欢你","脑残","大傻叉"]):
        print(i)

file_pos = R"C:\Users\v_jiaohaicheng\Downloads\pos_all_dict.txt"
file_neg = R"C:\Users\v_jiaohaicheng\Downloads\neg_all_dict.txt"

文章来源:https://blog.csdn.net/CXY00000/article/details/134924708
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。