Google python mimic.py (python 2.7)

#!/usr/bin/python -tt

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.

"""

import random
import sys


def mimic_dict(filename):
"""Returns mimic dict mapping each word to list of words which follow it."""
[color=red] f=open(filename,'r')
words=f.read()
f.close()
dicts={}
words=words.split()
#solution1:
if len(words)>0:
dicts['']=[words[0]]
for word in words:
if words.index(word)<len(words)-1:
if word in dicts.keys() and words[words.index(word)+1] not in dicts.values():
dicts.get(word).append(words[words.index(word)+1])
else:
dicts[word]=[words[words.index(word)+1]]
return dicts[/color]
#solution 2:
[color=blue] temp=''
for word in words:
if temp not in dicts:
dicts[temp]=[word]
else:
dicts[temp].append(word)
temp=word
return dicts[/color]

def print_mimic(mimic_dict, word):
"""Given mimic dict and start word, prints 200 random words."""

[color=red] for i in range(0,200):
print word,
if len(mimic_dict[word])<1:
word=''
word=random.choice(mimic_dict.get(word))
[/color]

# Provided main(), calls mimic_dict() and mimic()
def main():
if len(sys.argv) != 2:
print 'usage: ./mimic.py file-to-read'
sys.exit(1)

dict = mimic_dict(sys.argv[1])
print_mimic(dict, '')


if __name__ == '__main__':
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值