python nlp人工智能例子,jieba分词, word2vec

金庸老爷子一共写了15部武侠小说,它们分别是:

  • 《飞狐外传》(1960年)
  • 《雪山飞狐》(1959年)
  • 《连城诀》(1963年)
  • 《天龙八部》(1963年)
  • 《射雕英雄传》(1957年)
  • 《白马啸西风》(1961年)
  • 《鹿鼎记》(1969年)
  • 《笑傲江湖》(1967年)
  • 《书剑恩仇录》(1955年)
  • 《神雕侠侣》(1959年)
  • 《侠客行》(1965年)
  • 《倚天屠龙记》(1961年)
  • 《碧血剑》(1956年)
  • 《鸳鸯刀》(1961年)
  • 《越女剑》(1970年)
    我们现在就用 Python 来探索一下金庸小说中的武侠世界吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import jieba.analyse
from gensim.models import word2vec
 
if __name__ == '__main__':
    # 1 用jieba分词处理小说
    text = './novel/天龙八部.txt'
    processedText = './novel/处理后的天龙八部.txt'
    with open(text, encoding="utf8") as f:
        document = f.read()
        document_cut = jieba.cut(document)
        result = ' '.join(document_cut)
        with open(processedText, 'w', encoding="utf8") as f2:
            f2.write(result)
             
    # 2训练模型
    sentences = word2vec.LineSentence(processedText)
    model = word2vec.Word2Vec(sentences, hs=1, min_count=1, window=3)
    # 3 检验模型
    print(model.wv.similar_by_key("乔峰", topn=5))
 
    # 檢視兩個詞的相近程度
    print("乔峰|萧峰的相似度:", model.wv.similarity("乔峰", "萧峰"))
 
    # 找出不同類的詞
    print("乔峰 |木婉清  劉慶祝中不同類別的詞為:", model.wv.doesnt_match(u"乔峰 木婉清".split()))

  

posted @   拍片哥  阅读(271)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示