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

好的,这是一个很好的文本分类任务,可以使用Python中的NLTK库和sklearn库来完成。下面是一个基本的代码框架,你可以根据需要进行修改和优化。 首先,我们需要从NLTK库中导入电影评论数据集movie_reviews。 ```python import nltk nltk.download('movie_reviews') from nltk.corpus import movie_reviews ``` 接下来,我们需要将数据集分为训练集和测试集。可以采用随机划分的方法,将数据集的70%作为训练集,剩下的30%作为测试集。 ```python import random documents = [(list(movie_reviews.words(fileid)), category) for category in movie_reviews.categories() for fileid in movie_reviews.fileids(category)] random.shuffle(documents) split = int(len(documents) * 0.7) train_documents = documents[:split] test_documents = documents[split:] ``` 然后,我们需要从训练集中提取特征。可以使用NLTK库中的 `FreqDist()` 方法来计算词汇出现频率,并选取出现频率较高的词汇作为特征。 ```python all_words = [] for w in movie_reviews.words(): all_words.append(w.lower()) all_words = nltk.FreqDist(all_words) word_features = list(all_words.keys())[:3000] def document_features(document): document_words = set(document) features = {} for word in word_features: features[word] = (word in document_words) return features train_set = [(document_features(d), c) for (d,c) in train_documents] test_set = [(document_features(d), c) for (d,c) in test_documents] ``` 接着,我们可以使用sklearn库中的 `MultinomialNB()` 方法来训练分类器模型,并使用测试集数据测试识别效果。 ```python from sklearn.naive_bayes import MultinomialNB classifier = MultinomialNB().fit(train_set, train_labels) accuracy = nltk.classify.accuracy(classifier, test_set) ``` 以上是一个基本的代码框架,你可以根据需要进行修改和优化。需要注意的是,文本分类任务是一个相对复杂的问题,需要针对具体场景和数据进行分析和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值