NLP05-Gensim源码[包与接口]

42 篇文章 1 订阅

这里写图片描述

摘要:粗略从的方面查看一下gensim包中的文件结构与接口,感性地认识一下gensim的源码都有些什么东西,这个是认识Gensim源码的第一步。内容包含了文件结构,核心接口,Corpora模块,Models模块 ,Similarity模块,Models模块 ,scripts, 集成sklearn,摘要与关键词,单元测试,topic coherence这几个方面。

0.文件结构

把开gensim包,目录结构如下地出现眼前:
这里写图片描述
模块分为语料,模型等等,另外interfaces.py核心接口,matutils.py数学工具,utils.py公共方法。nosy.py这个不重要,是用来监控py文档是否有修改更的。

1. Gensim核心接口[interfaces.py]###

这里写图片描述

1.1corpusABC

Interface (abstract base class) for corpora. A corpus is simply an iterable, where each iteration step yields one document:
语料接口(抽象基类),一个语料是一个简单的迭代器,每步产生一个文档;

>>> for doc in corpus:
>>>     # do something with the doc...

A document is a sequence of (fieldId, fieldValue) 2-tuples:
一个文档是一个二元组(域id,域值)序列;

>>> for attr_id, attr_value in doc:
>>>     # do something with the attribute
1.2 SimilarityABC

Abstract interface for similarity searches over a corpus.
In all instances, there is a corpus against which we want to perform the similarity search.
For each similarity search, the input is a document and the output are its similarities to individual corpus documents.
Similarity queries are realized by calling self[query_document].
There is also a convenience wrapper, where iterating over self yields similarities of each document in the corpus against the whole corpus (ie., the query is each corpus document in turn).
在语料之上的相似搜索抽象接口。
所有实例中,凭借一个语料我们可以执行相似搜索。
对于每个相似搜索,输入一个文档,输出是各自相似的文档集合;
相似查询是通过调用self[query_document]这样方法来实现的。
这里也有一个方便的包装器,可以自迭代按顺序产生自已的相似性文档 。

1.3 TransformationABC

Interface for transformations. A ‘transformation’ is any object which accepts a sparse document via the dictionary notation [] and returns another sparse document in its stead:
转换的接口,接收通过字典标记’[]‘的一个稀疏文档,返回取而代之的稀疏文档;

2. Corpora模块

This package contains implementations of various streaming corpus I/O format.
这个包包含了各种流式语料I/O格式的实现。
这里写图片描述
各类的层次关系,可以看成一个子类就是一个语料的储存形式了:
这里写图片描述

3.Models模块

This package contains algorithms for extracting document representations from their raw bag-of-word counts.
这个包主要是维护从源数据的词袋计算中抽取文档的表示算法;
models包下的文件结构:
这里写图片描述
各自的继承关系:
这里写图片描述

4. Similarity模块

This package contains implementations of pairwise similarity queries.
这个包是相似查询对的实现,
只有两个文件:docsim.py与index.py
docsim.py中的类如下,均继承于SimilarityABC接口。
Similarity模块下的类图:
这里写图片描述

5. Parsing模块

This package contains functions to preprocess raw text
文本预处理
里面包含两个文件:
preprocessing.py:文档的预处理,例如停用词,大小写等。
porter.py : Porter Stemming Algorithm 【词干提取算法】,来自论文
Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
no. 3, pp 130-137,
算法相关信息:http://www.tartarus.org/~martin/PorterStemmer
词干提取,也就是把单词的复数,第三人称之类的单词还原成原型,例如:

"""Get rid of plurals and -ed or -ing. E.g.,

   caresses  ->  caress
   ponies    ->  poni
   ties      ->  ti
   caress    ->  caress
   cats      ->  cat

   feed      ->  feed
   agreed    ->  agree
   disabled  ->  disable

   matting   ->  mat
   mating    ->  mate
   meeting   ->  meet
   milling   ->  mill
   messing   ->  mess

   meetings  ->  meet
"""

6. scripts

这个是一个脚本集合,方便处理与转换的,
例如

glove2word2vec.py,是GloVe vectors format 转成 word2vec text format;
USAGE: $ python -m gensim.scripts.glove2word2vec --input <GloVe vector file> --output <Word2vec vector file>
Where:
    <GloVe vector file>: Input GloVe .txt file
    <Word2vec vector file>: Desired name of output Word2vec .txt file
word2vec2tensor是word2vec转成tensor形式:
USAGE: $ python -m gensim.scripts.word2vec2tensor --input <Word2Vec model file> --output <TSV tensor filename prefix> [--binary] <Word2Vec binary flag>
Where:
    <Word2Vec model file>: Input Word2Vec model
    <TSV tensor filename prefix>: 2D tensor TSV output file name prefix
    <Word2Vec binary flag>: Set True if Word2Vec model is binary. Defaults to False.
Output:
    The script will create two TSV files. A 2d tensor format file, and a Word Embedding metadata file. Both files will
    us the --output file name as prefix

7. 集成sklearn

Scikit learn对于gensim的包装器:SklearnWrapperLdaModel与SklearnWrapperLsiModel

8. summarization

8.1 关键词:

def keywords(text, ratio=0.2, words=None, split=False, scores=False, pos_filter=[‘NN’, ‘JJ’], lemmatize=False, deacc=True)
关键词的计算用到了graph;

8.2 概述

def summarize(text, ratio=0.2, word_count=None, split=False)
主用到TextRank algorithm,计算用到了graph;

8.3 相关的数据结构及算法

BM25[bm25.py]
TextRank算法
Graph【common.py,graph.py】

9. 单元测试

10 topic coherence###

主题模型有评估模型,对于这方面的相关资料:

What is Topic Coherence?
https://rare-technologies.com/what-is-topic-coherence/

Exploring the Space of Topic Coherence Measures
http://svn.aksw.org/papers/2015/WSDM_Topic_Evaluation/public.pdf

Evaluating topic coherence measures
https://mimno.infosci.cornell.edu/nips2013ws/nips2013tm_submission_7.pdf

Topic Coherence To Evaluate Topic Models
http://qpleple.com/topic-coherence-to-evaluate-topic-models/

对topic cohearnce的演示:
https://nbviewer.jupyter.org/github/dsquareindia/gensim/blob/280375fe14adea67ce6384ba7eabf362b05e6029/docs/notebooks/topic_coherence_tutorial.ipynb

基于语义连贯性实现主题挖掘和分类 http://blog.csdn.net/shirdrn/article/details/7076505

【作者:happyprince, http://blog.csdn.net/ld326/article/details/78379449

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 自然语言处理(Natural Language Processing,简称NLP)是计算机科学与人工智能领域的一个重要研究方向,目的是让计算机能够理解、处理和生成人类的自然语言。NLP-100例是一份经典的NLP问题集合,含了各种与自然语言处理相关的问题和挑战。 这份NLP-100例涵盖了从基础的文本处理到更高级的自然语言理解和生成的问题。例如,其中括了文本预处理、词频统计、语法分析、词性标注、实体识别、情感分析、机器翻译等任务。 NLP-100例的目的是帮助研究者和开发者更好地理解NLP领域的核心问题和技术,同时提供一些典型的案例和数据集供实践和研究使用。通过完成这些例题,可以锻炼自己在NLP领域的能力和技术,提高对自然语言的处理和理解能力。 此外,NLP-100例也为研究者提供了一个可以与其他人交流和探讨的平台。研究者可以使用相同的数据集和问题进行实验和评估,从而更好地了解NLP技术的优劣和进展。 总之,NLP-100例是一个对NLP进行实践和研究的重要资。通过解决这些例题,可以深入理解自然语言处理的基础和技术,掌握各种NLP任务的方法和技巧。同时,它也是一个促进交流和合作的平台,为NLP研究者提供了一个共同的基础和语言。 ### 回答2: 自然语言处理(Natural Language Processing,简称NLP)是研究计算机与人类自然语言之间的交互的一门学科。NLP-100例指的是日本的一个NLP入门教程,含了100个常见的NLP问题和对应的解答。 NLP-100例涵盖了从文本处理到语义理解等多个方面的问题。其中,一些例子括:文本的分词、词性标注、句法分析、语义角色标注和文本分类等。 以分词为例,分词是将一段连续的文本分割成词语的过程。在NLP-100例中,可以通过使用Python中的分词工具NLTK(Natural Language Toolkit)来实现分词功能。 另外,对于文本的词性标注,NLP-100例提供了使用POS(Part-Of-Speech)标记对文本中的每个词进行词性标注的方法。可以使用NLTK提供的POS标注工具来实现。 此外,NLP-100例还括了语义角色标注的问题,语义角色标注是为了确定句子中的谓语动词所承担的语义角色,如施事者、受事者、时间等。可以使用Stanford CoreNLP工具来实现语义角色标注。 最后,NLP-100例还介绍了文本分类的问题,文本分类是将文本划分到预定义的类别中。可以使用机器学习算法,如朴素贝叶斯或支持向量机(SVM)等来进行文本分类。 通过学习NLP-100例,我们可以了解到自然语言处理的基本方法和技术,并且可以利用这些技术来解决相关的自然语言处理问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值