《使用Python进行自然语言处理》学习笔记三
2014-03-12 15:58
393 查看
第二章 获得文本语料和词汇资源
2.1 获取文本语料库
1 古腾堡语料库
Project Gutenberg的语料库包含>>>import nltk
>>>from nltk.corpus import gutenberg
>>>gutenberg.fileids()
['austen-emma.txt','austen-persuasion.txt', 'austen-sense.txt', 'bible-kjv.txt','blake-poems.txt','bryant-stories.txt', 'burgess-busterbrown.txt','carroll-alice.txt','chesterton-ball.txt', 'chesterton-brown.txt', 'chesterton-thursday.txt','edgeworth-parents.txt',
'melville-moby_dick.txt','milton-paradise.txt','shakespeare-caesar.txt', 'shakespeare-hamlet.txt',
'shakespeare-macbeth.txt','whitman-leaves.txt’]
调用使用:
>>>emma = nltk.corpus.gutenberg.words('austen-emma.txt')
#num_chars 变量计数了空白字符
#row()对文件的内容不进行任何语言处理
#sents()函数把文本划分成句子,其中每一个句子是一个词链表
非正规文本语料库
>>> from nltk.corpus import webtext
>>> for fileid in webtext.fileids():
即时消息聊天会话语料库
>>> from nltk.corpus import nps_chat
>>> chatroom =nps_chat.posts('10-19-20s_706posts.xml')
>>> chatroom[123]
2 布朗语料库
布朗语料库是第一个百万词级的英语电子语料库的,由布朗大学于 1961 年创建。这个语料库包含 500 个不同来源的文本,按照文体分类,如:新闻、社论等。(详情P58)见代码NltkTest59(建议,如果电脑配置有限在执行书上的指令时需要注意,有的书里打了省略号的地方如取得fileids的时候,其实内容特别多,需要检查的目录也特别多,可能会需要很长的等待时间)
3 路透社语料库
路透社语料库包含 10,788 个新闻文档,共计 130 万字。这些文档分成 90 个主题,按照“训练”和“测试”分为两组。与布朗语料库不同,路透社语料库的类别是有互相重叠的,只是因为新闻报道往往涉及多个主题。我们可以查找由一个或多个文档涵盖的主题,也可以查找包含在一个或多个类别中的文档。4 就职演说语料库
语料库实际上是 55 个文本的集合,每个文本都是一个总统的演说。这个集合的一个有趣特性是它的时间维度。这里书上提供的程序时无法正常运行的,因为运行出错,出这个问题的原因是BabelfishChangedError Thrown when babelfish.yahoo.com changes somedetail of their HTML layout, and babelizer no longer submits data in thecorrect form, or can no longer parse the results”。现在里面调用的是MicrosoftTranslator
web service (SOAP),和书里用的版本不一致。可以通过查询当前版本的文档进行调用。参考解决方案:http://stackoverflow.com/questions/12267544/machine-translation-using-babelize-shell-in-nltk
目前没能解决,好可惜啊,一个有意思的图没能画出来。不过也说明一个问题,Windows做开发有的时候确实是不给力啊,真正开始做系统的时候还是linux吧。
5 标注文本语料库
NLTK 包含多国语言语料库。“世界人权宣言” (udhr )语料库中不同语言版本中的字长差异。cumulative=True时
cumulative=False时
好吧,我觉得False时比较容易看懂,True时看着比较高端。
6 文本语料库的结构
NLTK 语料库阅读器支持高效的访问大量语料库,并且能用于处理新的语料库。NLTK 中定义的基本语料库函数。P657 载入你自己的语料库
(1) 将变量corpus_root的值设置为自己的语料的文件夹目录(2) PlaintextCorpusReader 初始化函数的第二个参数可以是需要加载的文件,可以使用正则表达式
(3) CropsBPCRTest导入txt一类的数据很顺利,但是BracketParseCorpusReader载入宾州树库的实验是失败的,我看了自己的宾州树库语料库与书中描述的不一致但是不应该啊,还没有找到原因,先留下这个悬案,待日后再审。
#!/usr/python/bin #Filename:NltkTest59,一些关于语料库使用的测试 importnltk fromnltk.corpus import brown fromnltk.corpus import reuters fromnltk.corpus import inaugural fromnltk.corpus import udhr fromnltk.corpus import BracketParseCorpusReader fromnltk.corpus import PlaintextCorpusReader classNltkTest59: def __init__(self): print 'Initing...' def BrownTest(self,genres,modals): '''来源于p59,对于不同问题的常用词统计的测试''' cfd = nltk.ConditionalFreqDist(\ (genre, word)\ for genre in brown.categories()\ for word inbrown.words(categories=genre)) cfd.tabulate(conditions=genres,samples=modals) def ReutersTest(self): # reuters.fileids() # reuters.categories() printreuters.categories('training/9865') printreuters.categories(['training/9865', 'training/9880']) # reuters.fileids('barley') # reuters.fileids(['barley', 'corn']) print reuters.words('training/9865')[:14] print reuters.words(['training/9865','training/9880']) printreuters.words(categories='barley') printreuters.words(categories=['barley', 'corn']) def InauguralTest(self): '''运行会出错 ''' cfd=nltk.ConditionalFreqDist(\ (target, file[:4])\ for fileids in inaugural.fileids()\ for w in inaugural.words(fileids)\ for target in ['america','citizen']\ if w.lower().startswith(target)) cfd.plot() def UdhrTest(self): languages = ['Chickasaw', 'English','German_Deutsch',\ 'Greenlandic_Inuktikut','Hungarian_Magyar', 'Ibibio_Efik'] cfd=nltk.ConditionalFreqDist(\ (lang, len(word))\ for lang in languages\ for word in udhr.words(lang +'-Latin1')) cfd.plot(cumulative=False) def CropsPCRTest(self): corpus_root=r'C:\corpora\udhr2' file_pattern=r'.*' encoding='utf-8' pcr=PlaintextCorpusReader(corpus_root,file_pattern) print pcr.fileids() print pcr.words('007.txt') def CropsBPCRTest(self): '''可耻的失败了,没法运行,可能是语料库版本问题,或者其他,待查''' corpus_root=r'C:\corpora\penntreebank\parsed' file_pattern=r'*.wsj' ptb=BracketParseCorpusReader(corpus_root, file_pattern) print ptb.fileids() print len(ptb.sents()) ptb.sents(fileids='\wsj_0001.mrg')[19] nt59=NltkTest59() genres= ['news', 'religion', 'hobbies', 'science_fiction', 'romance', 'humor'] modals= ['can', 'could', 'may', 'might', 'must', 'will'] #nt59.BrownTest(genres,modals) #nt59.ReutersTest() #不能运行,因为存在SOAP版本错误,不急着解决,以后再补一个解决方案 #nt59.InauguralTest() nt59.UdhrTest() #nt59.CropsPCRTest() #nt59.CropsBPCRTest()
相关文章推荐
- 《使用Python进行自然语言处理》学习笔记三
- 《使用Python进行自然语言处理》学习笔记三
- 《使用Python进行自然语言处理》学习笔记六
- 《使用Python进行自然语言处理》学习笔记一
- 《使用Python进行自然语言处理》学习笔记四
- Python编程使用NLTK进行自然语言处理详解
- 《使用Python进行自然语言处理》学习笔记四
- 《使用Python进行自然语言处理》学习笔记七
- 《使用Python进行自然语言处理》学习笔记五
- 《使用Python进行自然语言处理》学习笔记七
- 《使用Python进行自然语言处理》学习笔记八
- 《使用Python进行自然语言处理》学习笔记八
- 《使用Python进行自然语言处理》学习笔记一
- 《使用Python进行自然语言处理》学习笔记四
- 《使用Python进行自然语言处理》学习笔记五
- Python 使用nltk对数据进行自然语言处理(nlp)
- 《使用Python进行自然语言处理》学习笔记一
- 《使用Python进行自然语言处理》学习笔记一
- 《使用Python进行自然语言处理》学习笔记六
- Python 爬虫(以赛马数据为例)之使用BeautifulSoup进行Html解析