赞
踩
Gensim能很方便的分析文本,包括了TFIDF,LDA,LSA,DP等文本分析方法
首先将文本处理生成dictionary和corpus。
dictionary是词典,包含词以及词在词典中对应的位置。
corpus将文本存贮成(词在词典中位置,词频)这种形式,每个文本为一行。
实战
from gensim import corpora, models, similarities
from pprint import pprint
def GenDictandCorpus():
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
texts = [[word for word in document.lower().split()] for document in documents]
# 词典
dictionary = corpora.Dictionary(texts)
# 词库,以(词,词频)方式存贮
corpus = [dictionary.doc2bow(text) for text in texts]
print(dictionary)
print(corpus)
return dictionary, corpus
结果:
实战
def Tfidf():
dictionary, corpus = GenDictandCorpus()
# initialize a model
tfidf = models.TfidfModel(corpus)
# print(tfidf)
# Transforming vectors
# 此时,tfidf被视为一个只读对象,可以用于将任何向量从旧表示(词频)转换为新表示(TfIdf实值权重)
doc_bow = [(0, 1), (1, 1)]
# 使用模型tfidf,将doc_bow(由词,词频)表示转换成(词,tfidf)表示
# print(tfidf[doc_bow])
# 转换整个词库
corpus_tfidf = tfidf[corpus]
for doc in corpus_tfidf:
print(doc)
return corpus_tfidf
结果:
实战:
def LDA():
dictionary, corpus = GenDictandCorpus()
corpus_tfidf = Tfidf()
ldamodel = models.LdaModel(corpus, id2word=dictionary, num_topics=2)
ldamodel.print_topics()
pprint(ldamodel.print_topics())
结果:
实战:
# 潜在语义索引(Latent Semantic Indexing,以下简称LSI),有的文章也叫Latent Semantic Analysis(LSA)
# LSI是基于奇异值分解(SVD)的方法来得到文本的主题的
def LSI():
dictionary, corpus = GenDictandCorpus()
corpus_tfidf = Tfidf()
# initialize an LSI transformation
lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=2)
corpus_lsi = lsi[corpus_tfidf]
# print(corpus_lsi)
# pprint(lsi.print_topics(2))
# 在这里实际执行了bow-> tfidf和tfidf-> lsi转换
for doc in corpus_lsi:
print(doc)
# lsi.save('/tmp/model.lsi')
# lsi = models.LsiModel.load('/tmp/model.lsi')
结果:
实战:
# 随机投影(Random Projections),RP旨在减少矢量空间维数。
# 这是非常有效的方法,通过投掷一点随机性来近似文档之间的TfIdf距离。
# 推荐的目标维度数百/千,取决于您的数据集。
def RP():
corpus_tfidf = Tfidf()
RP_model = models.RpModel(corpus_tfidf, num_topics=2)
# print(RP_model)
corpus_rp = RP_model[corpus_tfidf]
for doc in corpus_rp:
print(doc)
结果:
如有理解错误,还望指教。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。