Google python course basic exercise——mimic

在学习了前面python课程的综合知识后,再来做个小游戏。

 游戏规则:任给一个文件file,实现一个dict,key为file中出现的单词,value为一个由key在file中出现的位置后紧跟的下一个单词组成的list。

例如:file中,紧跟单词'and'后可能出现了'then','the','you'等,则dict中包含{'and',['then','the','you']}。

详细描述如下:

"""Mimic pyquick exercise -- optional extra exercise.
Google's Python Class

Read in the file specified on the command line.
Do a simple split() on whitespace to obtain all the words in the file.
Rather than read the file line by line, it's easier to read
it into one giant string and split it once.

Build a "mimic" dict that maps each word that appears in the file
to a list of all the words that immediately follow that word in the file.
The list of words can be be in any order and should include
duplicates. So for example the key "and" might have the list
["then", "best", "then", "after", ...] listing
all the words which came after "and" in the text.
We'll say that the empty string is what comes before
the first word in the file.

With the mimic dict, it's fairly easy to emit random
text that mimics the original. Print a word, then look
up what words might come next and pick one at random as
the next work.
Use the empty string as the first word to prime things.
If we ever get stuck with a word that is not in the dict,
go back to the empty string to keep things moving.

Note: the standard python module 'random' includes a
random.choice(list) method which picks a random element
from a non-empty list.

For fun, feed your program to itself as input.
Could work on getting it to put in linebreaks around 70
columns, so the output looks better.

"""

两代码对比:

 1 import random
 2 import sys
 3 
 4 
 5 """Returns mimic dict mapping each word to list of words that follow it."""
 6 def mimic_dict(filename):
 7     dict1 = {}
 8     f = open(filename,'r')
 9     text = (f.read()).split()
10     f.close()               # a good habit to keep consistency
11     global c        #to bring the value to print_mimic() function
12     c = text[0]
13     for i in range(len(text)-1):
14         if text[i] in dict1.keys():
15             dict1[text[i]] += [text[i+1]]#append other words to existing key
16         else:
17             dict1[text[i]] = [text[i+1]] #create new elment
18     return dict1
19 
20 
21 """Given mimic dict and start word, prints 200 random words."""
22 def print_mimic(mimic_dict, word):
23     for i in range(200):
24         if word not in mimic_dict.keys():#If  word  not in the dict,
25                                         #back to the empty string 
26             word = ''
27         if word == '':
28             mimic_dict[word] = [c]
29             word = c
30         else:
31             word = random.choice(mimic_dict[word])
32 #to use random.choice() to help choose
33         print word,  #keep the good format
34         
35 
36 # Provided main(), calls mimic_dict() and mimic()
37 def main():
38     if len(sys.argv) != 2:
39         print 'usage: ./mimic.py file-to-read'
40         sys.exit(1)
41 
42     dict = mimic_dict(sys.argv[1])
43     print_mimic(dict, '')
44 
45 
46 if __name__ == '__main__':
47     main()

 

 
 1 import random
 2 import sys
 3 def mimic_dict(filename):
 4   """Returns mimic dict mapping each word to list of words which follow it."""
 5   # +++your code here+++
 6   # LAB(begin solution)
 7   mimic_dict = {}
 8   f = open(filename, 'r')
 9   text = f.read()
10   f.close()
11   words = text.split()
12   prev = ''
13   for word in words:
14     if not prev in mimic_dict:
15       mimic_dict[prev] = [word]
16     else:
17       mimic_dict[prev].append(word)
18     # Could write as: mimic_dict[prev] = mimic_dict.get(prev, []) + [word]
19     # It's one line, but not totally satisfying.
20     prev = word
21   return mimic_dict
22   # LAB(replace solution)
23   # return
24   # LAB(end solution)
25 
26 def print_mimic(mimic_dict, word):
27   """Given mimic dict and start word, prints 200 random words."""
28   # +++your code here+++
29   # LAB(begin solution)
30   for unused_i in range(200):
31     print word,
32     nexts = mimic_dict.get(word)          # Returns None if not found
33     if not nexts:
34       nexts = mimic_dict['']  # Fallback to '' if not found
35     word = random.choice(nexts)
36   # The 'unused_' prefix turns off the lint warning about the unused variable.
37   # LAB(replace solution)
38   # return
39   # LAB(end solution)
40 
41 # Provided main(), calls mimic_dict() and mimic()
42 def main():
43   if len(sys.argv) != 2:
44     print 'usage: ./mimic.py file-to-read'
45     sys.exit(1)
46 
47   dict = mimic_dict(sys.argv[1])
48   print_mimic(dict, '')
49 
50 
51 if __name__ == '__main__':
52   main()

 运行结果示意:

PS:

  1. 表格左边一列是我刚开始学小试牛刀时写的一小段代码,右边是后来查看google提供的anwser。两段代码均能实现功能,逻辑基本一致,前者可读性略差于后者,稍显冗余啦啦,学习了。
  2. 本题旨在帮助熟悉文本分割,提取文本内容,掌握random.option()等函数使用,帮助分析词汇前后相关性,提高搜索性能。

转载于:https://www.cnblogs.com/Emma437211/archive/2013/04/03/2997432.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值