python自然语言处理第五章习题

3.分词和标注下面的句子:They wind back the clock,while we chase after the wind.句子中包含哪些不同的发音和词类?

import nltk
s='They wind back the clock,while we chase after the wind'
s1=nltk.word_tokenize(s)
s2=nltk.pos_tag(s1) #list类型

4.字典相关练习

d={'colorless':'ADJ','idea':'N','sleep':'V'} #create a dict
del d['sleep'] #delete an item from dict d
d.update({'color':'N'}) #add new item into dict d
d2=nltk.defaultdict(list)
d2['N'].append('color') #add new word for type N

9.验证go和went在分布上的限制,也就是说,它们不能自由互换。

from nltk.book import *
text1.concordance('go')
text1.concordance('went')

10.训练一个unigram标注器,在一些新的文本上运行。观察没有分配到标记的词。为什么没有分配到标记?一元标注器的行为与查找

from nltk.corpus import brown

brown_tagged_sents=brown.tagged_sents(categories='news')

brown_sents=brown.sents(categories='news')

unigram_tagger=nltk.UnigramTagger(brown_sents)

unigram_tagger.tag(brown_sents[2007])

unigram_tagger.evaluate(brown_tagged_sents)

11.了解词缀标注器(输入help(nltk.AffixTagger)。训练一个词缀标注器,在一些新的文本上运行。设置不同的词缀长度和最小词长。并讨论结果。

import nltk

from nltk.corpus import brown

brown_sents=brown.sents(categories='news')

brown_tagged_sents=brown.tagged_sents(categories='news')

affixtagger=nltk.AffixTagger(brown_tagged_sents)

affistagger.tag(brown_sents[2007])

12.训练一个没有回退标注器的bigram标注器。在一些训练数据上运行。然后,在一些新的数据上运行它。标注器的性能会发生什么变化?为什么?

import nltk

from nltk.corpus import brown

brown_sents=brown.sents(categories='news')

brown_tagged_sents=brown.tagged_sents(categories='news')

bigram_tagger=nltk.BigramTagger(brown_tagged_sents)

bigram_tagger.tag(brown_sents[2007])

bigram_tagger.evaluate(brown_tagged_sents)

13.我们可以使用字典指定由一个格式化字符串替换的值。阅读关于格式化字符串的python文档,使用这种方法以两种不同格式显示今天的日期。

d={'year':2016,'month':8,'day':15}

print %s-%s-%s %( d['year'],d['month'],d['day'])

14.使用sorted()和set()获得布朗语料库使用的标记排序链表,删除重复。

sorted(set(brown.word(categories='news')))

15.编写程序处理布朗语料,找到一下答案。 
a.哪些名词经常以它们的复数形式出现而不是它们的单数形式?

你可能感兴趣的:(Python)