NLTK(语料库)

本文介绍了使用Python进行自然语言处理时常用的多种语料库,包括古腾堡语料库、网络文本语料库、即时消息聊天会话语料库、布朗语料库、路透社语料库和就职演说语料库。通过这些语料库,读者可以了解如何加载和处理文本数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本系列博客为学习《用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

### ZZULIOJ Java 编程题解及资源 #### 题目解析与解决方案 对于ZZULIOJ平台上关于Java编程的问题,通常涉及基础算法、数据结构以及特定功能实现。下面是一个典型的例子,展示了如何处理学生信息查询问题。 假设有一个题目要求根据给定的学生列表(包括学号、姓名和三门课程的成绩),当用户提供一个学号时,程序应返回对应的完整记录;如果找不到匹配的学号,则输出"Not Found"。这个问题可以通过创建一个`Student`类来简化操作,并利用哈希表快速定位目标条目[^3]。 ```java import java.util.*; class Student { String id; String name; List<Integer> scores; public Student(String id, String name, List<Integer> scores) { this.id = id; this.name = name; this.scores = scores; } } public class Main { private static final Map<String, Student> studentMap = new HashMap<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 假设这里有多组测试用例输入 while (scanner.hasNext()) { int numberOfStudents = Integer.parseInt(scanner.nextLine()); for (int i = 0; i < numberOfStudents; ++i) { String line = scanner.nextLine(); String[] parts = line.split(" "); String id = parts[0]; String name = parts[1]; List<Integer> scores = Arrays.stream(parts).skip(2).mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()); studentMap.put(id, new Student(id, name, scores)); } String queryId = scanner.nextLine(); if (studentMap.containsKey(queryId)) { Student foundStudent = studentMap.get(queryId); System.out.println(foundStudent.id + " " + foundStudent.name + " " + String.join(" ", foundStudent.scores.stream().map(Object::toString).toArray(String[]::new))); } else { System.out.println("Not Found"); } } } } ``` 此代码片段定义了一个名为`Main`的应用程序入口点,在其中读取多行输入直到遇到文件结束标志EOF。每行代表一位学生的详细资料,之后再接收一次单独的ID作为查询条件。通过构建映射关系使得每次查找都能高效完成。 #### 资源推荐 为了更好地准备类似的竞赛或练习,建议访问官方文档和其他在线教程以加深理解: - **Oracle 官方文档**: 提供最权威的语言特性和API说明。 - **LeetCode 和 Codeforces**: 这两个网站提供了丰富的编程挑战赛题库,有助于提高解决问题的能力。 - **GitHub 上开源项目**: 学习他人编写的高质量代码可以极大地提升自己的技术水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值