您的位置:首页 > 编程语言 > Python开发

《python自然语言处理》第二章 获得文本预料和词汇资源

2018-01-30 15:08 561 查看
from nltk.corpus import gutenberg
gutenberg.fileids()#加载古滕堡语料库
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)
#平均词长、平均句子长度、本文中每个词出现的平均次数

1、raw()函数返回没有进行处理的文件内容;sents()函数将文本划分成句子,每一个句子是一个词链表(一篇文章就由链表的链表组成)

macbeth_sentences = gutenberg.sents('shakespeare-macbeth.txt')
macbeth_sentences[1037]#返回一个句子,即一个词链表
longest_len = max([len(s) for s in macbeth_sentences])#最长句子的句子长度
[s for s in macbeth_sentences if len(s)==longest_len]#返回所有最长句子组成的链表
from nltk.corpus import webtext#网络文本小集合
from nltk.corpus import nps_chat#即使消息聊天会话语料库
from nltk.corpus import brown#布朗语料库,不同类型的文本,用于研究不同文体中的情态动词的用法
brown.categories()
news_text = brown.words(categories='news')
fdist = nltk.FreqDist([w.lower() for w in news_text])#返回每个词的词频
modals = ['can','could','may','might','must']
for m in modals:
print(m + ':',fdist[m])#输出modals中每个词的词频



.条件频率分布是频率分布的集合,每个频率分布有一个不同的条件,通常是文本的类别

#条件频率分布
from nltk.corpus import inaugural
inaugural.fileids()
[fileid[:4] for fileid in inaugural.fileids()]
cfd = nltk.ConditionalFreqDist(
(target, fileid[:4])
for fileid in inaugural.fileids()
for w in inaugural.words(fileid)
for target in ['america', 'citizen']
if w.lower().startswith(target)#以文件中小写形式为america和citizen开头
)
cfd.plot()
from nltk.corpus import udhr
languages = ['English', 'German_Deutsch','Greenlandic_Inuktikut']
cfd = nltk.ConditionalFreqDist(
(lang, len(word))
for lang in languages
for word in udhr.words(lang+'-Latin1')#每一种语言的文件名称后面跟-latin
)
cfd.tabulate(conditions=['English', 'German_Deutsch'],
samples=range(10),cumulative=True)
#conditions=parameter指定哪些条件显示,sample=parameter限制要显示的样本
cfd = nltk.ConditionalFreqDist(
(genre, word)#以文体和词汇配对输入
for genre in brown.categories()#布朗语料库中的每一体裁
for word in brown.words(categories=genre)#每个题材中的每个词汇
)
genre_word = [(genre, word)
for genre in ['news', 'romance']#限定只看新闻和言情两个文体
for word in brown.words(catefories=genre)]
print(genre_word)
print(genre_word[:4])#观察配对形式
print(genre_word[-4:])
cfd = nltk.ConditionalFreqDist(genre_word)#有两个条件
#使用双连词生成随机文本
sent = ['In', 'the', 'beginning', 'God', 'created']
nltk.bigrams(sent)#词汇链表作为输入,输出一个词对链表
def generate_model(cfdist, word, num=15):#生成文本,word为初始选定词汇
for i in range(num):
print(word),#输出word当前值
word = cfdist[word].max()#重新设置word为上下文中最可能的标识符
text = nltk.corpus.genesis.words('english-kjv.txt')
bigrams = nltk.bigrams(text)
cfd = nltk.ConditionalFreqDist(bigrams)
print(cfd['living'])#输出与living配对的词汇及频率
generate_model(cfd, 'living')



3.在一个文件中的变量和函数定义的集合被称为一个python模块,模块的集合成为一个包,包的集合成为库
#词典资源
from nltk.corpus import stopwords#停用词语料库
stopwords.words('english')
#计算没有在停用词列表中德词的比例
def content_fraction(text):
stopwords = nltk.corpus.stopwords.words('english')
content = [w for w in text if w.lower() not in stopwords]
return len(content)/len(text)
content_fraction(nltk.corpus.reuters.words())
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: