def modified_precision(references, hypothesis, n):
 counts = (Counter(ngrams(hypothesis, n)) if (len(hypothesis) >= n) else Counter())
 max_counts = {}
 for reference in references:
  reference_counts = (Counter(ngrams(reference, n)) if (len(reference) >= n) else Counter())
  for ngram in counts:
   max_counts[ngram] = max(max_counts.get(ngram, 0), reference_counts[ngram])
 clipped_counts = {ngram: min(count, max_counts[ngram]) for (ngram, count) in counts.items()}
 numerator = sum(clipped_counts.values())
 denominator = max(1, sum(counts.values()))
 return Fraction(numerator, denominator, _normalize=False)

def modified_precision ( references , hypothesis , n ) : counts = ( Counter ( ngrams ( hypothesis , n ) ) if ( len ( hypothesis ) >= n ) else Counter ( ) ) max_counts = { } for reference in references : reference_counts = ( Counter ( ngrams ( reference , n ) ) if ( len ( reference ) >= n ) else Counter ( ) ) for ngram in counts : max_counts [ ngram ] = max ( max_counts . get ( ngram , 0 ) , reference_counts [ ngram ] ) clipped_counts = { ngram : min ( count , max_counts [ ngram ] ) for ( ngram , count ) in counts . items ( ) } numerator = sum ( clipped_counts . values ( ) ) denominator = max ( 1 , sum ( counts . values ( ) ) ) return Fraction ( numerator , denominator , _normalize = False )
calculate modified ngram precision .