码迷,mamicode.com
首页 > 其他好文 > 详细

第八次作业

时间:2017-09-26 22:24:04      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:col   提取   eve   word   bsp   code   python   排除   because   

1.读入待分析的字符串

fo=open(test.txt,w)
n=fo.write(‘‘‘The driver, being a sensitive man as well as an animal lover, pulled over t
o the side of the road, and got out to see what had become of the Bunny carrying
the basket. Much to his dismay, the colorful Bunny was dead. The driver felt gui
lty and began to cry. A woman driving down the same highway saw the man crying on the side of the roa
d and pulled over. She stepped out of her car and asked the man what was wrong.
"I feel terrible," he explained, "I accidentally hit the Easter Bunny and kille
d it. There may not be an Easter because of me. What should I do?"The woman told the man not to worry. She knew exactly what to do. She went to
her car trunk, and pulled ut a spray can. She walked over to the limp,dead Bunny, and sprayed the entire contents of the can onto the little furryanimalMiraculously4 the Easter Bunny came to back life, jumped up, picked up
the spilled eggs and candy, waved its paw at the two humans and hopped5 on down the road. 50 yards away the Easter Bunny stopped, turned around,
waved and hopped on down the road another 50 yards, turned, waved, hopped anoth
er 50 yards and waved again!‘‘‘)
fo.close()

 

2.分解提取单词 

n=‘‘‘The driver, being a sensitive man as well as an animal lover, pulled over t
o the side of the road, and got out to see what had become of the Bunny carrying
the basket. Much to his dismay, the colorful Bunny was dead. The driver felt gui
lty and began to cry. A woman driving down the same highway saw the man crying on the side of the roa
d and pulled over. She stepped out of her car and asked the man what was wrong.
"I feel terrible," he explained, "I accidentally hit the Easter Bunny and kille
d it. There may not be an Easter because of me. What should I do?"The woman told the man not to worry. She knew exactly what to do. She went to
her car trunk, and pulled ut a spray can. She walked over to the limp,dead Bunny, and sprayed the entire contents of the can onto the little furryanimalMiraculously4 the Easter Bunny came to back life, jumped up, picked up
the spilled eggs and candy, waved its paw at the two humans and hopped5 on down the road. 50 yards away the Easter Bunny stopped, turned around,
waved and hopped on down the road another 50 yards, turned, waved, hopped anoth
er 50 yards and waved again!‘‘‘
n=n.lower()
for i in ‘,!""?‘:
    n=n.replace(i,‘ ‘)
words=n.split(‘ ‘)
print(words)

  

3.计数字典

dic={}
keys=set(words)
print(keys)
for i in keys:
     dic[i]=words.count(i)
print(dic)

  

4.排除语法型词汇

n=n.lower()
exc={‘the‘,‘a‘,‘as‘,‘of‘,‘I‘,‘an‘,‘had‘,‘to‘,‘‘}
for i in ‘,!""?‘:
    n=n.replace(i,‘ ‘)
words=n.split(‘ ‘)
print(words)
dic={}
keys=set(words)
keys=keys-exc
for i in keys:
     dic[i]=words.count(i)
print(dic)

  

5.排序

nc=list(dic.items())
nc.sort(key=lambda x:x[1],reverse=True)

  

6.输出TOP(20)

for i in range(20):
print(nc[i])

  

第八次作业

标签:col   提取   eve   word   bsp   code   python   排除   because   

原文地址:http://www.cnblogs.com/wangze019/p/7599058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!