本文使用哈工大做文本预处理; 两层隐层神经网络;
后注:不是标准的ann,做了去停用词和词性筛选,没有端到端。
# -*- coding: utf-8 -*-
# @bref :使用tensorflow做中文情感分析
import numpy as np
import tensorflow as tf
import random
from sklearn.feature_extraction.text import CountVectorizer
import os
import traceback
real_dir_path = os.path.split(os.path.realpath(__file__))[0]
pos_file = os.path.join(real_dir_path, 'data/pos_bak.txt')
neg_file = os.path.join(real_dir_path, 'data/neg_bak.txt')
#使用哈工大分词和词性标注
from pyltp import Segmentor, Postagger
seg = Segmentor()
seg.load('/root/git/ltp_data/cws.model')
poser = Postagger()
poser.load('/root/git/ltp_data/pos.model')
real_dir_path = os.path.split(os.path.realpath(__file__))[0] #文件所在路径
stop_words_file = os.path.join(real_dir_path, '../util/stopwords.txt')
#定义允许的词性
allow_pos_ltp = ('a', 'i', 'j', 'n', 'nh', 'ni', 'nl', 'ns', 'nt', 'nz', 'v', 'ws')
#分词、去除停用词、词性筛选
def cut_stopword_pos(s):
words = seg.segment(''.join(s.split()))
poses = poser.postag(words)
stopwords = {}.fromkeys([line.rstrip() for line in open(stop_words_file)])
sentence = []
for i, pos in enumerate(poses):