码迷,mamicode.com
首页 > 其他好文 > 详细

Gensim的model使用word2vec 示例

时间:2020-08-13 22:13:28      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:word2vec   min   har   sof   row   mil   tail   utf-8   import   

# coding=utf-8
"""
 @ File: word2vec_gensim.py
 @Software: PyCharm
 @desc:
"""
from gensim.models import word2vec
import logging
logging.basicConfig(format=%(asctime)s: %(levelname)s: %(message)s, level=logging.INFO)
raw_sentences = [the quick brown fox jumps over ther lazy dogs, yoyoyo you go home now to sleep]

sentences = [s.split() for s in raw_sentences]
print(sentences)
# out: [[‘the‘, ‘quick‘, ‘brown‘, ‘fox‘, ‘jumps‘, ‘over‘, ‘ther‘, ‘lazy‘, ‘dogs‘], [‘yoyoyo‘, ‘you‘, ‘go‘, ‘home‘, ‘now‘, ‘to‘, ‘sleep‘]]

# 传参是文章分词后的列表,每篇文章一个元素
model = word2vec.Word2Vec(sentences, min_count=1)

model.wv.save(m2.mdl)
# 或者
model.save(m1.mdl)

# 加载使用模型
md = word2vec.Word2Vec.load(m1.mdl)
# 用于比较单个词语
print(md.similarity(dogs, you))
# out: -0.06432766
# wv是4.0新版本后的方法,代替model.n_similartity
# n_similarity用于比较文章
print(md.wv.n_similarity([fox,dogs], [dogs, fox]))
# out:1.0

# most_similar找到相似度最高的词
print(model.most_similar(‘dogs‘))
 

ref : https://blog.csdn.net/luoluonuoyasuolong/article/details/107810578

Gensim的model使用word2vec 示例

标签:word2vec   min   har   sof   row   mil   tail   utf-8   import   

原文地址:https://www.cnblogs.com/yoyowin/p/13495918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!