首页 > 自然语言处理 > 正文

4.Similarity and Queries

标签:Gensim, nlp
import logging


logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)

from gensim import corpora,models,similarities

import os
if os.path.exists('./model/dictionary.m'):
    dictionary=corpora.Dictionary.load('./model/dictionary.m')
    corpus=corpora.MmCorpus('./model/corpra.mms')

    lsi_model=models.LsiModel(corpus=corpus,id2word=dictionary,num_topics=5)
    doc='你今天心情怎么样'
    vec_doc=dictionary.doc2bow(list(doc))
    vec_lsi=lsi_model[vec_doc]
    print(vec_lsi)


    #build similarity matrix
    sim_matrix=similarities.MatrixSimilarity(lsi_model[corpus])
    sim_matrix.save('./model/lsi_sim.m')
    del(sim_matrix)
    sim_matrix=similarities.MatrixSimilarity.load('./model/lsi_sim.m')
    sims=sim_matrix[vec_lsi]
    sims = sorted(enumerate(sims), key=lambda item: -item[1])
    print(sims)


原创文章,转载请注明出处!
本文链接:http://youran.tech/posts/gensim-similarity-Queries.html
上篇: 3.Topic and Transformations
下篇: 5.Word2Vec

留言: