gensim入门

原英文:https://github.com/RaRe-Technologies/gensim/blob/develop/docs/notebooks/gensim%20Quick%20Start.ipynb

作者小白,一边学一边翻译,拿出来跟大家分享,欢迎拍砖。

简介:gensim是挖掘文档语义结构的工具,通过语料库(文档集),生成表示文本的向量。

语料库:语料库是数字文档的集合,是gensim的输入。通过语料库得出的潜在结构,可用于新的文档。所以,语料库也叫做训练集。下面,是一个简单的语料库,包含9句话:

raw_corpus = ["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"]
以上是一个简单的例子,实际应用时需要用全部的文档组成语料库,也可以用维基百科的文章或者其他的大型文档集合作为语料库(语料库越大,得出的词向量更准确)。

语料库收集完成之后,需要进行一系列的预处理,下面的例子中尽量使用简单的操作,比如去除停用词(of, the, a, ,,,本人研究方向为人格计算,有研究表明,不同人格的人在写作时,使用停用词的情况不尽相同,所以,在实际应用时,并不是所有的情况都需要去除停用词。),去除只出现一次的词(有的词表是通过选择出现次数最多的n个词)。为了做到这一步,需要进行词素切分(tokenise),词素切分是将文档分成一个一个的词(可以用空格作为分割符)。

# Create a set of frequent words
stoplist = set('for a of the and to in'.split(' '))
# Lowercase each document, split it by white space and filter out stopwords
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in raw_corpus]

# Count word frequencies
from collections import defaultdict
frequency = defaultdict(int)
for text in texts:
    for token in text:
        frequency[token] += 1

# Only keep words that appear more than once
processed_corpus = [[token for token in text if frequency[token] > 1] for text in texts]
processed_corpus
处理之前,赋给语料库中每个词一个整数ID,可以使用gensim.corpora.Dictionary类,这个词典定义了所有词的词汇表。

from gensim import corpora

dictionary = corpora.Dictionary(processed_corpus)
print(dictionary)
向量

用向量表示文档的方法有很多,一种简单的方法是词袋模型,在词袋模型中,根据每个词在文档中的频率组成向量,来表示文档。比如词典包括【‘coffee’,‘milk’,‘sugar’,‘spoon’】,文档是“coffee milk coffee”,那么用【2,1,0,0】来表示文档。文档向量的长度就是词典的长度。词袋模型忽略了文档中词素的顺序。

在本例中,语料库中有12个的词,也就是说每篇文档都使用12维的向量来表示。使用词典将文档转换为12为向量,

print(dictionary.token2id)
{u'minors': 11, u'graph': 10, u'system': 6, u'trees': 9, u'eps': 8, u'computer': 1, u'survey': 5, u'user': 7, u'human': 2, u'time': 4, u'interface': 0, u'response': 3}
新例子:

元组中的第一个元素是词素在词典中对应的ID,第二个是该词素在文档中出现的次数。其中,词典中并不包含interaction。注意,这里只包括了文档中实际出现过的词。

bow_corpus = [dictionary.doc2bow(text) for text in processed_corpus]
bow_corpus

模型

接下来,使用模型model将语料库转变为向量,

from gensim import models
# train the model
tfidf = models.TfidfModel(bow_corpus)
# transform the "system minors" string
tfidf[dictionary.doc2bow("system minors".lower().split())]

另一篇翻译的文章:http://blog.csdn.net/Star_Bob/article/details/47808499


model = gensim.models.Word2Vec(sentence)

#sentence是预处理后的文本

model.similarity('first', 'second')   #输出这两个词的相似度,也就是cos(x,y)

print(model.wv.vocab)    #显示model中的词表

model.most_similar(['school'], topn = 2)       #显示跟school最接近的topn个词


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值