#NEWLINE# def __generate_grammar( model , corpus , abstract_grammars , values , val_indices , indices_val , max_len , max_tries , diversity ) : #NEWLINE# #INDENT# curr_grammar = '' #NEWLINE# start_index = np . random . randint ( 0 , ( len ( corpus ) - max_len ) ) #NEWLINE# sentence = corpus [ start_index : ( start_index + max_len ) ] #NEWLINE# running_length = 0.0 #NEWLINE# while ( running_length <= 4.1 ) : #NEWLINE# #INDENT# x = np . zeros ( ( 1 , max_len , len ( values ) ) ) #NEWLINE# for ( t , val ) in enumerate ( sentence ) : #NEWLINE# #INDENT# if ( not ( val in val_indices ) ) : #NEWLINE# #INDENT# print ( val ) #NEWLINE# #UNINDENT# x [ ( 0 , t , val_indices [ val ] ) ] = 1.0 #NEWLINE# #UNINDENT# next_val = __predict ( model , x , indices_val , diversity ) #NEWLINE# if ( running_length < 1e-05 ) : #NEWLINE# #INDENT# tries = 0 #NEWLINE# while ( ( next_val . split ( ',' ) [ 0 ] == 'R' ) or ( len ( next_val . split ( ',' ) ) != 2 ) ) : #NEWLINE# #INDENT# if ( tries >= max_tries ) : #NEWLINE# #INDENT# print ( 'Gave up on first note generation after' , max_tries , 'tries' ) #NEWLINE# rand = np . random . randint ( 0 , len ( abstract_grammars ) ) #NEWLINE# next_val = abstract_grammars [ rand ] . split ( ' ' ) [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# next_val = __predict ( model , x , indices_val , diversity ) #NEWLINE# #UNINDENT# tries += 1 #NEWLINE# #UNINDENT# #UNINDENT# sentence = sentence [ 1 : ] #NEWLINE# sentence . append ( next_val ) #NEWLINE# if ( running_length > 1e-05 ) : #NEWLINE# #INDENT# curr_grammar += ' ' #NEWLINE# #UNINDENT# curr_grammar += next_val #NEWLINE# length = float ( next_val . split ( ',' ) [ 1 ] ) #NEWLINE# running_length += length #NEWLINE# #UNINDENT# return curr_grammar