Gensim详细介绍和使用:一个Python文本建模库

The algorithms in Gensim, such as Word2Vec, FastText, Latent Semantic Indexing (LSI, LSA, LsiModel), Latent Dirichlet Allocation (LDA, LdaModel) etc, automatically discover the semantic structure of documents by examining statistical co-occurrence patterns within a corpus of training documents. These algorithms are unsupervised, which means no human input is necessary – you only need a corpus of plain text documents.
Gensim中的算法,如Word2Verc、FastText、潜在语义索引(LSI、LSA、LsiModel)、潜在狄利克雷分配(LDA、LdaModel)等,通过检查训练文档语料库中的统计共现模式,自动发现文档的语义结构。这些算法是无监督的,这意味着不需要人工输入——你只需要一个纯文本文档的语料库。

一、安装

pip install --upgrade gensim

二、文本预处理

  • Document: 文档,文档字符串 即:“这是一段话”
  • Corpus: 语料,文档字符串列表 即:[“这是一段话”, “这是一段话”]
  • Vector: 向量,应该最好懂 即词在空间中的位置表
  • Bow Corpus:文档分词元组化后的语料列表,元组第一个位置是词对应dictionary的id,第二个是文档中该词出现的次数

2.1 中文语料处理

中文语料:无大小写问题,标点符号问题,词之间无空格,无时态词,需要分词

这里我们使用清华的分词工具Thulac进行分词;

def chinese_process(text_corpus, stoplist=[], k=0, punctuation='"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、\u3000、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·.!?。。'):
    """
    text_corpus: 语料
    stoplist: 需要删除的词
    k:过滤次数小于等于k次的词
    punctuation:需要删除的标点符号
    """
    import thulac
    from collections import defaultdict
    model = thulac.thulac(seg_only=True)
    texts = [[word for word in model.cut(document, text=True).split() if word not in stoplist] for document in text_corpus]
    frequency = defaultdict(int)
    for text in texts:
        for token in text:
            frequency[token] += 1
    processed_corpus = [[token for token in text if frequency[token] > k] for text in texts]
    return processed_corpus

测试:

text_corpus = [
    '实验室abc计算机应用的人机界面',
    '用户对计算机系统响应时间的看法调查',
    'EPS用户界面管理系统',
    'EPS的系统与人系统工程测试',
    '用户感知的响应时间与误差测量的关系',
    '随机二进制无序树的生成',
    '树中路径的交集图',
    '图子Ⅳ树的宽度和井的拟序',
    '图表未成年人调查'
]
print(chinese_process(text_corpus))
# [['实验室', 'abc', '计算机', '应用', '的', '人', '机界', '面'],
#  ['用户', '对', '计算机', '系统', '响应', '时间', '的', '看法', '调查'],
#  ['EPS', '用户', '界面', '管理', '系统'],
#  ['EPS', '的', '系统', '与', '人', '系统工程', '测试'],
#  ['用户', '感知', '的', '响应', '时间', '与', '误差', '测量', '的', '关系'],
#  ['随机', '二进制', '无序', '树', '的', '生成'],
#  ['树', '中', '路径', '的', '交集', '图'],
#  ['图子', 'Ⅳ', '树', '的', '宽度', '和', '井', '的', '拟序'],
#  ['图表', '未成年人', '调查']]

2.2 英文语料处理

英文语料:大小写问题,标点符号问题,词之间有空格, 词有不同形式,需要分词

def english_process(text_corpus, stoplist=[], k=0, punctuation='!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'):
    """
    text_corpus: 语料
    stoplist: 需要删除的词
    k:过滤次数小于等于k次的词
    punctuation:需要删除的标点符号
    """
    from collections import defaultdict
    import re
    texts = [[word for word in re.sub(r'[{}]+'.format(punctuation), '', document).lower().split() if word not in stoplist] for document in text_corpus]
    frequency = defaultdict(int)
    for text in texts:
        for token in text:
            frequency[token] += 1
    processed_corpus = [[token for token in text if frequency[token] > k] for text in texts]
    return processed_corpus

测试:

text_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",
]
stoplist = set('for a of the and to in'.split(' '))
print(english_process(text_corpus, stoplist))
# [['human', 'machine', 'interface', 'lab', 'abc', 'computer', 'applications'],
#  ['survey', 'user', 'opinion', 'computer', 'system', 'response', 'time'],
#  ['eps', 'user', 'interface', 'management', 'system'],
#  ['system', 'human', 'system', 'engineering', 'testing', 'eps'],
#  ['relation', 'user', 'perceived', 'response', 'time', 'error', 'measurement'],
#  ['generation', 'random', 'binary', 'unordered', 'trees'],
#  ['intersection', 'graph', 'paths', 'trees'],
#  ['graph', 'minors', 'iv', 'widths', 'trees', 'well', 'quasi', 'ordering'],
#  ['graph', 'minors', 'survey']]

2.3 BOW语料建立

首先把语料进行处理后,得到二维列表,然后将二维列表传入gensim库中的corpus.dictionary模块建立字典,然后根据字典对处理后的语料进行生成BOW语料

text_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",
]
# 中文语料处理
text_corpus = chinese_process(text_corpus)
# 建立字典
dictionary = gensim.corpora.Dictionary(text_corpus)
# dictionary.token2id {'abc': 0, '人': 1,'实验室': 2, '应用': 3, ...}
bow_corpus = [dictionary.doc2bow(item) for item in text_corpus]
# [[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)],
#  [(5, 1), (6, 1), (8, 1), (9, 1), (10, 1), (11, 1), (12, 1), (13, 1), (14, 1)],
#  [(11, 1), (13, 1), (15, 1), (16, 1), (17, 1)],....]

三、模型使用

3.1 word2vec

Word2Vec 是一种词向量模型,它使用浅层神经网络将单词嵌入到低维向量空间中。结果是一组词向量,其中在向量空间中靠近的向量根据上下文具有相似的含义,而彼此相距较远的词向量具有不同的含义。

word2vec 有两种出名的模型,两种加速方式,如果理解可以在此自由配置,
论文实现:Efficient Estimation of Word Representations in Vector Space [CBOW and Skip-gram]
sg : {0, 1}, optional
  Training algorithm: 1 for skip-gram; otherwise CBOW.
hs : {0, 1}, optional
  If 1, hierarchical softmax will be used for model training.
  If 0, hierarchical softmax will not be used for model training.
negative : int, optional
  If > 0, negative sampling will be used, the int for negative specifies how many “noise words” should be drawn (usually between 5-20).
  If 0, negative sampling will not be used.

模型相关代码:

text_corpus = [['实验室', 'abc', '计算机', '应用', '的', '人', '机界', '面'],
 ['用户', '对', '计算机', '系统', '响应', '时间', '的', '看法', '调查']]
 
word2vec = gensim.models.Word2Vec(
    sentences=text_corpus, 
    min_count=1,
    window=3,
    vector_size=100,
)

# 输出word向量
word2vec.wv['中']
# array([ 0.00480066, -0.00362838, -0.00426481,  0.00121976, -0.0041273, ... dtype=float32)

# 计算两个词之间的相似性
word2vec.wv.similarity('图表','图子')
# 0.13703643

# 类似于king-man+woman=queen
word2vec.wv.most_similar(positive=['图子', '图表'], negative=['中'])
# [('系统工程', 0.1806158870458603),('感知', 0.17817144095897675),('界面', 0.13787229359149933),...]

# 找不和其他匹配的项
word2vec.wv.doesnt_match(['图子', '图表', '中'])
# '中'

词向量可视化 这里使用的是TSNE算法进行的降维

from sklearn.manifold import TSNE
import numpy as np
import matplotlib.pyplot as plt

# 中文设置
plt.rcParams['font.sans-serif']=['SimHei'] 
plt.rcParams['axes.unicode_minus']=False

def reduce_dimensions(model):
    num_dimensions = 2

    # 提取vectors和labels
    vectors = np.asarray(model.wv.vectors)
    labels = np.asarray(model.wv.index_to_key)

	# 降维
    tsne = TSNE(n_components=num_dimensions, random_state=0)
    vectors = tsne.fit_transform(vectors)

    x_vals = [v[0] for v in vectors]
    y_vals = [v[1] for v in vectors]
    return x_vals, y_vals, labels


x,y,label = reduce_dimensions(word2vec)
plt.scatter(x,y)
for x_,y_,label_ in zip(x,y,label):
    plt.text(x_, y_, label_)
plt.show()

可视化结果如下:

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 文本关键词抽取方法有很多,常见的有TF-IDF、TextRank、LDA等。 在Python中,可以使用jieba、gensim、nltk等来实现文本关键词抽取。 - jieba一个中文分词工具,可以用来提取中文文本的关键词。 - gensim一个用于文档索引和文档相似性的,可以用来进行文本主题建模。 - nltk一个自然语言处理工具包,可以用来进行词频统计、文本分类等操作。 ### 回答2: 从文本中抽取关键词有多种方法,以下是其中一些常见的办法: 1. 基于词频统计:通过分词将文本划分为词语,然后计算每个词出现的次数,并选择词频较高的词语作为关键词。 2. 基于TF-IDF:TF-IDF算法通过计算词频和逆文档频率,来确定一个词语在文本中的重要程度,根据TF-IDF值高低选择关键词。 3. 基于TextRank:TextRank算法将文本中的词语作为节点,词语之间的关系作为边,通过迭代计算词语之间的权重,得出关键词。 4. 基于主题模型:主题模型将文本看作是多个主题的组合,通过计算每个词语在主题中的权重,选择权重较高的词语作为关键词。 在Python中,可以使用以下来进行关键词抽取: 1. jieba:jieba是一个开源的中文分词,可以用于划分文本中的词语,并计算词语的频率。 2. scikit-learn:scikit-learn是一个常用的机器学习,提供了TF-IDF向量化的功能,可以计算文本的TF-IDF向量,从而得出关键词。 3. GensimGensim一个Python,提供了TextRank算法和主题模型的实现,可以用于文本关键词抽取。 以上只是一些常见的方法和Python,实际上还有其他更多的方法和可以进行关键词抽取,选择适合自己需求和文本特点的方法和最为重要。 ### 回答3: 从文本中抽取关键词可以使用以下几种方法: 1. 基于统计的方法:根据词在文本中出现的频率来判断其重要程度,常用的统计方法有TF-IDF、词频等。 2. 基于词性标注的方法:通过标注每个词的词性,然后根据一定规则选择关键词,例如选择名词、动词等。 3. 基于机器学习的方法:使用机器学习算法对文本进行训练,然后预测每个词的重要程度,例如使用TextRank算法。 在Python中,可以使用以下来进行文本关键词抽取: 1. jieba:提供了中文分词的功能,可以用于将文本分词,然后计算关键词的出现频率。 2. NLTK:提供了各种文本处理的功能,包括分词、词性标注等,可以用于进行关键词抽取。 3. gensim:提供了TF-IDF模型和TextRank模型的实现,可以用于进行基于统计的关键词抽取。 4. scikit-learn:提供了机器学习算法的实现,可以用于进行基于机器学习的关键词抽取,例如使用分类算法进行词重要性预测。 根据不同的需求和文本特点,选择适合的方法和,可以更准确地抽取关键词。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值