GENSIM 使用笔记1 — 语料和向量空间
GENSIM 使用笔记2 — 主题模型和相似性查询
在上一个笔记当中,使用gensim针对中文预料创建了字典和语料库,在这一章节中,主要讲下如何创建相应的主题模型和相似度的匹配,对应原教程的第二章和第三章
模型创建
在GENSIM当中,最基本的就是创建TF-IDF模型,随后可以在TF-IDF的基础上创建LSI/LDA等等的主题模型。
GENSIM已经封装的很好了,所以其实这部分会过的非常快(算法具体的内容这里不讲)
那么,首先我们需要加载上一篇笔记当中的数据:
# 首先加载语料库
if os.path.exists('mydict.dic') and os.path.exists('corpus.mm'):
dictionary = corpora.Dictionary.load('mydict.dic')
corpus = corpora.MmCorpus('corpus.mm')
print 'used files generated from string2vector'
else:
print 'please run string2vector firstly'
随后,我们需要首先创建最关键的TF-IDF模型,并且可以生成TF-IDF的语料库,并将其序列化,以方便我们的后续使用
#创建一个model
tfidf = models.Tfidf