#NEWLINE# def reply( self , text , loop_ms = 500 , max_len = None ) : #NEWLINE# #INDENT# 'Reply to a string of text. If the input is not already\n        Unicode, it will be decoded as utf-8.' #NEWLINE# if ( type ( text ) != types . UnicodeType ) : #NEWLINE# #INDENT# text = text . decode ( 'utf-8' , 'ignore' ) #NEWLINE# #UNINDENT# tokens = self . tokenizer . split ( text ) #NEWLINE# input_ids = map ( self . graph . get_token_by_text , tokens ) #NEWLINE# pivot_set = self . _filter_pivots ( input_ids ) #NEWLINE# if ( self . stemmer is not None ) : #NEWLINE# #INDENT# self . _conflate_stems ( pivot_set , tokens ) #NEWLINE# #UNINDENT# if ( len ( pivot_set ) == 0 ) : #NEWLINE# #INDENT# pivot_set = self . _babble ( ) #NEWLINE# #UNINDENT# score_cache = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# best_score = ( - 1.0 ) #NEWLINE# best_reply = None #NEWLINE# start = time . time ( ) #NEWLINE# end = ( start + ( loop_ms * 0.001 ) ) #NEWLINE# count = 0 #NEWLINE# all_replies = [ ] #NEWLINE# _start = time . time ( ) #NEWLINE# for ( edges , pivot_node ) in self . _generate_replies ( pivot_set ) : #NEWLINE# #INDENT# reply = Reply ( self . graph , tokens , input_ids , pivot_node , edges ) #NEWLINE# if ( max_len and self . _too_long ( max_len , reply ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# key = reply . edge_ids #NEWLINE# if ( key not in score_cache ) : #NEWLINE# #INDENT# with trace_us ( 'Brain.evaluate_reply_us' ) : #NEWLINE# #INDENT# score = self . scorer . score ( reply ) #NEWLINE# score_cache [ key ] = score #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# score = ( - 1 ) #NEWLINE# #UNINDENT# if ( score > best_score ) : #NEWLINE# #INDENT# best_reply = reply #NEWLINE# best_score = score #NEWLINE# #UNINDENT# if log . isEnabledFor ( logging . DEBUG ) : #NEWLINE# #INDENT# all_replies . append ( ( score , reply ) ) #NEWLINE# #UNINDENT# count += 1 #NEWLINE# if ( time . time ( ) > end ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# if ( best_reply is None ) : #NEWLINE# #INDENT# return "I don't know enough to answer you yet!" #NEWLINE# #UNINDENT# _time = ( time . time ( ) - _start ) #NEWLINE# if ( best_reply is None ) : #NEWLINE# #INDENT# return "I don't know enough to answer you yet!" #NEWLINE# #UNINDENT# self . scorer . end ( best_reply ) #NEWLINE# if log . isEnabledFor ( logging . DEBUG ) : #NEWLINE# #INDENT# replies = [ ( score , reply . to_text ( ) ) for ( score , reply ) in all_replies ] #NEWLINE# replies . sort ( ) #NEWLINE# for ( score , text ) in replies : #NEWLINE# #INDENT# log . debug ( '%f %s' , score , text ) #NEWLINE# #UNINDENT# #UNINDENT# trace ( 'Brain.reply_input_token_count' , len ( tokens ) ) #NEWLINE# trace ( 'Brain.known_word_token_count' , len ( pivot_set ) ) #NEWLINE# trace ( 'Brain.reply_us' , _time ) #NEWLINE# trace ( 'Brain.reply_count' , count , _time ) #NEWLINE# trace ( 'Brain.best_reply_score' , int ( ( best_score * 1000 ) ) ) #NEWLINE# trace ( 'Brain.best_reply_length' , len ( best_reply . edge_ids ) ) #NEWLINE# log . debug ( ( 'made %d replies (%d unique) in %f seconds' % ( count , len ( score_cache ) , _time ) ) ) #NEWLINE# if ( len ( text ) > 60 ) : #NEWLINE# #INDENT# msg = ( text [ 0 : 60 ] + '...' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# msg = text #NEWLINE# #UNINDENT# log . info ( '[%s] %d %f' , score_cache , count , best_score ) #NEWLINE# with trace_us ( 'Brain.reply_words_lookup_us' ) : #NEWLINE# #INDENT# text = best_reply . to_text ( ) #NEWLINE# #UNINDENT# return text