视频网站用什么做的好,深圳的小程序开发公司,商丘搜索引擎优化,大型外包公司文章目录 自然语言处理Gensim入门#xff1a;建模与模型保存关于gensim基础知识1. 模块导入2. 内部变量定义3. 主函数入口 (if __name__ __main__:)4. 加载语料库映射5. 加载和预处理语料库6. 根据方法参数选择模型训练方式7. 保存模型和变换后的语料8.代码 自然语言处理Gens… 文章目录 自然语言处理Gensim入门建模与模型保存关于gensim基础知识1. 模块导入2. 内部变量定义3. 主函数入口 (if __name__ __main__:)4. 加载语料库映射5. 加载和预处理语料库6. 根据方法参数选择模型训练方式7. 保存模型和变换后的语料8.代码 自然语言处理Gensim入门建模与模型保存
关于gensim基础知识
Gensim是一个专门针对大规模文本数据进行主题建模和相似性检索的Python库。 MmCorpus是gensim用于高效读写大型稀疏矩阵的一种格式适用于大数据集。 TF-IDF是一种常见的文本表示方法通过对词频进行加权以突出重要性较高的词语。 LSI、LDA和RP都是降维或主题提取方法常用于信息检索、文本分类和聚类任务。
这段代码是使用gensim库生成主题模型的一个脚本它根据用户提供的语言和方法参数来训练文本数据集并将训练好的模型保存为文件。以下是核心代码逻辑的分析与解释
1. 模块导入
导入了logging模块用于记录程序运行日志。导入sys模块以获取命令行参数和程序名。导入os.path模块处理文件路径相关操作。从gensim.corpora导入dmlcorpus一个用于加载特定格式语料库的模块和MmCorpus存储稀疏矩阵表示的文档-词项矩阵的类。从gensim.models导入四个模型lsimodel、ldamodel、tfidfmodel、rpmodel分别对应潜在语义索引LSI、潜在狄利克雷分配LDA、TF-IDF转换模型以及随机投影RP。
2. 内部变量定义
DIM_RP, DIM_LSI, DIM_LDA 分别指定了RP、LSI和LDA模型的维度大小。
3. 主函数入口 (if __name__ __main__:)
配置日志输出格式并设置日志级别为INFO。检查输入参数数量是否满足要求至少包含语言和方法两个参数否则打印帮助信息并退出程序。获取指定的语言和方法参数。
4. 加载语料库映射
根据传入的语言参数创建DmlConfig对象该对象包含了语料库的相关配置信息如存放结果的目录等。加载词汇表字典即wordids.txt文件将其转换成id2word字典结构以便在后续模型构建中将词语ID映射回实际词语。
5. 加载和预处理语料库
使用MmCorpus加载二进制bow.mm文件该文件存储了文档-词项矩阵每个文档是一个稀疏向量表示。
6. 根据方法参数选择模型训练方式
如果方法为’tfidf’则训练并保存TF-IDF模型该模型对原始词频进行加权增加了逆文档频率因子。若方法为’lda’则训练LDA模型这是一个基于概率统计的主题模型通过文档-主题分布和主题-词语分布抽取主题结构。若方法为’lsi’首先用TF-IDF模型转换语料然后在此基础上训练LSI模型它是一种线性代数方法用于发现文本中的潜在主题空间。若方法为’rp’同样先转为TF-IDF表示然后训练RP模型利用随机投影技术降低数据维数。对于未知的方法抛出ValueError异常。
7. 保存模型和变换后的语料
训练完相应模型后将其保存到指定的文件中例如model_lda.pkl或model_lsi.pkl。将原始语料经过所训练模型变换后得到的新语料即主题表示形式保存为一个新的MM格式文件文件名反映所使用的主题模型方法。
8.代码
#!/usr/bin/env python
#
# Copyright (C) 2010 Radim Rehurek radimrehurekseznam.cz
# Licensed under the GNU LGPL v2.1 - https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
USAGE: %(program)s LANGUAGE METHODGenerate topic models for the specified subcorpus. METHOD is currently one \
of tfidf, lsi, lda, rp.Example: ./gensim_genmodel.py any lsi
import logging
import sys
import os.pathfrom gensim.corpora import dmlcorpus, MmCorpus
from gensim.models import lsimodel, ldamodel, tfidfmodel, rpmodelimport gensim_build# internal method parameters
DIM_RP 300 # dimensionality for random projections
DIM_LSI 200 # for lantent semantic indexing
DIM_LDA 100 # for latent dirichlet allocationif __name__ __main__:logging.basicConfig(format%(asctime)s : %(levelname)s : %(message)s)logging.root.setLevel(levellogging.INFO)logging.info(running %s, .join(sys.argv))program os.path.basename(sys.argv[0])# check and process input argumentsif len(sys.argv) 3:print(globals()[__doc__] % locals())sys.exit(1)language sys.argv[1]method sys.argv[2].strip().lower()logging.info(loading corpus mappings)config dmlcorpus.DmlConfig(%s_%s % (gensim_build.PREFIX, language),resultDirgensim_build.RESULT_DIR, acceptLangs[language])logging.info(loading word id mapping from %s, config.resultFile(wordids.txt))id2word dmlcorpus.DmlCorpus.loadDictionary(config.resultFile(wordids.txt))logging.info(loaded %i word ids, len(id2word))corpus MmCorpus(config.resultFile(bow.mm))if method tfidf:model tfidfmodel.TfidfModel(corpus, id2wordid2word, normalizeTrue)model.save(config.resultFile(model_tfidf.pkl))elif method lda:model ldamodel.LdaModel(corpus, id2wordid2word, num_topicsDIM_LDA)model.save(config.resultFile(model_lda.pkl))elif method lsi:# first, transform word counts to tf-idf weightstfidf tfidfmodel.TfidfModel(corpus, id2wordid2word, normalizeTrue)# then find the transformation from tf-idf to latent spacemodel lsimodel.LsiModel(tfidf[corpus], id2wordid2word, num_topicsDIM_LSI)model.save(config.resultFile(model_lsi.pkl))elif method rp:# first, transform word counts to tf-idf weightstfidf tfidfmodel.TfidfModel(corpus, id2wordid2word, normalizeTrue)# then find the transformation from tf-idf to latent spacemodel rpmodel.RpModel(tfidf[corpus], id2wordid2word, num_topicsDIM_RP)model.save(config.resultFile(model_rp.pkl))else:raise ValueError(unknown topic extraction method: %s % repr(method))MmCorpus.saveCorpus(config.resultFile(%s.mm % method), model[corpus])logging.info(finished running %s, program)