鱼C论坛

 找回密码
 立即注册
查看: 2745|回复: 2

请求处理报错,谢谢

[复制链接]
发表于 2019-9-11 15:50:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import jieba

import jieba.analyse

from gensim.test.utils import common_texts, get_tmpfile

from gensim.models import Word2Vec

#文件位置需要改为自己的存放路径

#将文本分词

with open(r'C:\Users\amgalang\Desktop\in_the_name_of_people.txt',encoding='utf-8') as f:

    document = f.read()

    document_cut = jieba.cut(document)

    result = ' '.join(document_cut)

    with open('./in_the_name_of_people_segment.txt', 'w',encoding="utf-8") as f2:

        f2.write(result)



#加载语料

sentences = word2Vec.LineSentence('./in_the_name_of_people_segment.txt')

#训练语料

path = get_tmpfile("word2vec.model") #创建临时文件

model = word2Vec.Word2Vec(sentences, hs=1,min_count=1,window=10,size=100)

# model.save("word2vec.model")

# model = Word2Vec.load("word2vec.model")

#输入与“贪污”相近的100个词

for key in model.wv.similar_by_word('贪污', topn =100):

    print(key)








代码跑完结果:

Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\amgalang\AppData\Local\Temp\jieba.cache
Loading model cost 0.872 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "C:/Users/amgalang/Desktop/3.py", line 30, in <module>
    sentences = word2Vec.LineSentence('./in_the_name_of_people_segment.txt')
NameError: name 'word2Vec' is not defined


希望处理报错,望大神们给予修改。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-9-11 18:07:30 | 显示全部楼层
这个word2Vec要大写成Word2Vec,要跟你加载的模块名称一致
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-11 21:15:13 | 显示全部楼层
word2Vec 要改成 Word2Vec

  1. import jieba

  2. import jieba.analyse

  3. from gensim.test.utils import common_texts, get_tmpfile

  4. from gensim.models import Word2Vec

  5. #文件位置需要改为自己的存放路径

  6. #将文本分词

  7. with open(r'C:\Users\amgalang\Desktop\in_the_name_of_people.txt',encoding='utf-8') as f:

  8.     document = f.read()

  9.     document_cut = jieba.cut(document)

  10.     result = ' '.join(document_cut)

  11.     with open('./in_the_name_of_people_segment.txt', 'w',encoding="utf-8") as f2:

  12.         f2.write(result)



  13. #加载语料

  14. sentences = word2Vec.LineSentence('./in_the_name_of_people_segment.txt')

  15. #训练语料

  16. path = get_tmpfile("word2vec.model") #创建临时文件

  17. model = Word2Vec.Word2Vec(sentences, hs=1,min_count=1,window=10,size=100)

  18. # model.save("word2vec.model")

  19. # model = Word2Vec.load("word2vec.model")

  20. #输入与“贪污”相近的100个词

  21. for key in model.wv.similar_by_word('贪污', topn =100):

  22.     print(key)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-19 06:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表