Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Ailearning
提交
29b06be4
A
Ailearning
项目概览
OpenDocCN
/
Ailearning
大约 1 年 前同步成功
通知
12
Star
36240
Fork
11272
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
Ailearning
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
29b06be4
编写于
12月 09, 2019
作者:
片刻小哥哥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改完,加载外界词向量部分
上级
be4eb93a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
27 addition
and
18 deletion
+27
-18
src/py3.x/tensorflow2.x/text_Emotion.py
src/py3.x/tensorflow2.x/text_Emotion.py
+27
-18
未找到文件。
src/py3.x/tensorflow2.x/text_Emotion.py
浏览文件 @
29b06be4
# *-* coding:utf-8 *-*
# https://blog.csdn.net/u012052268/article/details/90238282
# 词向量:
# https://blog.csdn.net/xiezj007/article/details/85073890
# https://www.cnblogs.com/Darwin2000/p/5786984.html
# https://ai.tencent.com/ailab/nlp/embedding.html
import
re
import
os
import
keras
...
...
@@ -15,27 +19,28 @@ from config import Config
## 训练自己的词向量,并保存。
def
trainWord2Vec
(
filePath
):
sentences
=
gensim
.
models
.
word2vec
.
LineSentence
(
filePath
)
# 读取分词后的 文本
def
trainWord2Vec
(
infile
,
outfile
):
sentences
=
gensim
.
models
.
word2vec
.
LineSentence
(
infile
)
# 读取分词后的 文本
model
=
gensim
.
models
.
Word2Vec
(
sentences
,
size
=
100
,
window
=
5
,
min_count
=
1
,
workers
=
4
)
# 训练模型
model
.
save
(
'./CarComment_vord2vec_100'
)
model
.
save
(
outfile
)
def
testMyWord2Vec
(
):
def
loadMyWord2Vec
(
outfile
):
# 导入 预训练的词向量
myPath
=
'./CarComment_vord2vec_100'
# 读取词向量
Word2VecModel
=
gensim
.
models
.
Word2Vec
.
load
(
myPath
)
print
(
'空间的词向量(100 维):'
,
Word2VecModel
.
wv
[
'空间'
])
print
(
'打印与空间最相近的5个词语:'
,
Word2VecModel
.
wv
.
most_similar
(
'空间'
,
topn
=
5
))
Word2VecModel
=
gensim
.
models
.
Word2Vec
.
load
(
outfile
)
return
Word2VecModel
def
load_embeding
():
# 训练词向量
trainWord2Vec
(
'./CarCommentAll_cut.csv'
)
# 训练词向量(用空格隔开的文本)
infile
=
"./CarCommentAll_cut.csv"
outfile
=
"/opt/data/开源词向量/gensim_word2vec_60/Word60.model"
# trainWord2Vec(infile, outfile)
# 加载词向量
Word2VecModel
=
testMyWord2Vec
()
Word2VecModel
=
loadMyWord2Vec
(
outfile
)
print
(
'空间的词向量(60 维):'
,
Word2VecModel
.
wv
[
'空间'
].
shape
,
Word2VecModel
.
wv
[
'空间'
])
print
(
'打印与空间最相近的5个词语:'
,
Word2VecModel
.
wv
.
most_similar
(
'空间'
,
topn
=
5
))
## 2 构造包含所有词语的 list,以及初始化 “词语-序号”字典 和 “词向量”矩阵
vocab_list
=
[
word
for
word
,
Vocab
in
Word2VecModel
.
wv
.
vocab
.
items
()]
# 存储 所有的 词语
...
...
@@ -44,7 +49,7 @@ def load_embeding():
word_vector
=
{}
# 初始化`[word : vector]`字典
# 初始化存储所有向量的大矩阵,留意其中多一位(首行),词向量全为 0,用于 padding补零。
# 行数 为 所有单词数+1 比如 10000+1 ; 列数为 词向量“维度”比如
10
0。
# 行数 为 所有单词数+1 比如 10000+1 ; 列数为 词向量“维度”比如
6
0。
embeddings_matrix
=
np
.
zeros
((
len
(
vocab_list
)
+
1
,
Word2VecModel
.
vector_size
))
## 3 填充 上述 的字典 和 大矩阵
...
...
@@ -54,6 +59,7 @@ def load_embeding():
word_index
[
word
]
=
i
+
1
# 词语:序号
word_vector
[
word
]
=
Word2VecModel
.
wv
[
word
]
# 词语:词向量
embeddings_matrix
[
i
+
1
]
=
Word2VecModel
.
wv
[
word
]
# 词向量矩阵
print
(
"加载词向量结束.."
)
return
embeddings_matrix
...
...
@@ -123,9 +129,12 @@ class EmotionModel(object):
if
__name__
==
'__main__'
:
model
=
EmotionModel
(
Config
)
while
1
:
text
=
input
(
"text:"
)
res
=
model
.
predict
(
text
)
print
(
res
)
# 测试加载外界word2vec词向量
load_embeding
()
# model = EmotionModel(Config)
# while 1:
# text = input("text:")
# res = model.predict(text)
# print(res)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录