File tree Expand file tree Collapse file tree 1 file changed +16
-10
lines changed
Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Original file line number Diff line number Diff line change 77
88http://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
1313import string
1414
4141for 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+
5254import random
5355new_text = []
5456for 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
You can’t perform that action at this time.
0 commit comments