#NEWLINE# def _evaluate( self , prediction ) : #NEWLINE# #INDENT# 'Helper method to get the precision and recall of 1 sample' #NEWLINE# def calc_prop_distance( ref , pred ) : #NEWLINE# #INDENT# if ( len ( ref ) == 0 ) : #NEWLINE# #INDENT# return 1.0 #NEWLINE# #UNINDENT# diff = len ( ( set ( ref ) - set ( pred ) ) ) #NEWLINE# return ( ( len ( ref ) - diff ) / float ( len ( ref ) ) ) #NEWLINE# #UNINDENT# wordlist = sorted ( ( set ( prediction . keys ( ) ) & set ( self . reference . keys ( ) ) ) ) #NEWLINE# recall_sum = 0.0 #NEWLINE# precis_sum = 0.0 #NEWLINE# for word in wordlist : #NEWLINE# #INDENT# if ( len ( word ) < 2 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# recall_sum += max ( ( calc_prop_distance ( r , p ) for ( p , r ) in product ( prediction [ word ] , self . reference [ word ] ) ) ) #NEWLINE# precis_sum += max ( ( calc_prop_distance ( p , r ) for ( p , r ) in product ( prediction [ word ] , self . reference [ word ] ) ) ) #NEWLINE# #UNINDENT# precision = ( precis_sum / len ( wordlist ) ) #NEWLINE# recall = ( recall_sum / len ( wordlist ) ) #NEWLINE# f_score = ( 2.0 / ( ( 1.0 / precision ) + ( 1.0 / recall ) ) ) #NEWLINE# return ( precision , recall , f_score , len ( wordlist ) )