Back to index

python3.2  3.2.2
Classes | Functions
markov Namespace Reference

Classes

class  Markov

Functions

def test

Function Documentation

def markov.test ( )

Definition at line 39 of file markov.py.

00039 
00040 def test():
00041     import sys, random, getopt
00042     args = sys.argv[1:]
00043     try:
00044         opts, args = getopt.getopt(args, '0123456789cdwq')
00045     except getopt.error:
00046         print('Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0])
00047         print('Options:')
00048         print('-#: 1-digit history size (default 2)')
00049         print('-c: characters (default)')
00050         print('-w: words')
00051         print('-d: more debugging output')
00052         print('-q: no debugging output')
00053         print('Input files (default stdin) are split in paragraphs')
00054         print('separated blank lines and each paragraph is split')
00055         print('in words by whitespace, then reconcatenated with')
00056         print('exactly one space separating words.')
00057         print('Output consists of paragraphs separated by blank')
00058         print('lines, where lines are no longer than 72 characters.')
00059         sys.exit(2)
00060     histsize = 2
00061     do_words = False
00062     debug = 1
00063     for o, a in opts:
00064         if '-0' <= o <= '-9': histsize = int(o[1:])
00065         if o == '-c': do_words = False
00066         if o == '-d': debug += 1
00067         if o == '-q': debug = 0
00068         if o == '-w': do_words = True
00069     if not args:
00070         args = ['-']
00071 
00072     m = Markov(histsize, random.choice)
00073     try:
00074         for filename in args:
00075             if filename == '-':
00076                 f = sys.stdin
00077                 if f.isatty():
00078                     print('Sorry, need stdin from file')
00079                     continue
00080             else:
00081                 f = open(filename, 'r')
00082             if debug: print('processing', filename, '...')
00083             text = f.read()
00084             f.close()
00085             paralist = text.split('\n\n')
00086             for para in paralist:
00087                 if debug > 1: print('feeding ...')
00088                 words = para.split()
00089                 if words:
00090                     if do_words:
00091                         data = tuple(words)
00092                     else:
00093                         data = ' '.join(words)
00094                     m.put(data)
00095     except KeyboardInterrupt:
00096         print('Interrupted -- continue with data read so far')
00097     if not m.trans:
00098         print('No valid input files')
00099         return
00100     if debug: print('done.')
00101 
00102     if debug > 1:
00103         for key in m.trans.keys():
00104             if key is None or len(key) < histsize:
00105                 print(repr(key), m.trans[key])
00106         if histsize == 0: print(repr(''), m.trans[''])
00107         print()
00108     while True:
00109         data = m.get()
00110         if do_words:
00111             words = data
00112         else:
00113             words = data.split()
00114         n = 0
00115         limit = 72
00116         for w in words:
00117             if n + len(w) > limit:
00118                 print()
00119                 n = 0
00120             print(w, end=' ')
00121             n += len(w) + 1
00122         print()
00123         print()

Here is the call graph for this function: