# $Id: markow.py 750 2005-08-13 20:28:39Z md $ """Class for handling markow chains. A little amusement, find more at http://c0re.jp/ --drt@un.bewaff.net """ from __future__ import generators import random import pprint _NOWORD = '' class markow: """Class for using a simple text based markow chain. e.g. # create a new chain m = markow() # initialize the cahin with some data m.build('laber rababer and about 1000 additional words go here ...') # output some text based on thet chain print ' '.join(m.putput) """ def __init__(self, prefixlen=3): self.startstate = [_NOWORD] * prefixlen # how to copy by calue in python? self.statetab = {} def build(self, s): state = self.startstate * 1 for x in s.split(): if tuple(state) not in self.statetab: self.statetab[tuple(state)] = [] self.statetab[tuple(state)].append(x) state.pop(0) state.append(x) # add tail if tuple(state) not in self.statetab: self.statetab[tuple(state)] = [] self.statetab[tuple(state)].append(_NOWORD) def buildfromfile(self, fd): state = self.startstate * 1 for line in fd.readlines(): for x in line.split(): if tuple(state) not in self.statetab: self.statetab[tuple(state)] = [] self.statetab[tuple(state)].append(x) state.pop(0) state.append(x) # add tail if tuple(state) not in self.statetab: self.statetab[tuple(state)] = [] self.statetab[tuple(state)].append(_NOWORD) def output(self, maxlen=50000): state = self.startstate * 1 l = [] for i in range(maxlen): suffix = random.choice(self.statetab[tuple(state)]) if suffix == _NOWORD: return l l.append(suffix) state.pop(0) state.append(suffix) if __name__ == '__main__': # run an example # produce an comp.lang.python lookalike message import nntplib # change this to your server name. Append username and password at the end if needed # eg s = nntplib.NNTP('news.cis.dfn.de', 119, 'user', 'pass') s = nntplib.NNTP('news.t-online.de', 119) resp, count, first, last, name = s.group('comp.lang.python') resp, subs = s.xhdr('subject', first + '-' + last) m = markow() for id, sub in subs[-10:]: m.build('\n'.join(s.body(id)[3])) column = 0 for x in m.output(): if x == '>' or column > 70: print column = 0 print x, column += len(x) + 1 s.quit()