Gensim - 文档和语料库
在这里,我们将学习 Gensim 的核心概念,主要关注文档和语料库。
Gensim 的核心概念
以下是理解和使用 Gensim 所需的核心概念和术语 −
文档 − Z它指的是一些文本。
语料库 − 它指的是文档的集合。
向量 − 文档的数学表示称为向量。
模型 −它指的是一种用于将向量从一种表示形式转换为另一种表示形式的算法。
什么是文档?
如上所述,它指的是一些文本。如果我们详细了解,它是一个文本序列类型的对象,在 Python 3 中称为'str'。例如,在 Gensim 中,文档可以是任何内容,例如 −
- 140 个字符的短推文
- 单段,即文章或研究论文摘要
- 新闻文章
- 书籍
- 小说
- 论文
文本序列
文本序列类型在 Python 3 中通常称为'str'。众所周知,在 Python 中,文本数据是用字符串或更具体地说是'str'对象处理的。字符串基本上是 Unicode 代码点的不可变序列,可以用以下方式编写 −
单引号 − For example, ‘Hi! How are you?’. It allows us to embed double quotes also. For example, ‘Hi! "How" are you?’
双引号 − For example, "Hi! How are you?". It allows us to embed single quotes also. For example, "Hi! 'How' are you?"
三重引号 − It can have either three single quotes like, '''Hi! How are you?'''. or three double quotes like, """Hi! 'How' are you?"""
所有空格都将包含在字符串文字中。
示例
以下是 Gensim 中文档的示例 −
Document = "Tutorialspoint.com 是最大的在线教程库,而且全部免费"
什么是语料库?
语料库可以定义为在自然交流环境中生成的大型且结构化的机器可读文本集。在 Gensim 中,文档对象的集合称为语料库。语料库的复数形式为corpora。
语料库在 Gensim 中的作用
Gensim 中的语料库具有以下两个作用 −
用作训练模型的输入
语料库在 Gensim 中发挥的第一个也是最重要的作用是作为训练模型的输入。为了初始化模型的内部参数,在训练期间,模型会从训练语料库中寻找一些共同的主题和话题。如上所述,Gensim 专注于无监督模型,因此它不需要任何类型的人工干预。
用作主题提取器
一旦模型经过训练,它就可以用于从新文档中提取主题。这里,新文档是训练阶段未使用的文档。
示例
语料库可以包括某个人的所有推文、报纸上所有文章的列表或特定主题的所有研究论文等。
收集语料库
以下是包含 5 个文档的小型语料库的示例。这里,每个文档都是由单个句子组成的字符串。
t_corpus = [ "A survey of user opinion of computer system response time", "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", ]
预处理收集语料库
一旦我们收集了语料库,就应该采取一些预处理步骤来保持语料库的简单性。我们可以简单地删除一些常用的英文单词,如"the"。我们也可以删除语料库中只出现一次的单词。
例如,以下 Python 脚本用于将每个文档小写,用空格将其拆分并过滤掉停用词 −
示例
import pprint t_corpus = [ "A survey of user opinion of computer system response time", "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", ] stoplist = set('for a of the and to in'.split(' ')) processed_corpus = [[word for word in document.lower().split() if word not in stoplist] for document in t_corpus] pprint.pprint(processed_corpus) ]
输出
[['survey', 'user', 'opinion', 'computer', 'system', 'response', 'time'], ['relation', 'user', 'perceived', 'response', 'time', 'error', 'measurement'], ['generation', 'random', 'binary', 'unordered', 'trees'], ['intersection', 'graph', 'paths', 'trees'], ['graph', 'minors', 'iv', 'widths', 'trees', 'well', 'quasi', 'ordering']]
有效的预处理
Gensim 还提供了更有效的语料库预处理功能。在这种预处理中,我们可以将文档转换为小写标记列表。我们还可以忽略太短或太长的标记。这样的函数是gensim.utils.simple_preprocess(doc, deacc=False, min_len=2, max_len=15)。
gensim.utils.simple_preprocess() 函数
Gensim 提供此功能将文档转换为小写标记列表,并忽略太短或太长的标记。它具有以下参数 −
doc(str)
它指的是应对其应用预处理的输入文档。
deacc(bool, 可选)
此参数用于从标记中删除重音符号。它使用 deaccent() 来执行此操作。
min_len(int, 可选)
借助此参数,我们可以设置标记的最小长度。短于定义长度的标记将被丢弃。
max_len(int, 可选)
借助此参数,我们可以设置标记的最大长度。长于定义长度的标记将被丢弃。
此函数的输出将是从输入文档中提取的标记。