python之获得文本语料和词汇资源(2)

2.1 获取文本语料库
2.1.1古腾堡语料库

import nltk
print(nltk.corpus.gutenberg.fileids())

#简·奥斯丁的《艾玛》–emma,找出它包含多少词

emma = nltk.corpus.gutenberg.words('austen-emma.txt')
print(len(emma))

#‘austen-emma.txt’ 该文本中surprize的上下文

emma = nltk.Text(nltk.corpus.gutenberg.words('austen-emma.txt'))
print(emma.concordance('surprize'))

#直接调用古腾堡语料库中的文件,就不用每次输入了

nltk.corpus.gutenberg.fileids
from nltk.corpus import gutenberg
print(gutenberg.fileids())
emma = gutenberg.words('austen-emma.txt')
print(emma)

循环遍历列出与gutenberg文件标识符相应的fileid。然后计算统计每个文本,其中raw()函数能在没有进行过任何语言学处理之前把文件的内容分析出来。

for fileid in gutenberg.fileids():
	num_chars = len(gutenberg.raw(fileid))
	num_words = len(gutenberg.words(fileid))
	num_sents = len(gutenberg.sents(fileid))
	print(int(num_chars/num_words),int(num_words/num_sents),int(num_words/num_vocab),fileid)
	#分别是平均词长、平均句子长度、本文中每个词出现的平均次数

raw函数的例子

```python
print(len(gutenberg.raw('blake-poems.txt')))
#告诉我们文本中出现的词汇个数,包括词之间的空格

sents()函数把文本划分成句子,其中每一个句子是一个词链表

macbeth_sentences = gutenberg.sents('shakesprare-macbeth.txt')
print(macbeth_sentences) #输出该文本的所有句子
print(macbeth_sentences[1037]) #输出切分句子位置索引为1037的那一句。
longest_len = max([len(s) for s in macbeth_sentences])
print(s for s in macbeth_sentences if len(s) == longest_len]) #找到文件中最长的句子的长度,并输出该句子

2.1.2 网络和聊天文本

from nltk.corpus import webtext
for fileid in webtext.fileids():
	print(fileid,webtext.raw(fileid)[:65])
#输出在webtext库中的原始文件内容的前64号索引位置的内容

#处理即时消息聊天会话语料库

from nltk.corpus import nps_chat
chatroom = nps_chat.posts('10-19-20s_706posts.xml') #10月19号从20多岁聊天室收集的706个帖子
print(chatroom[123]) #处于123号位置索引的帖子

2.1.3 布朗语料库
该语料库包含500个不同来源的文本,按照文本分类,如新闻、社论等。

from nltk.corpus import brown
print(brown.categries()) #输出该语料库的类别
print(brown.words(categories = 'news') #输出类别为news中的词
print(brown.words(fileids = ['cg22'])) #输出文件名为cg22中的词
print(brown.sents(categories = ['news','editorial','reviews'] #输出类别为'news','editorial','reviews‘中的句子

布朗语料库是一个研究文体之间的系统性差异(又叫文体学的语言学研究)的资源,比较不同文本中的情态动词的用法。

import nltk
from nltk.corpus import brown
news_text = brown.words(categories = 'news')
fdist = nltk.FreqDist(w.lower for w in news_text])
modals = ['can','could','may','might','must','will']
from m in modals:
	print(m + ':' ,fdist[m],)

#输出情态动词在文中使用的频率

条件频率

cfd = nltk.ConditionalFreqDist(
	(genre,word)
	for genre in brown.categories()  #体裁属于布朗语料库
	for word in brown.words(categories = genre)) #单词属于该体裁中的单词
genres = ['news','religion','hobbies','science_fiction','romance','humor'] #要求的体裁
modals = ['can','could','may','might','must','will']#要求的单词
cfd.tabulate(conditions = genres,samples = modals) #在该体裁的条件下,样本为modals中单词,制作分布表

2.1.4路透社语料库

from nltk.corpus import retuers
print(retuers.fileids())
print(retuers.categories())

路透社语料库的类别是互相重叠的,因为新闻报道往往涉及多个主题。我们可以查找由一个或多个文档涵盖的主题,也可以查找包含在一个或多个类别中的文档。

print(retuers.categorise('training/9880'))
print(retuers.categories([training/9865','training/9880'])
print(retuers.fileids('barley'))
print(retures.fileids(['barley','corn']))
print(reuters.words('training/9865')[:14])
print(reuters.words(['training/9865','training/9880']))
print(reuters.words(categories = 'barley'))
print(reuters.words(categories = ['barley','corn']))

2.1.5就职演说语料库

import nltk
from nltk.corpus import inaugural
print(inaugural.fileids())

#词汇america和citizen随时间推移的使用情况
cfd = nltk.ConditionalFreqDist(
	(target,file[:4])
	for target in inaugural.fileids()
	for w in inaugurak.words(fileid)
	for target in ['america','citizen']
	if w.lower().startwith(target))
cdf.plot

2.1.6标注文本语料库
2.1.7其他语言的语料库
2.1.8载入你自己的语料库

from nltk.corpus import PlaintextCorpusReader
corpus_root = r'C:\Users\admin\Desktop\1'
wordlists = PlaintextCorpusReader(corpus_root,'.*')
print(wordlists.fileids())