Skip to content

Commit e8988ef

Browse files
committed
first basic solution to the coding kata (trigram)
1 parent f387ee8 commit e8988ef

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

week-04/code/trigram.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
88
http://codekata.pragprog.com/2007/01/kata_fourteen_t.html
99
"""
10-
infilename = "sherlock_small.txt"
11-
#infilename = "sherlock.txt"
10+
# infilename = "sherlock_small.txt"
11+
infilename = "sherlock.txt"
1212

1313
import string
1414

@@ -41,18 +41,24 @@
4141
for i in range(len(words) - 2):
4242
pair = " ".join(words[i:i+2])
4343
follower = words[i+2]
44-
if pair in word_pairs:
45-
print "pair is repeated:", pair
46-
word_pairs[pair].append(follower)
47-
else:
48-
word_pairs[pair] = [follower]
44+
word_pairs.setdefault(pair,[]).append(follower)
4945

5046
# create some new text
5147

48+
# A little reporting
49+
for pair, followers in word_pairs.items():
50+
if len(followers) > 1:
51+
print pair, followers
52+
53+
5254
import random
5355
new_text = []
5456
for i in range (10): #just do a few
55-
pair = random.sample(word_pairs, 1)
56-
57+
pair = random.sample(word_pairs, 1)[0]
58+
print pair
59+
follower = random.sample(word_pairs[pair], 1)[0]
60+
new_text.extend( (pair, follower) )
61+
62+
new_text = " ".join(new_text)
5763

58-
print word_pairs
64+
print new_text

0 commit comments

Comments
 (0)