Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Demo/scripts/markov.py

    r2 r391  
    66        self.choice = choice
    77        self.trans = {}
     8
    89    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
    1312    def put(self, seq):
    1413        n = self.histsize
     
    1817            add(seq[max(0, i-n):i], seq[i:i+1])
    1918        add(seq[len(seq)-n:], None)
     19
    2020    def get(self):
    2121        choice = self.choice
     
    2323        n = self.histsize
    2424        seq = choice(trans[None])
    25         while 1:
     25        while True:
    2626            subseq = seq[max(0, len(seq)-n):]
    2727            options = trans[subseq]
    2828            next = choice(options)
    29             if not next: break
    30             seq = seq + next
     29            if not next:
     30                break
     31            seq += next
    3132        return seq
    3233
     34
    3335def test():
    34     import sys, string, random, getopt
     36    import sys, random, getopt
    3537    args = sys.argv[1:]
    3638    try:
    37         opts, args = getopt.getopt(args, '0123456789cdw')
     39        opts, args = getopt.getopt(args, '0123456789cdwq')
    3840    except getopt.error:
    39         print 'Usage: markov [-#] [-cddqw] [file] ...'
     41        print 'Usage: %s [-#] [-cddqw] [file] ...' % sys.argv[0]
    4042        print 'Options:'
    4143        print '-#: 1-digit history size (default 2)'
     
    5052        print 'Output consists of paragraphs separated by blank'
    5153        print 'lines, where lines are no longer than 72 characters.'
     54        sys.exit(2)
    5255    histsize = 2
    53     do_words = 0
     56    do_words = False
    5457    debug = 1
    5558    for o, a in opts:
    56         if '-0' <= o <= '-9': histsize = eval(o[1:])
    57         if o == '-c': do_words = 0
    58         if o == '-d': debug = debug + 1
     59        if '-0' <= o <= '-9': histsize = int(o[1:])
     60        if o == '-c': do_words = False
     61        if o == '-d': debug += 1
    5962        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
    6267    m = Markov(histsize, random.choice)
    6368    try:
     
    7378            text = f.read()
    7479            f.close()
    75             paralist = string.splitfields(text, '\n\n')
     80            paralist = text.split('\n\n')
    7681            for para in paralist:
    7782                if debug > 1: print 'feeding ...'
    78                 words = string.split(para)
     83                words = para.split()
    7984                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)
    8289                    m.put(data)
    8390    except KeyboardInterrupt:
     
    8794        return
    8895    if debug: print 'done.'
     96
    8997    if debug > 1:
    9098        for key in m.trans.keys():
     
    93101        if histsize == 0: print repr(''), m.trans['']
    94102        print
    95     while 1:
     103    while True:
    96104        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()
    99109        n = 0
    100110        limit = 72
     
    104114                n = 0
    105115            print w,
    106             n = n + len(w) + 1
     116            n += len(w) + 1
    107117        print
    108118        print
    109119
    110 def tuple(list):
    111     if len(list) == 0: return ()
    112     if len(list) == 1: return (list[0],)
    113     i = len(list)//2
    114     return tuple(list[:i]) + tuple(list[i:])
    115 
    116120if __name__ == "__main__":
    117121    test()
Note: See TracChangeset for help on using the changeset viewer.