X Tutup
Skip to content

Commit 86e1a2e

Browse files
committed
Updating code
1 parent 6a1e58b commit 86e1a2e

File tree

3 files changed

+130
-19
lines changed

3 files changed

+130
-19
lines changed

code/Map.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
14+
1015
class LinearMap(object):
1116
"""A simple implementation of a map using a list of tuples
1217
where each tuple is a key-value pair."""
@@ -92,7 +97,7 @@ def resize(self):
9297
self.maps = new_map
9398

9499

95-
def main(script):
100+
def main():
96101
import string
97102

98103
m = HashMap()
@@ -102,9 +107,8 @@ def main(script):
102107
m.add(k, v)
103108

104109
for k in range(len(s)):
105-
print k, m.get(k)
110+
print(k, m.get(k))
106111

107112

108113
if __name__ == '__main__':
109-
import sys
110-
main(*sys.argv)
114+
main()

code/Markov.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
14+
15+
import sys
16+
import random
17+
18+
from markov import skip_gutenberg_header, shift
19+
20+
21+
class Markov(object):
22+
"""Encapsulates the statistical summary of a text."""
23+
24+
def __init__(self):
25+
self.suffix_map = {} # map from prefixes to a list of suffixes
26+
self.prefix = () # current tuple of words
27+
28+
def process_file(self, filename, order=2):
29+
"""Reads a file and performs Markov analysis.
30+
31+
filename: string
32+
order: integer number of words in the prefix
33+
34+
Returns: map from prefix to list of possible suffixes.
35+
"""
36+
fp = open(filename)
37+
skip_gutenberg_header(fp)
38+
39+
for line in fp:
40+
for word in line.rstrip().split():
41+
self.process_word(word, order)
42+
43+
def process_word(self, word, order=2):
44+
"""Processes each word.
45+
46+
word: string
47+
order: integer
48+
49+
During the first few iterations, all we do is store up the words;
50+
after that we start adding entries to the dictionary.
51+
"""
52+
if len(self.prefix) < order:
53+
self.prefix += (word,)
54+
return
55+
56+
try:
57+
self.suffix_map[self.prefix].append(word)
58+
except KeyError:
59+
# if there is no entry for this prefix, make one
60+
self.suffix_map[self.prefix] = [word]
61+
62+
self.prefix = shift(self.prefix, word)
63+
64+
def random_text(self, n=100):
65+
"""Generates random wordsfrom the analyzed text.
66+
67+
Starts with a random prefix from the dictionary.
68+
69+
n: number of words to generate
70+
"""
71+
# choose a random prefix (not weighted by frequency)
72+
start = random.choice(self.suffix_map.keys())
73+
74+
for i in range(n):
75+
suffixes = self.suffix_map.get(start, None)
76+
if suffixes == None:
77+
# if the prefix isn't in map, we got to the end of the
78+
# original text, so we have to start again.
79+
self.random_text(n-i)
80+
return
81+
82+
# choose a random suffix
83+
word = random.choice(suffixes)
84+
print(word, end=' ')
85+
start = shift(start, word)
86+
87+
88+
def main(script, filename='emma.txt', n=100, order=2):
89+
try:
90+
n = int(n)
91+
order = int(order)
92+
except ValueError:
93+
print('Usage: %d filename [# of words] [prefix length]' % script)
94+
else:
95+
markov = Markov()
96+
markov.process_file(filename, order)
97+
markov.random_text(n)
98+
99+
100+
if __name__ == '__main__':
101+
main(*sys.argv)
102+

code/markov.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
1014
import sys
1115
import string
1216
import random
@@ -22,7 +26,7 @@ def process_file(filename, order=2):
2226
filename: string
2327
order: integer number of words in the prefix
2428
25-
Returns: map from prefix to list of possible suffixes.
29+
returns: map from prefix to list of possible suffixes.
2630
"""
2731
fp = open(filename)
2832
skip_gutenberg_header(fp)
@@ -85,7 +89,7 @@ def random_text(n=100):
8589

8690
# choose a random suffix
8791
word = random.choice(suffixes)
88-
print word,
92+
print(word, end=' ')
8993
start = shift(start, word)
9094

9195

@@ -100,15 +104,16 @@ def shift(t, word):
100104
return t[1:] + (word,)
101105

102106

103-
def main(name, filename='', n=100, order=2, *args):
107+
def main(script, filename='emma.txt', n=100, order=2):
104108
try:
105109
n = int(n)
106110
order = int(order)
107-
except:
108-
print 'Usage: randomtext.py filename [# of words] [prefix length]'
111+
except ValueError:
112+
print('Usage: %d filename [# of words] [prefix length]' % script)
109113
else:
110114
process_file(filename, order)
111115
random_text(n)
116+
print()
112117

113118

114119
if __name__ == '__main__':

0 commit comments

Comments
 (0)
X Tutup