import logging import jieba import gensim from gensim.models import Word2Vec def get_Segment(): texts = [] jieba.load_userdict("data\\name_dict.txt") with open('data\\in_the_name_of_people.txt','r',encoding='utf-8') as f: for line in f.readlines(): texts.append(list(jieba.cut(line.strip()))) with open('data\\in_the_name_of_people_segment.txt','w',encoding='utf-8')as f: for line in texts: f.write(" ".join(w for w in line)) f.write("\r\n") def getmodel(): logging.basicConfig(format='%(asctime)s : %(LeveLname)s : %(message)s',level=logging.INFO) sentences = word2vec.LineSentence('data\\in_the_name_of_people_segment.txt') model = word2vec.Word2Vec(sentences,min_count=1) return model if __name__=='__main__': get_Segment() model = getmodel() print('相似度: ',model.wv.similarity('人民','名义')) print(model.wv.similarity('候亮平','钟小艾')) print(model.mv.most_similar('候亮平',topn=10))
时间: 2023-12-19 20:04:23 浏览: 152
这段代码存在一些问题,我来帮你解释一下:
1. 代码中缺少了部分 import 语句,需要加上:
```
from gensim.models import word2vec
```
2. 在 `logging.basicConfig()` 函数的第一个参数中,`LeveLname` 应该是 `levelname`,需要修正。
3. 在 `getmodel()` 函数中,`word2vec.LineSentence()` 应该是 `word2vec.Text8Corpus()`,需要修正。
4. 在 `getmodel()` 函数中,`model.mv.most_similar()` 应该是 `model.wv.most_similar()`,需要修正。
修正后的代码如下:
```
import logging
import jieba
import gensim
from gensim.models import word2vec
def get_Segment():
texts = []
jieba.load_userdict("data\\name_dict.txt")
with open('data\\in_the_name_of_people.txt','r',encoding='utf-8') as f:
for line in f.readlines():
texts.append(list(jieba.cut(line.strip())))
with open('data\\in_the_name_of_people_segment.txt','w',encoding='utf-8')as f:
for line in texts:
f.write(" ".join(w for w in line))
f.write("\r\n")
def getmodel():
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
sentences = word2vec.Text8Corpus('data\\in_the_name_of_people_segment.txt')
model = word2vec.Word2Vec(sentences, min_count=1)
return model
if __name__=='__main__':
get_Segment()
model = getmodel()
print('相似度: ',model.wv.similarity('人民','名义'))
print(model.wv.similarity('候亮平','钟小艾'))
print(model.wv.most_similar('候亮平',topn=10))
```
希望能帮到你!
阅读全文