| 37 | |
| 38 | |
| 39 | def test(): |
| 40 | import sys, random, getopt |
| 41 | args = sys.argv[1:] |
| 42 | try: |
| 43 | opts, args = getopt.getopt(args, '0123456789cdwq') |
| 44 | except getopt.error: |
| 45 | print('Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0]) |
| 46 | print('Options:') |
| 47 | print('-#: 1-digit history size (default 2)') |
| 48 | print('-c: characters (default)') |
| 49 | print('-w: words') |
| 50 | print('-d: more debugging output') |
| 51 | print('-q: no debugging output') |
| 52 | print('Input files (default stdin) are split in paragraphs') |
| 53 | print('separated blank lines and each paragraph is split') |
| 54 | print('in words by whitespace, then reconcatenated with') |
| 55 | print('exactly one space separating words.') |
| 56 | print('Output consists of paragraphs separated by blank') |
| 57 | print('lines, where lines are no longer than 72 characters.') |
| 58 | sys.exit(2) |
| 59 | histsize = 2 |
| 60 | do_words = False |
| 61 | debug = 1 |
| 62 | for o, a in opts: |
| 63 | if '-0' <= o <= '-9': histsize = int(o[1:]) |
| 64 | if o == '-c': do_words = False |
| 65 | if o == '-d': debug += 1 |
| 66 | if o == '-q': debug = 0 |
| 67 | if o == '-w': do_words = True |
| 68 | if not args: |
| 69 | args = ['-'] |
| 70 | |
| 71 | m = Markov(histsize, random.choice) |
| 72 | try: |
| 73 | for filename in args: |
| 74 | if filename == '-': |
| 75 | f = sys.stdin |
| 76 | if f.isatty(): |
| 77 | print('Sorry, need stdin from file') |
| 78 | continue |
| 79 | else: |
| 80 | f = open(filename, 'r') |
| 81 | with f: |
| 82 | if debug: print('processing', filename, '...') |
| 83 | text = f.read() |
| 84 | paralist = text.split('\n\n') |
| 85 | for para in paralist: |
| 86 | if debug > 1: print('feeding ...') |
| 87 | words = para.split() |
| 88 | if words: |
| 89 | if do_words: |
| 90 | data = tuple(words) |
| 91 | else: |
| 92 | data = ' '.join(words) |
| 93 | m.put(data) |
| 94 | except KeyboardInterrupt: |
| 95 | print('Interrupted -- continue with data read so far') |
| 96 | if not m.trans: |