Changeset 391 for python/trunk/Demo/scripts/markov.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Demo/scripts/markov.py
r2 r391 6 6 self.choice = choice 7 7 self.trans = {} 8 8 9 def add(self, state, next): 9 if not self.trans.has_key(state): 10 self.trans[state] = [next] 11 else: 12 self.trans[state].append(next) 10 self.trans.setdefault(state, []).append(next) 11 13 12 def put(self, seq): 14 13 n = self.histsize … … 18 17 add(seq[max(0, i-n):i], seq[i:i+1]) 19 18 add(seq[len(seq)-n:], None) 19 20 20 def get(self): 21 21 choice = self.choice … … 23 23 n = self.histsize 24 24 seq = choice(trans[None]) 25 while 1:25 while True: 26 26 subseq = seq[max(0, len(seq)-n):] 27 27 options = trans[subseq] 28 28 next = choice(options) 29 if not next: break 30 seq = seq + next 29 if not next: 30 break 31 seq += next 31 32 return seq 32 33 34 33 35 def test(): 34 import sys, string,random, getopt36 import sys, random, getopt 35 37 args = sys.argv[1:] 36 38 try: 37 opts, args = getopt.getopt(args, '0123456789cdw ')39 opts, args = getopt.getopt(args, '0123456789cdwq') 38 40 except getopt.error: 39 print 'Usage: markov [-#] [-cddqw] [file] ...'41 print 'Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0] 40 42 print 'Options:' 41 43 print '-#: 1-digit history size (default 2)' … … 50 52 print 'Output consists of paragraphs separated by blank' 51 53 print 'lines, where lines are no longer than 72 characters.' 54 sys.exit(2) 52 55 histsize = 2 53 do_words = 056 do_words = False 54 57 debug = 1 55 58 for o, a in opts: 56 if '-0' <= o <= '-9': histsize = eval(o[1:])57 if o == '-c': do_words = 058 if o == '-d': debug = debug +159 if '-0' <= o <= '-9': histsize = int(o[1:]) 60 if o == '-c': do_words = False 61 if o == '-d': debug += 1 59 62 if o == '-q': debug = 0 60 if o == '-w': do_words = 1 61 if not args: args = ['-'] 63 if o == '-w': do_words = True 64 if not args: 65 args = ['-'] 66 62 67 m = Markov(histsize, random.choice) 63 68 try: … … 73 78 text = f.read() 74 79 f.close() 75 paralist = string.splitfields(text,'\n\n')80 paralist = text.split('\n\n') 76 81 for para in paralist: 77 82 if debug > 1: print 'feeding ...' 78 words = string.split(para)83 words = para.split() 79 84 if words: 80 if do_words: data = tuple(words) 81 else: data = string.joinfields(words, ' ') 85 if do_words: 86 data = tuple(words) 87 else: 88 data = ' '.join(words) 82 89 m.put(data) 83 90 except KeyboardInterrupt: … … 87 94 return 88 95 if debug: print 'done.' 96 89 97 if debug > 1: 90 98 for key in m.trans.keys(): … … 93 101 if histsize == 0: print repr(''), m.trans[''] 94 102 print 95 while 1:103 while True: 96 104 data = m.get() 97 if do_words: words = data 98 else: words = string.split(data) 105 if do_words: 106 words = data 107 else: 108 words = data.split() 99 109 n = 0 100 110 limit = 72 … … 104 114 n = 0 105 115 print w, 106 n = n +len(w) + 1116 n += len(w) + 1 107 117 print 108 118 print 109 119 110 def tuple(list):111 if len(list) == 0: return ()112 if len(list) == 1: return (list[0],)113 i = len(list)//2114 return tuple(list[:i]) + tuple(list[i:])115 116 120 if __name__ == "__main__": 117 121 test()
Note:
See TracChangeset
for help on using the changeset viewer.