-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimic.py
104 lines (81 loc) · 2.96 KB
/
mimic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
#PERFORMED BY: alexxa
#DATE: 20.12.2013
#UPDATE: 11.01.2014
#SOURCE: Google Python course
# https://developers.google.com/edu/python/
#PURPOSE: Basics.
# The original course and exercises are in Python 2.4
# But I performed them in Python 3
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
# Google's Python Class
# http://code.google.com/edu/languages/google-python-class/
'''Mimic pyquick exercise -- optional extra exercise.
Google's Python Class
1) 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.
'''
import random, sys, string
def mimic_dict():
'''
Returns mimic dict mapping each word to list of words which follow it.
'''
myfile = open('alice.txt', 'r')
text = myfile.read().replace('\n', '')
text = ''.join(c for c in text if c not in string.punctuation).split()
myfile.close
dict = {}
for i in range(len(text)-1):
if text[i] in dict.keys():
dict[text[i]] += [text[i+1]]
else:
dict[text[i]] = [text[i+1]]
return dict
'''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.
'''
def print_mimic():
'''
Given mimic dict and start word, prints 200 random words.
'''
word = input('Please enter a word: ')
dict = mimic_dict()
if word in dict.keys():
for i in range(200):
print('{} {}'.format(word, random.choice(dict[word])))
else:
print('Sorry, there is no {} word in file alice.txt'.format(word))
#return
print_mimic()
# 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()