当前位置:   article > 正文

python课本第二章答案_《Python自然语言处理》答案第一、二章

python自然语言处理课后答案

第一章

112/(4+1)

226**100

4len(text2)

len(set(text2))

7len(list(nltk.bigrams(text5)))

15[w for w in sorted(text5) if w.startswith('b')]

17def find_word(text,word):

...: pos=0

...: while pos

...: try:

...: pos=text.index(word,pos)+1

...: print(pos)

...: except Exception as e:

...: print('all have bean found!')

...: return

...:

find_word(list(text9),'sunset')

22fd=FreqDist(text5)

[w for (w,_) in fd.most_common() if len(w)==4]

23[w for w in text6 if w.isupper()]

24[w for w in list(text6) if w.endswith('ize') and w.find('pt')!=-1 and w[0].isupper() and w[1:].islower()]

25[w for w in sent if w .startswith('sh')]

[w for w in sent if len(w)>4]

28def percent(word,text):

fd=FreqDist(text)

return '{}%'.format((fd[word])*100/len(text))

第二章

2persusion==nltk.Text(nltk.corpus.gutenberg.words('austen-persuasion.txt'))

len(persusion)

len(set(persusion))

4cfd=ConditionalFreqDist((target,fileid[:4]) for fileid in state_union.fileids() for word in

state_union.words(fileid) for target in ['men','women','people'] if target == word.lower()

)

8male_names=names.words('male.txt')

female_names=names.words('female.txt')

fd_male=nltk.FreqDist(male_names)

fd_female=nltk.FreqDist(female_names)

cfd=nltk.ConditionalFreqDist((fd_male[name],name[0])

for fileid in names.fileids()

for name in names.words(fileid)

if fd_male[name]>fd_female[name])

12len(set(w for (w,p) in cmudict.entries()))

fd=FreqDist([len(pron) for (word,pron) in cmudict.entries()])

fd.most_common()[0][1]/len(cmudict.entries())

15fd=FreqDist(brown.words())

[w for (w,_) in fd.most_common() if fd[w]>3]

16def word_diversity(words):

...: return len(words)/len(set(words))

for category in brown.categories():

...: diversity=word_diversity(brown.words(categories=category))

...: print('%s\t%.2f'%(category,diversity))

17def fun(text):

fd=FreqDist([w.lower() for w in text if w not in stopwords.words('english')])

return [w for (w,_) in fd.most_common()[:50]]

18def fun(text):

...: fd=FreqDist([(w1,w2) for (w1,w2) in bigrams(text) if w1 not in stopwords.words('english') and w2 not in stopwords.words('english')])

...: return [w for w in fd.most_common()[:50]]

20def word_freq(text,word):

...: count=nltk.Text(text).count(word)

...: return count/len(text)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/521713
推荐阅读
相关标签
  

闽ICP备14008679号