python输入两个数字的成语_python自然语言处理学习笔记第一章第二部分 | 学步园...

>>> sent1=['fdasdf','fdgdf','qwfdge','dfeger']

>>> for xyzzy in sent1:

if xyzzy.endswith('e'):

print xyzzy

qwfdge

>>> tricky = sorted([w for w in set(text2) if 'cie' in w or 'cei' in w])

>>> for word in tricky:

print word,

ancient ceiling conceit conceited conceive conscience conscientious conscientiously deceitful deceive deceived deceiving deficiencies deficiency deficient delicacies excellencies fancied insufficiency insufficient legacies perceive perceived perceiving prescience prophecies receipt receive received receiving society species sufficient sufficiently undeceive undeceiving

第一张习题

1尝试使用Python 解释器作为一个计算器,输入表达式,如12/(4+1)。

>>> from __future__ import division

>>> 12/(4+1)

2.4

>>>

2. ○26 个字母可以组成26 的10 次方或者26**10 个10 字母长的字符串。也就是1411

67095653376L(结尾处的L 只是表示这是Python 长数字格式)。100 个字母长度的

字符串可能有多少个?

>>> 26**100

3142930641582938830174357788501626427282669988762475256374173175398995908420104023465432599069702289330964075081611719197835869803511992549376L

3. ○Python 乘法运算可应用于链表。当你输入['Monty', 'Python'] * 20 或者3 * se

nt1 会发生什么?

>>> ['Monty','python']*20

['Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty',

'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python', 'Monty', 'python']

>>> 3*sent1

['fdasdf', 'fdgdf', 'qwfdge', 'dfeger', 'fdasdf', 'fdgdf', 'qwfdge', 'dfeger', 'fdasdf', 'fdgdf', 'qwfdge', 'dfeger']

4. ○复习1.1 节关于语言计算的内容。在text2 中有多少个词?有多少个不同的词?

>>> len(text2)

141576

>>> len(set(text2))

6833

5. ○比较表格1-1 中幽默和言情小说的词汇多样性得分,哪一个文体中词汇更丰富?

romance fiction

6. ○制作《理智与情感》中四个主角:Elinor,Marianne,Edward 和Willoughby 的分布图。

在这部小说中关于男性和女性所扮演的不同角色,你能观察到什么?你能找出一对夫妻

吗?

>>> text2.dispersion_plot(["Elinor", "Marianne", "Edward", "Willoughby"])

Elinor和Marianne是女一和女二号,Edward和Willoughby应该就属于男一,男二。其中E对E,M对W一共两对,推测这是部小说应该是主要讲两个女主人公的。

7. ○查找text5 中的搭配。

>>> text5.collocations()

Building collocations list

wanna chat; PART JOIN; MODE #14-19teens; JOIN PART; cute.-ass MP3; MP3

player; times .. .; ACTION watches; guys wanna; song lasts; last

night; ACTION sits; -...)...- S.M.R.; Lime Player; Player 12%; dont

know; lez gurls; long time; gently kisses; Last seen

8. ○思考下面的Python 表达式:len(set(text4))。说明这个表达式的用途。描述在执行

此计算中涉及的两个步骤。

这个表达式计算出总共在text4中与多少个元素。首先计算出唯一的元素set(text4)。len()计算出元素的个数。

9. ○复习1.2 节关于链表和字符串的内容。

a. 定义一个字符串,并且将它分配给一个变量,如:my_string = 'My String'(在

字符串中放一些更有趣的东西)。用两种方法输出这个变量的内容,一种是通过简

单地输入变量的名称,然后按回车;另一种是通过使用print 语句。

>>> my_string ="nihao huang chengdu"

>>> my_string

'nihao huang chengdu'

>>> print my_string

nihao huang chengdu

b. 尝试使用my_string+ my_string 或者用它乘以一个数将字符串添加到它自身,

例如:my_string* 3。请注意,连接在一起的字符串之间没有空格。怎样能解决

这个问题?

>>> my_string +" " +my_string

'nihao huang chengdu nihao huang chengdu'

>>> my_string*2

'nihao huang chengdunihao huang chengdu'

10. ○使用的语法my_sent = ["My", "sent"],定义一个词链表变量my_sent(用你

自己的词或喜欢的话)。

a. 使用' '.join(my_sent)将其转换成一个字符串。

b. 使用split()在你指定的地方将字符串分割回链表。

>>> my_sent = ["nihao","huangchengdu"]

>>> ''.join(my_s)

Traceback (most recent call last):

File "", line 1, in

''.join(my_s)

NameError: name 'my_s' is not defined

>>> ''.join(my_sent)

'nihaohuangchengdu'

>>> 'nihao huangchengdu'.split

>>> 'nihao huangchengdu'.split()

['nihao', 'huangchengdu']

11. ○定义几个包含词链表的变量,例如:phrase1,phrase2 等。将它们连接在一起组

成不同的组合(使用加法运算符),最终形成完整的句子。len(phrase1 + phrase2)

与len(phrase1) + len(phrase2)之间的关系是什么?

>>> phrase1=['Hello']

>>> phrase2=[' ,World!']

>>> phrase1+phrase2

['Hello', ' ,World!']

>>> len(phrase1 + phrase2)

2>>> len(phrase1) + len(phrase2)

2>>> len(phrase1 + phrase2)==len(phrase1) + len(phrase2)

True

>>> len(phrase1 + phrase2) is len(phrase1) + len(phrase2)

True

12. ○考虑下面两个具有相同值的表达式。哪一个在NLP 中更常用?为什么?

a. "Monty Python"[6:12]

b. ["Monty", "Python"][1]

13. ○我们已经看到如何用词链表表示一个句子,其中每个词是一个字符序列。sent1[2][2]

代表什么意思?为什么?请用其他的索引值做实验。

14. ○在变量sent3 中保存的是text3 的第一句话。在sent3 中the 的索引值是1,因为s

ent3[1]的值是“the”。sent3 中“the”的其它出现的索引值是多少?

15. ○复习1.4 节讨论的条件语句。在聊天语料库(text5)中查找所有以字母b 开头的词。

按字母顺序显示出来。

16. ○在Python 解释器提示符下输入表达式range(10)。再尝试range(10, 20), range

(10, 20, 2)和range(20, 10, -2)。在后续章节中我们将看到这个内置函数的多用

用途。

17. ◑使用text9.index()查找词sunset 的索引值。你需要将这个词作为一个参数插入到圆

括号之间。通过尝试和出错的过程中,找到完整的句子中包含这个词的切片。

18. ◑使用链表加法、set 和sorted 操作,计算句子sent1...sent8 的词汇表。

19. ◑下面两行之间的差异是什么?哪一个的值比较大?其他文本也是同样情况吗?

>>> sorted(set([w.lower() for w in text1]))

>>> sorted([w.lower() for w in set(text1)]

20. ◑w.isupper()和not w.islower()这两个测试之间的差异是什么?

21. ◑写一个切片表达式提取text2 中最后两个词。

22. ◑找出聊天语料库(text5)中所有四个字母的词。使用频率分布函数(FreqDist),

以频率从高到低显示这些词。

23. ◑复习1.4 节中条件循环的讨论。使用for 和if 语句组合循环遍历《巨蟒和圣杯》(tex

t6)的电影剧本中的词,输出所有的大写词,每行输出一个。

24. ◑写表达式找出text6 中所有符合下列条件的词。结果应该是词链表的形式:['word

1', 'word2', ...]。

a. 以ize 结尾

b. 包含字母z

c. 包含字母序列pt

d. 除了首字母外是全部小写字母的词(即titlecase)

25. ◑定义sent 为词链表['she', 'sells', 'sea', 'shells', 'by', 'the', 'sea', 'shore']。

编写代码执行以下任务:

a. 输出所有sh 开头的单词

b. 输出所有长度超过4 个字符的词

26. ◑下面的Python 代码是做什么的?sum([len(w) for w in text1]),你可以用它来

算出一个文本的平均字长吗?

27. ◑定义一个名为vocab_size(text)的函数,以文本作为唯一的参数,返回文本的词汇

量。

def vocab_size(text):

count=0

for vocab in text:

count+=len(vocab)

return count

28. ◑定义一个函数percent(word, text),计算一个给定的词在文本中出现的频率,结

果以百分比表示。

import nltk

from nltk.book import *

def percent(word, text):

count=0

for words in text:

if words==word:

count+=1

return '%f%%'%(count*1.0/len(text)*100)

29. ◑我们一直在使用集合存储词汇表。试试下面的Python 表达式:set(sent3) < set(text1)。实验在set()中使用不同的参数。它是做什么用的?你能想到一个实际的应用

吗?

sent3的词汇集合属于text1的词汇集合

set()属于集合概念,可用来评价分词的性能,例如,已分完词的集合A与词典B比较,列出不属于B的单词集合。

set(A)-set(B)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值