<s> import time <EOL> from markov import tokenise <EOL> from markov import markov <EOL> class MarkovStateError ( Exception ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> class MarkovState : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . markov = None <EOL> self . generator = None <EOL> def generate ( self , chunks , seed = None , prob = <NUM_LIT:0> , offset = <NUM_LIT:0> , cln = None , <EOL> startf = lambda t : True , endchunkf = lambda t : True , <EOL> kill = <NUM_LIT:0> , prefix = ( ) ) : <EOL> """<STR_LIT>""" <EOL> if self . markov is None : <EOL> raise MarkovStateError ( "<STR_LIT>" ) <EOL> if seed is None : <EOL> seed = int ( time . time ( ) ) <EOL> print ( "<STR_LIT>" . format ( seed ) ) <EOL> if len ( prefix ) > self . markov . n : <EOL> print ( "<STR_LIT>" ) <EOL> prefix = prefix [ self . markov . n - <NUM_LIT:1> : ] <EOL> self . markov . reset ( seed , prob , prefix , cln ) <EOL> for i in range ( offset ) : <EOL> next ( self . markov ) <EOL> while not startf ( next ( self . markov ) ) : <EOL> pass <EOL> def gen ( n ) : <EOL> out = [ ] <EOL> while n > <NUM_LIT:0> : <EOL> tok = next ( self . markov ) <EOL> out . append ( tok ) <EOL> if endchunkf ( tok ) or self . markov . lastStateSaturated ( ) : <EOL> n -= <NUM_LIT:1> <EOL> self . markov . cleanRecentData ( ) <EOL> return ( '<STR_LIT:U+0020>' . join ( out if not kill else out [ : - kill ] ) ) <EOL> self . generator = gen <EOL> return self . generator ( chunks ) <EOL> def more ( self , chunks = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if self . generator is None : <EOL> raise MarkovStateError ( "<STR_LIT>" ) <EOL> return self . generator ( chunks ) <EOL> def train ( self , n , stream , noparagraphs = False ) : <EOL> """<STR_LIT>""" <EOL> training_data = tokenise . Tokeniser ( stream = stream , <EOL> noparagraphs = noparagraphs ) <EOL> self . markov = markov . Markov ( n ) <EOL> self . markov . train ( training_data ) <EOL> self . generator = None <EOL> def load ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . generator = None <EOL> self . markov = markov . Markov ( ) <EOL> self . markov . load ( filename ) <EOL> def dump ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if self . markov is None : <EOL> raise MarkovStateError ( "<STR_LIT>" ) <EOL> self . markov . dump ( filename ) </s>
