一,获取文本语料库
1,古腾堡语料库
古腾堡语料库主要存储的是免费的电子图书。
import nltk
from nltk.corpus import *
fileids = gutenberg.fileids();
text1 = gutenberg.words(fileids[0]);
len(text1)
# 如果要使用第一章中的concordance()等函数需要将text1转换为Text类型...
text1_1 = nltk.Text(text1);
text1_1.concordance('surprize');
# 常用操作,raw()函数给我们的是没有进行任何语言学处理的文件的内容
chars = gutenberg.raw(fileids[0]);
words = gutenberg.words(fileids[0]);
sents = gutenberg.sents(fileids[0]);
2,网络与聊天文件
webfiles = webtext.fileids();
chatroom = nps_chat.posts('1--19-20s_706posts.xml');
3,布朗语料库
from nltk.corpus import brown
ctgr = brown.categories();
fileids = brown.fileids();
words1 = brown.words(categories=ctgr[0]);
word2 = brown.words(fileids=fileids[0]);
sents = brown.sents(categories=['news','editorial','reviews']);