Python中使用Word2Vector

我的环境是win10 + python 3.6 (64位)

参考步骤:
https://blog.csdn.net/u012052268/article/details/78643260#word2vec的python应用

出现的问题:
1.出现异常 ‘str’ object has no attribute ‘seek’ 发生在word2vec.py中。
源码如下:

 try:
 # Assume it is a file-like object and try treating it as such
      # Things that don't have seek will trigger an exception
      self.source.seek(0)
      for line in itertools.islice(self.source, self.limit):
          line = utils.to_unicode(line).split()
          i = 0
          while i < len(line):
              yield line[i: i + self.max_sentence_length]
              i += self.max_sentence_length
 except AttributeError:
      # If it didn't work like a file, use it as a string filename
      with utils.smart_open(self.source) as fin:
          for line in itertools.islice(fin, self.limit):
              line = utils.to_unicode(line).split()
              i = 0
              while i < len(line):
                  yield line[i: i + self.max_sentence_length]
                  i += self.max_sentence_length

找到原因;
参数source的类型问题,在init中发现关于参数source的解释:source : string or a file-like object
Path to the file on disk, or an already-open file object (must support seek(0)).
解决方案:
将传进来的文件路径修改为打开的文件

    #语料的路径
    path = 'E:\\Source\\pyCharm\\pyWord2Vec\\res\\wiki.zh.word.text'
    # 训练模型
    sentences = LineSentence(open(path,'r',encoding='utf-8')

2.编码异常
于是将上面打开文件的方式增加忽略错误参数

    # 训练模型
    sentences = LineSentence(open(path,'r',encoding='utf-8',errors='ignore'))

以上,然后并无其他异常,正在训练。
(有一个warning,‘ detected Windows; aliasing chunkize to chunkize_serial’),网上有解决方案:https://blog.csdn.net/u013236830/article/details/78278274

具体如下:

import warnings
warnings.filterwarnings(action='ignore',category=UserWarning,module='gensim')

实验结果:
1.生成3个文件:
在这里插入图片描述
2.返回与"中国“相似度最大的词集相似度

我国 0.6511900424957275
北京 0.6456124186515808
内地 0.6364805102348328
上海 0.5867422223091125
全国 0.5867372155189514
境外 0.5863537788391113
各省市 0.572288990020752
台湾 0.5721427202224731
中国政府 0.5720890164375305
外国 0.5642403364181519

3.返回“男人“与”女人”的相似度:

0.8946562

附:我的代码,其实是参开上述链接的代码
1.安装gensim ,命令为:

pip install gensim

2.完整代码

#忽略导入gensim包的警告信息
import warnings
warnings.filterwarnings(action='ignore',category=UserWarning,module='gensim')

# 加载包
from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence

TRAIN = 'train'
TEST = 'test'

# 第一次使用,采用训练模式 (训练挺快的,不到二十分钟吧,具体也没记录)
#ops = TRAIN 
#已经训练好了模型
ops = TEST

if ops == TRAIN:
    #语料的路径
    path = 'E:\\Source\\pyCharm\\pyWord2Vec\\res\\wiki.zh.word.text'
    # 训练模型
    sentences = LineSentence(open(path,'r',encoding='utf-8',errors='ignore'))
    # size:词向量的维度
    # window:上下文环境的窗口大小
    # min_count:忽略出现次数低于min_count的词
    model = Word2Vec(sentences, size=128, window=5, min_count=5, workers=4)
    # 保存模型
    model.save('word_embedding_128')

if ops == TEST:
    model = Word2Vec.load("word_embedding_128")
    # 使用模型
    # 返回和一个词语最相关的多个词语以及对应的相关度
    items = model.most_similar(u'中国')
    for item in items:
        # 词的内容,词的相关度
        print(item[0], item[1])
    # 返回两个词语之间的相关度
    print(model.similarity(u'男人', u'女人'))

你可能感兴趣的:(信息抽取与问答系统)