python自然语言处理——2.1 获取文本语料库

微信公众号:数据运营人
本系列为博主的读书学习笔记,如需转载请注明出处。

第二章 获取文本预料和词汇资源

2.1 获取文本语料库古腾堡语料库网络和聊天文本布朗语料库路透社语料库就职演说语料库标注文本语料库其他文本语料库文本语料库结构

2.1 获取文本语料库

一个文本语料库是一大段文本,许多语料库的设计都要考虑一个或多个文本间的平衡。比如就职演说语料库,这种特殊的语料库实际上包含几十个单独的文本——一个人一个演讲。

古腾堡语料库

NLTK包含古腾堡项目(http://www.gutenberg.org)电子文本档案的一小部分文本,该项目大约有36000本免费的电子图书。

 1import nltk
2#返回古腾堡语料库的所有文本名称
3print(nltk.corpus.gutenberg.fileids())
4# 将austen-emma.txt文本用分割成单词
5emma = nltk.corpus.gutenberg.words('austen-emma.txt')
6print(emma)
7
8for fileids in gutenberg.fileids():
9    # 文本中的原始字母,包含空格的数量
10    num_chars = len(gutenberg.raw(fileids))
11    # 文本中的单词的数量
12    num_words = len(gutenberg.words(fileids))
13    # 文本中的句子的数量
14    num_sents = len(gutenberg.sents(fileids))
15    # 将文本中的单词全部转换为小写,并去重
16    num_vocab = len(set([w.lower() for w in gutenberg.words(fileids)]))    print(int(num_chars/num_words),int(num_words/num_sents),int(num_words/num_vocab),fileids)
17macbeth_sentences = gutenberg.sents('shakespeare-macbeth.txt')
18print(macbeth_sentences)
19print(macbeth_sentences[0])
20longest_len = max([len(s) for s in macbeth_sentences])
21print([s for s in macbeth_sentences if len(s) == longest_len])

返回结果:

网络和聊天文本

网络聊天中的文本包括Firefox论坛/在纽约无意听到的对话/《加勒比海盗》的电影剧本/个人广告/葡萄酒评论。

1from nltk.corpus import webtext
2for fileid in webtext.fileids():
3    print(fileids,webtext.raw(fileid)[:50])

返回结果:


一个即时消息聊天会话语料库,最初由美国海军研究生院为研究自动检测互联网幼童虐待癖而收集的。语料库包含超过 10,000 张帖子,以“UserNNN”形式的通用名替换掉 用户名,手工编辑消除任何其他身份信息,制作而成。语料库被分成 15 个文件,每个文件包含几百个按特定日期和特定年龄的聊天室(青少年、20 岁、30 岁、40 岁、再加上一个通 用的成年人聊天室)收集的帖子。文件名中包含日期、聊天室和帖子数量,例如: 10-19-20s_706posts.xml 包含 2006 年 10 月 19 日从 20 多岁聊天室收集的 706 个帖子。

1from nltk.corpus import nps_chat
2chatroom = nps_chat.posts('10-19-20s_706posts.xml')
3print(chatroom[123])

返回结果:

布朗语料库

布朗语料库是第一个百万词级的英语电子语料库的,由布朗大学于 1961 年创建。这个 语料库包含 500 个不同来源的文本,按照文体分类,如:新闻、社论等。

布朗语料库每一部分的示例文档
布朗语料库每一部分的示例文档
 1from nltk.corpus import brown
2print(brown.categories())
3new_text = brown.words(categories='news')
4fdist = nltk.FreqDist([w.lower() for w in new_text])
5modals = ['can','could','may','might','must','will']
6for m in modals:
7    print(m+':',fdist[m])
8cfd = nltk.ConditionalFreqDist(
9        (genre,word)
10        for genre in brown.categories()
11        for word in brown.words(categories=genre))
12genres = ['news''religion''hobbies''science_fiction''romance''humor']
13modals = ['can''could''may''might''must''will']
14cfd.tabulate(conditions=genres, samples=modals)

返回结果:

路透社语料库

路透社语料库包含10788个新闻文档,共计130万字。这些文档分成90个主题,按照“训练”和“测试”分为两组。因此,fileid 为“test/14826”的文档属于测试组。这样分割 是为了训练和测试算法的,这种算法自动检测文档的主题.

1from nltk.corpus import reuters
2print(reuters.fileids())                               # 返回文件名
3print(reuters.categories())                         # 返回类别名         
4print(reuters.categories('training/9865'))
5print(reuters.categories(['training/9865''training/9880']))
6print(reuters.fileids('barley'))                     # 返回barley类别的文件名
7print(reuters.fileids(['barley','corn']))       
8print(reuters.words('training/9865')[:14])     # 返回training/9865类别的前14个字符
9print(reuters.fileids(categories='barley'))
就职演说语料库
 1from nltk.corpus import inaugural
2print(inaugural.fileids())
3print([fileid[:4for fileid in inaugural.fileids()])
4cfd = nltk.ConditionalFreqDist(                   
5        (target,fileid[:4])                       
6    for fileid in inaugural.fileids()             
7    for w in inaugural.words(fileid)              
8    for target in ["america","citizen"]           
9    if w.lower().startswith(target))              
10print(cfd.plot())      

返回结果:


标注文本语料库

网址:http://www.nltk.org/howto

其他文本语料库
 1print(nltk.corpus.cess_esp.words())                    
2print(nltk.corpus.floresta.words())                    
3print(nltk.corpus.indian.words("hindi.pos"))           
4print(nltk.corpus.udhr.fileids())                      
5# udhr是超过300多种语言的世界人权宣言                                                                                     
6from nltk.corpus import udhr                           
7languages = ["Chickasaw","English","German_Deutsch"]   
8cfd = nltk.ConditionalFreqDist(                        
9        (lang,len(word))                               
10         for lang in languages                         
11         for word in udhr.words(lang + '-Latin1'))     
12print(cfd.plot(cumulative = True))                     

返回结果:


文本语料库结构

查看帮助:help(nltk.corpus.reader)

1# raws words sentence区分                                  
2raw = gutenberg.raw("burgess-busterbrown.txt")         
3print(raw[1:20])                                       
4words = gutenberg.words("burgess-busterbrown.txt")     
5print(words[1:20])                                                                                             
6sents = gutenberg.sents("burgess-busterbrown.txt")     
7print(sents[1:20])       

返回结果:

原文地址:https://www.cnblogs.com/ly803744/p/10082862.html