<s> import re <EOL> from random import seed as _seed , choice <EOL> VARIABLE_TAG_START = '<STR_LIT>' <EOL> VARIABLE_TAG_END = '<STR_LIT>' <EOL> tag_re = re . compile ( '<STR_LIT>' % ( re . escape ( VARIABLE_TAG_START ) , re . escape ( VARIABLE_TAG_END ) ) ) <EOL> def choice_words ( phrase_book , root_key = '<STR_LIT:root>' , seed = None ) : <EOL> if isinstance ( seed , int ) : <EOL> seed = str ( seed << <NUM_LIT:10> ) <EOL> _seed ( seed ) <EOL> return eval_phrase ( phrase_book , lookup_phrase ( root_key , phrase_book ) ) <EOL> def apply_filters ( s , filters ) : <EOL> for f in filters : <EOL> if f == '<STR_LIT>' : <EOL> s = s . upper ( ) <EOL> elif f == '<STR_LIT>' : <EOL> s = s . lower ( ) <EOL> elif f == '<STR_LIT:title>' : <EOL> s = s . title ( ) <EOL> return s <EOL> def eval_phrase ( phrase_book , phrase ) : <EOL> s = "<STR_LIT>" <EOL> for token in tokenize ( phrase ) : <EOL> if token . token_type == TOKEN_TEXT : <EOL> s += token . text <EOL> else : <EOL> phrase = lookup_phrase ( token . text , phrase_book ) <EOL> text = eval_phrase ( phrase_book , phrase ) <EOL> s += apply_filters ( text , token . filters ) <EOL> return s <EOL> TOKEN_TEXT = '<STR_LIT:text>' <EOL> TOKEN_REF = '<STR_LIT>' <EOL> class Token ( object ) : <EOL> def __init__ ( self , text , token_type ) : <EOL> self . token_type = token_type <EOL> if token_type == TOKEN_REF : <EOL> text_without_tags = text [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) <EOL> text_and_filters = text_without_tags . split ( '<STR_LIT:|>' ) <EOL> self . text = text_and_filters [ <NUM_LIT:0> ] <EOL> self . filters = text_and_filters [ <NUM_LIT:1> : ] <EOL> else : <EOL> self . text = text <EOL> def tokenize ( phrase ) : <EOL> result = [ ] <EOL> in_tag = False <EOL> for part in tag_re . split ( phrase ) : <EOL> if part : <EOL> token_type = TOKEN_REF if in_tag else TOKEN_TEXT <EOL> result . append ( Token ( part , token_type ) ) <EOL> in_tag = not in_tag <EOL> return result <EOL> def lookup_phrase ( phrase_key , phrase_book ) : <EOL> v = phrase_book . get ( phrase_key ) <EOL> if v is None : <EOL> raise ValueError ( '<STR_LIT>' % phrase_key ) <EOL> assert isinstance ( v , list ) <EOL> return choice ( v ) <EOL> def parse_phrase_book ( fname ) : <EOL> phrases = [ ] <EOL> current_phrase = None <EOL> for line_number , line in enumerate ( open ( fname ) . readlines ( ) ) : <EOL> line = line . strip ( ) <EOL> if line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> elif len ( line ) == <NUM_LIT:0> : <EOL> current_phrase = None <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> if current_phrase is None : <EOL> raise ValueError ( "<STR_LIT>" % line_number ) <EOL> else : <EOL> current_phrase [ '<STR_LIT>' ] . append ( line [ <NUM_LIT:2> : ] . strip ( ) ) <EOL> elif line . endswith ( '<STR_LIT::>' ) : <EOL> current_phrase = { '<STR_LIT:key>' : line [ : - <NUM_LIT:1> ] , '<STR_LIT>' : [ ] } <EOL> phrases . append ( current_phrase ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % ( line_number , line ) ) <EOL> phrase_book = { } <EOL> for phrase in phrases : <EOL> phrase_book [ phrase [ '<STR_LIT:key>' ] ] = phrase [ '<STR_LIT>' ] <EOL> return phrase_book <EOL> def from_file ( fname , root_key = '<STR_LIT:root>' , seed = None ) : <EOL> phrase_book = parse_phrase_book ( fname ) <EOL> return choice_words ( phrase_book , root_key , seed ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from optparse import OptionParser <EOL> parser = OptionParser ( usage = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , default = '<STR_LIT:root>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , type = "<STR_LIT:int>" , default = <NUM_LIT:1> ) <EOL> option , args = parser . parse_args ( ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> for i in range ( option . amount ) : <EOL> print from_file ( args [ <NUM_LIT:0> ] , root_key = option . root , seed = option . seed ) <EOL> else : <EOL> parser . print_help ( ) </s>
