Gensim学习笔记-2-理解Gensim中的Corpus对象
2015-08-17 14:27
253 查看
所有corpora.xxxcorpus中的对象均继承接口
gensim.interfaces.CorpusABC
一个语料库对象(corpus)是一个可迭代的对象,每次迭代得到一篇文档(document)
一个document对象又是一个2元组的(fieldId,fueldValue)序列
不同的corpus有不同的格式和相应的处理方法,但都有继承于CorpusABC的
1 classmethod load(fname, mmap=None)
2 save(*args, **kwargs)
3 static save_corpus(fname, corpus, id2word=None, metadata=False)
方法
理解corpora.textcorpus
由Demo入手学习
在Python安装文件下查找site-packages\gensim\test
找到test_miislita.py 文件
打开后发现一个有趣的网站: http://www.miislita.com/
corpora.TextCorpus是一个抽象的语料库类,复写其get_texts方法即可构造一个能序列化为各种corpus对象的输入对象。
get_texts()
Iterate over the collection, yielding one document at a time. A document is a sequence of words (strings) that can be fed into Dictionary.doc2bow.
Override this function to match your input (parse input files, do any text preprocessing, lowercasing, tokenizing etc.). There will be no further preprocessing of the words coming out of this function.
在test_miislita.py 文件中把main函数改为
运行可知已经完成了MmCorpus的生成
gensim.interfaces.CorpusABC
一个语料库对象(corpus)是一个可迭代的对象,每次迭代得到一篇文档(document)
一个document对象又是一个2元组的(fieldId,fueldValue)序列
不同的corpus有不同的格式和相应的处理方法,但都有继承于CorpusABC的
1 classmethod load(fname, mmap=None)
2 save(*args, **kwargs)
3 static save_corpus(fname, corpus, id2word=None, metadata=False)
方法
理解corpora.textcorpus
由Demo入手学习
在Python安装文件下查找site-packages\gensim\test
找到test_miislita.py 文件
打开后发现一个有趣的网站: http://www.miislita.com/
corpora.TextCorpus是一个抽象的语料库类,复写其get_texts方法即可构造一个能序列化为各种corpus对象的输入对象。
get_texts()
Iterate over the collection, yielding one document at a time. A document is a sequence of words (strings) that can be fed into Dictionary.doc2bow.
Override this function to match your input (parse input files, do any text preprocessing, lowercasing, tokenizing etc.). There will be no further preprocessing of the words coming out of this function.
在test_miislita.py 文件中把main函数改为
if __name__ == '__main__': #logging.basicConfig(level=logging.DEBUG) #unittest.main() miislita = CorpusMiislita(datapath('miIslita.cor')) # make sure serializing works ftmp = get_tmpfile('test_textcorpus.mm') corpora.MmCorpus.save_corpus(ftmp, miislita) # make sure deserializing gives the same result miislita2 = corpora.MmCorpus(ftmp) print miislita2 for doc in miislita2: print doc
运行可知已经完成了MmCorpus的生成
相关文章推荐
- JSON入门指南
- 天气接口
- 通过ID查找元素
- oc 目录结构
- Oracle自定义函数示例
- zabbix监控mysql多实例和mariadb自定义脚本及其部署
- SQL ODER BY 多个字段
- Java, 基础(面试题)总结(分享-交流)
- STM32系列ARM单片机介绍
- 关于hp proliant sl210t服务器远程iLO接口的管理配置
- php的CURL简单实例
- js为字符串编码
- C# 模仿QQ右下角 消息闪烁提示
- CSDN学院 免费技术答疑公开课,本周三场即将开播~~~
- HDU 1401 Solitaire (双向搜索)
- Ruby正则中文汉字
- 外部中断的使用
- iOS- 利用AFNetworking(AFN) - 实现图片上传
- C#项目中Json字符串解析
- Lowest Common Ancestor of a Binary Search Tree