<s> """<STR_LIT>""" <EOL> import collections <EOL> class MarkovChain ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _pairs = collections . defaultdict ( collections . Counter ) <EOL> def add ( self , state_from , state_to ) : <EOL> """<STR_LIT>""" <EOL> self . _pairs [ state_from ] [ state_to ] += <NUM_LIT:1> <EOL> def probability_table ( self , state_from ) : <EOL> """<STR_LIT>""" <EOL> ret_val = { } <EOL> try : <EOL> counter = self . _pairs [ state_from ] <EOL> total = float ( sum ( counter . values ( ) ) ) <EOL> ret_val = { k : v / total for k , v , in counter . items ( ) } <EOL> except KeyError : <EOL> pass <EOL> return ret_val <EOL> def most_probable ( self , state_from ) : <EOL> """<STR_LIT>""" <EOL> ret_val = ( ) <EOL> try : <EOL> counter = self . _pairs [ state_from ] <EOL> if len ( counter ) : <EOL> max_val = max ( counter . values ( ) ) <EOL> ret_val = tuple ( [ k for k , v in counter . items ( ) if v == max_val ] ) <EOL> except KeyError : <EOL> pass <EOL> return ret_val </s>
