import os
import keras
import numpy as np
import keras.preprocessing.text as text
import re
import jieba
import random
def load_data():
xs = []
ys = []
with open('online_shopping_10_cats.csv', 'r', encoding='utf-8') as f:
line = f.readline() # escape first line"label review"
while line:
line = f.readline()
if not line:
break
contents = line.split(',')
# if contents[0]=="书籍":
# continue
label = int(contents[1])
review = contents[2]
if len(review) > 20:
continue
xs.append(review)
ys.append(label)
xs = np.array(xs)
ys = np.array(ys)
# 打乱数据集
indies = [i for i in range(len(xs))]
random.seed(666)
random.shuffle(indies)
xs = xs[indies]
ys = ys[indies]
m = len(xs)
cutpoint = int(m * 3 / 5)
x_train = xs[:cutpoint]
y_train = ys[:cutpoint]
x_test = xs[cutpoint:]
y_test = ys[cutpoint:]
print(x_train)
print(y_train)
print('总样本数量:%d' % (len(xs)))
print('训练集数量:%d' % (len(x_train)))
print('测试集数量:%d' % (len(x_test)))
return x_train, y_train, x_test, y_test
load_data()
def createWordIndex(x_train, x_test):
x_all = np.concatenate((x_train, x_test), axis=0)
# 建立词索引
tokenizer = text.Tokenizer()
# create word index
word_dic = {}
voca = []
for sentence in x_all:
# 去掉标点
sentence = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()]+", "", sentence)
# 结巴分词
cut = jieba.cut(sentence)
# cut_list = [ i for i in cut ]
for word in cut:
if not (word in word_dic):
word_dic[word] = 0
else:
word_dic[word] += 1
voca.append(word)
word_dic = sorted(word_dic.items(), key=lambda kv: kv[1], reverse=True)
voca = [v[0] for v in word_dic]
tokenizer.fit_on_texts(voca)
print("voca:" + str(len(voca)))
return len(voca), tokenizer.word_index
def word2Index(words, word_index):
vecs = []
for sentence in words:
# 去掉标点
sentence = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、~@#¥%……&*()]+", "", sentence)
# 结巴分词
cut = jieba.cut(sentence)
# cut_list = [ i for i in cut ]
index = []
for word in cut:
if word in word_index:
index.append(float(word_index[word]))
# if len(index)>25:
# index = index[0:25]
vecs.append(np.array(index))
return np.array(vecs)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
"深度学习实战-电商产品评论的情感分析" 看起来是一个深度学习实践项目的标题,主要目标是通过深度学习技术对电商产品评论进行情感分析。在这样的项目中,通常会使用自然语言处理(NLP)和深度学习模型来识别文本中的情感倾向,例如判断评论是积极的、消极的还是中性的。 项目可能包括以下主要步骤: 1. **数据收集:** 获取电商产品评论的数据集,可能涵盖多个产品类别和用户评论。 2. **数据预处理:** 对评论文本进行清理和预处理,包括分词、去除停用词、词干提取等,以便输入深度学习模型。 3. **标签处理:** 对评论进行标签化,即为每个评论分配情感标签(积极、消极、中性)。 4. **模型构建:** 使用深度学习模型,例如循环神经网络(RNN)、长短时记忆网络(LSTM)、或者更先进的模型如Transformer,以学习评论文本中的情感信息。 5. **模型训练:** 使用标注好的评论数据,训练深度学习模型以学习情感分析任务。 6. **评估模型:** 使用独立的测试集评估模型的性能,例如准确率、精确度、召回率等指标。
资源推荐
资源详情
资源评论
收起资源包目录
深度学习实战7-电商产品评论的情感分析.zip (3个子文件)
data_loader.py 3KB
Sentiment.py 2KB
online_shopping_10_cats.csv 10.78MB
共 3 条
- 1
资源评论
小夕Coding
- 粉丝: 5858
- 资源: 461
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功