NLTK(语料库)

本系列博客为学习《用Python进行自然语言处理》一书的学习笔记。
2.1 P41

一、古腾堡语料库

古腾堡语料库主要包含一些文学书籍。
先看一个例子,查看古腾堡语料库包含的文本名称:

import nltk

nltk.corpus.gutenberg.fileids()
Out[82]: 
[u'austen-emma.txt',
 u'austen-persuasion.txt',
 u'austen-sense.txt',
 u'bible-kjv.txt',
 u'blake-poems.txt',
 u'bryant-stories.txt',
 u'burgess-busterbrown.txt',
 u'carroll-alice.txt',
 u'chesterton-ball.txt',
 u'chesterton-brown.txt',
 u'chesterton-thursday.txt',
 u'edgeworth-parents.txt',
 u'melville-moby_dick.txt',
 u'milton-paradise.txt',
 u'shakespeare-caesar.txt',
 u'shakespeare-hamlet.txt',
 u'shakespeare-macbeth.txt',
 u'whitman-leaves.txt']
from nltk.corpus import gutenberg

gutenberg.fileids()
Out[84]: 
[u'austen-emma.txt',
 u'austen-persuasion.txt',
 u'austen-sense.txt',
 u'bible-kjv.txt',
 u'blake-poems.txt',
 u'bryant-stories.txt',
 u'burgess-busterbrown.txt',
 u'carroll-alice.txt',
 u'chesterton-ball.txt',
 u'chesterton-brown.txt',
 u'chesterton-thursday.txt',
 u'edgeworth-parents.txt',
 u'melville-moby_dick.txt',
 u'milton-paradise.txt',
 u'shakespeare-caesar.txt',
 u'shakespeare-hamlet.txt',
 u'shakespeare-macbeth.txt',
 u'whitman-leaves.txt']

utenberg是NLTK预先帮我们加载的语料库,我们可以把gutenberg看做是一个PlaintextCorpusReader对象。

PlaintextCorpusReader::fileids():该方法返回语料库中的文本标识列表。

PlaintextCorpusReader::words(fileids):该方法接受一个或多个文本标识作为参数,返回文本单词列表

emma = gutenberg.words("austen-emma.txt")

emma
Out[86]: [u'[', u'Emma', u'by', u'Jane', u'Austen', u'1816', ...]

PlaintextCorpusReader::raw(fileids):该方法接受一个或多个文本标识为参数,返回文本原始字符串。

emma = gutenberg.raw("austen-emma.txt")

emma
Out[88]: u'[Emma by Jane Austen 1816]\n\nVOLUME I\n\nCHAPTER I\n\n\nEmma... union.\n\n\nFINIS\n'

PlaintextCorpusReader::sents(fileids):该方法接受一个或多个文本标识为参数,返回文本中的句子列表。

emma_sents = gutenberg.sents("austen-emma.txt")

emma_sents 
Out[90]: [[u'[', u'Emma', u'by', u'Jane', u'Austen', u'1816', u']'], [u'VOLUME', u'I'], ...]
from nltk.corpus import gutenberg

for fileid in gutenberg.fileids():
    num_chars = len(gutenberg.raw(fileid))  #文本有多少字符
    num_Words = len(gutenberg.words(fileid))#文本有多少词
    num_sents = len(gutenberg.sents(fileid))#文本有多少句子
    num_vocab = len(set([w.lower() for w in gutenberg.words(fileid)]))
    print int(num_chars/num_Words), int(num_Words/num_sents),int(num_Words/num_vocab),fileid

平均词长、平均句子的长度、文本中每个词出现的平均次数

4 24 26 austen-emma.txt
4 26 16 austen-persuasion.txt
4 28 22 austen-sense.txt
4 33 79 bible-kjv.txt
4 19 5 blake-poems.txt
4 19 14 bryant-stories.txt
4 17 12 burgess-busterbrown.txt
4 20 12 carroll-alice.txt
4 20 11 chesterton-ball.txt
4 22 11 chesterton-brown.txt
4 18 10 chesterton-thursday.txt
4 20 24 edgeworth-parents.txt
4 25 15 melville-moby_dick.txt
4 52 10 milton-paradise.txt
4 11 8 shakespeare-caesar.txt
4 12 7 shakespeare-hamlet.txt
4 12 6 shakespeare-macbeth.txt
4 36 12 whitman-leaves.txt

二、网络文本语料库

网络文本语料库中包括火狐交流论坛、在纽约无意听到的话、加勒比海盗电影剧本、个人广告以及葡萄酒评论等等。
webtext同样可以看做是一个PlaintextCorpusReader对象。

from nltk.corpus import webtext

for f in webtext.fileids():
    print f,webtext.raw(f)[:20],'...'

firefox.txt Cookie Manager: "Don ...
grail.txt SCENE 1: [wind] [clo ...
overheard.txt White guy: So, do yo ...
pirates.txt PIRATES OF THE CARRI ...
singles.txt 25 SEXY MALE, seeks  ...
wine.txt Lovely delicate, fra ...

三、即时消息聊天会话语料库

语料库被分成15个文件,每个文件包含几百个按特定日期和特定年龄的聊天室收集的帖子,例如:10-19-20s_706posts.xml包含2006年10月19日从20多岁聊天室收集的706个帖子。

from nltk.corpus import nps_chat
for f in nps_chat.fileids():
    print f

10-19-20s_706posts.xml
10-19-30s_705posts.xml
10-19-40s_686posts.xml
10-19-adults_706posts.xml
10-24-40s_706posts.xml
10-26-teens_706posts.xml
11-06-adults_706posts.xml
11-08-20s_705posts.xml
11-08-40s_706posts.xml
11-08-adults_705posts.xml
11-08-teens_706posts.xml
11-09-20s_706posts.xml
11-09-40s_706posts.xml
11-09-adults_706posts.xml
11-09-teens_706posts.xml

nps_chat可以看做是一个NPSChatCorpusReader对象。

NPSChatCorpusReader::fileids():该方法返回语料库中的文本标识列表。

NPSChatCorpusReader::posts(fileids):该方法接受一个或多个文本标识作为参数,返回一个包含对话的列表,每一个对话又同时是单词的列表。

chat_room = nps_chat.posts('10-19-30s_705posts.xml')

print(chat_room)
[[u'U11', u'lol'], [u'lol', u'U11'], [u'wb', u'U9'], ...]

四、布朗语料库

布朗语料库是一个百万词级的英语电子语料库,这个语料库包含500个不同来源的文本,按照文体分类,如:新闻、社论等。我们可以先看看布朗语料库中包含哪些类别:

from nltk.corpus import brown

for f in brown.categories():
    print f

adventure
belles_lettres
editorial
fiction
government
hobbies
humor
learned
lore
mystery
news
religion
reviews
romance
science_fiction

brown可以看做是一个CategorizedTaggedCorpusReader对象。

CategorizedTaggedCorpusReader::categories():该方法返回语料库中的类别标识。

CategorizedTaggedCorpusReader::fileids(categories):该方法接受一个或多个类别标识作为参数,返回文本标识列表。

print brown.fileids(['news'])
[u'ca01', u'ca02', u'ca03', u'ca04', u'ca05', u'ca06', u'ca07', u'ca08', u'ca09', u'ca10', u'ca11', u'ca12', u'ca13', u'ca14', u'ca15', u'ca16', u'ca17', u'ca18', u'ca19', u'ca20', u'ca21', u'ca22', u'ca23', u'ca24', u'ca25', u'ca26', u'ca27', u'ca28', u'ca29', u'ca30', u'ca31', u'ca32', u'ca33', u'ca34', u'ca35', u'ca36', u'ca37', u'ca38', u'ca39', u'ca40', u'ca41', u'ca42', u'ca43', u'ca44']

CategorizedTaggedCorpusReader::words(fileids, categories):该方法接受文本标识或者类别标识作为参数,返回文本单词列表。

news = brown.words(categories='news')

print('news: ', news)
('news: ', [u'The', u'Fulton', u'County', u'Grand', u'Jury', ...])

ca02 = brown.words(fileids='ca02')

print('ca02: ', ca02)
('ca02: ', [u'Austin', u',', u'Texas', u'--', u'Committee', ...])

CategorizedTaggedCorpusReader::sents(fileids, categories):该方法接受文本标识或者类别标识作为参数,返回文本句子列表,句子本身是词列表。

五、路透社语料库

路透社语料库包含10,788个新闻文档,共计130万字。文档被分成了90个主题,按照训练和测试分为两组。路特社语料库中的类别是项目重叠的,因为新闻报道往往涉及多个主题。

reuters也可以看做是一个CategorizedTaggedCorpusReader对象。

六、就职演说语料库

该语料库是55个文本的集合,每个文本都是一个总统的演说。这个集合的一个显著特性是时间维度。

from nltk.corpus import inaugural

inaugural.fileids()
Out[112]: 
[u'1789-Washington.txt',
 u'1793-Washington.txt',
 u'1797-Adams.txt',
 u'1801-Jefferson.txt',
...
 u'2009-Obama.txt']

[f[:4] for f in inaugural.fileids()]
Out[113]: 
[u'1789',
 u'1793',
 u'1797',
 u'1801',
 u'1805',
 ...
 u'1997',
 u'2001',
 u'2005',
 u'2009']

inaugural同样可以看做是一个PlaintextCorpusReader对象。

载入自己的语料库

。。。未完

总结

gutenberg、webtext和inaugural是PlaintextCorpusReader的实例对象。

PlaintextCorpusReader成员方法:

fileids(),该方法返回语料库中的文本标识列表
words(fileids),该方法接受一个或多个文本标识作为参数,返回文本单词列表
raw(fileids),该方法接受一个或多个文本标识为参数,返回文本原始字符串
sents(fileids),该方法接受一个或多个文本标识为参数,返回文本中的句子列表

nps_chat是NPSChatCorpusReader的实例对象。

NPSChatCorpusReader成员方法:

fileids(),该方法返回语料库中的文本标识列表
posts(fileids),该方法接受一个或多个文本标识作为参数,返回一个包含对话的列表,每一个对话又同时是单词的列表

brown和reuters是CategorizedTaggedCorpusReader的实例对象。

CategorizedTaggedCorpusReader成员方法:

categories(),该方法返回语料库中的类别标识
fileids(categories),该方法接受一个或多个类别标识作为参数,返回文本标识列表
words(fileids, categories),该方法接受文本标识或者类别标识作为参数,返回文本单词列表
sents(fileids, categories),该方法接受文本标识或者类别标识作为参数,返回文本句子列表,句子本身是词列表

参考文献 http://www.coderjie.com/blog/0376c9eac69611e6841d00163e0c0e36

猜你喜欢

转载自blog.csdn.net/csdn_lzw/article/details/80394500