Gensim学习笔记-1--理解corpora.Dictionary

gensim使用python标准的logging包,引入方式为:

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

学习任何开源项目首先都要理解其中的专业术语。
gensim中的必须理解的概念有:
1 raw strings 原始字符串
2 corpora 语料库
3 sparse vectors 稀疏向量
4 vector space model 向量空间模型
5 transformation 转换,指由稀疏向量组成的稀疏矩阵生成某个向量空间模型。
6 index 索引
……

要深入理解开源项目的运行原理,需要认真研究其中的核心对象。
1 corpora.Dictionary 对象
可以理解为python中的字典对象, 其Key是字典中的词,其Val是词对应的唯一数值型ID
构造方法

Dictionary(documents=None, prune_at=2000000)

document参数
Each document is a list of tokens = tokenized and normalized strings (either utf8 or unicode).
也可以是列表的列表例如常见的文档矩阵:

pprint(texts)
[[‘human’, ‘interface’, ‘computer’],
[‘survey’, ‘user’, ‘computer’, ‘system’, ‘response’, ‘time’],
[‘eps’, ‘user’, ‘interface’, ‘system’]]
dictionary = corpora.Dictionary(texts)

prune_at参数
起到控制向量的维数的作用
keeping the total number of unique words <= prune_at
To disable this pruning, set prune_at=None.

常用方法
添加document

add_documents(documents, prune_at=2000000)

压实,使得ID更加紧凑

compactify()

将一个raw string 转换为根据本词典构构造的向量

doc2bow(document, allow_update=False, return_missing=False)¶

If allow_update is set, then also update dictionary in the process: create ids for new words. At the same time, update document frequencies – for each word appearing in this document, increase its document frequency (self.dfs) by one.

去低频、高频词

filter_extremes(no_below=5, no_above=0.5, keep_n=100000)

从已有的语料库中构造

static from_corpus(corpus, id2word=None)¶

合并Dictionary

merge_with(other)

你可能感兴趣的:(gensim)