当前位置:首页 » 《关于电脑》 » 正文

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

13 人参与  2024年04月22日 17:05  分类 : 《关于电脑》  评论

点击全文阅读


Gensim = “Generate Similar”

一、安装二、文本预处理2.1 中文语料处理2.2 英文语料处理2.3 BOW语料建立 三、模型使用3.1 word2vec

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=queenword2vec.wv.most_similar(positive=['图子', '图表'], negative=['中'])# [('系统工程', 0.1806158870458603),('感知', 0.17817144095897675),('界面', 0.13787229359149933),...]# 找不和其他匹配的项word2vec.wv.doesnt_match(['图子', '图表', '中'])# '中'

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

from sklearn.manifold import TSNEimport numpy as npimport matplotlib.pyplot as plt# 中文设置plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=Falsedef 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, labelsx,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()

可视化结果如下:


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/98908.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 未婚夫女兄弟把婚车改成宠物灵车,我反手让她的宾利变破烂最新阅读_魏成鸣乔诗诗林书妍小编推荐_小说后续在线阅读_无删减免费完结_
  • 军训当天男友为校花虐功勋犬,重生后我杀疯了潜力榜_顾野杜璇闻言大结局_小说后续在线阅读_无删减免费完结_
  • 天塌了!大佬们全被我捡回家了阿昭,小白,李惊雪小说整本+后续(阿昭,小白,李惊雪)清爽版阅读
  • 重生八零,我笑看真千金用土气布料卖港商免费阅读_妹妹姜雅沈俊爆款_小说后续在线阅读_无删减免费完结_
  • 秦昭:+后续人物讨喜无套路全集手握一把刀,砍翻万道!
  • 狸奴抓伤阿娘后,我和爹提议换个娘后续_阿爹阿娘宁王最新阅读_小说后续在线阅读_无删减免费完结_
  • 盛夏没有晴天小说(阮苏梨,傅屿安,宋颖)小说结尾+隐藏篇章(盛夏没有晴天阮苏梨,傅屿安,宋颖)畅享阅读
  • 全书浏览天降好运?村西头疯婆子是神医!(李萍萍周大孙月娘)_天降好运?村西头疯婆子是神医!(李萍萍周大孙月娘)全书结局
  • 校园星芒爱与科幻的交织(苏然林悦)全书浏览_校园星芒爱与科幻的交织全书浏览
  • 包浆修复师从核到银河(苏星摇豆汁儿于乾)_包浆修复师从核到银河
  • 小说大结局小说风入江云小说已更新+特别篇(柳江云,喜鹊,萧桉)纯净版
  • 连若涵,江屿白,秦知意小说(老婆为十个替身逼我卖身,我走后她悔疯了)序章+全章阅读(连若涵,江屿白,秦知意)最新章节

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1