<s> import numpy as np <EOL> def load_confusion ( filename ) : <EOL> confusion = { } <EOL> for line in open ( filename ) . readlines ( ) : <EOL> a , b , v = line . rstrip ( ) . split ( '<STR_LIT:\t>' ) <EOL> confusion [ a , b ] = float ( v ) <EOL> return confusion <EOL> def confusion_matrix ( confusion , dtype = int ) : <EOL> labels = set ( ) <EOL> for a , b in confusion . keys ( ) : <EOL> labels . add ( a ) <EOL> labels . add ( b ) <EOL> labels = sorted ( labels ) <EOL> cm = np . zeros ( ( len ( labels ) , len ( labels ) ) , dtype = dtype ) <EOL> for ( a , b ) , count in confusion . items ( ) : <EOL> cm [ labels . index ( a ) , labels . index ( b ) ] = count <EOL> return cm <EOL> def confusion_reduce ( operation , confusions ) : <EOL> d = confusions [ <NUM_LIT:0> ] . copy ( ) <EOL> for c in confusions [ <NUM_LIT:1> : ] : <EOL> for k , v in c : <EOL> d [ k ] = operation ( d [ k ] , v ) <EOL> return d <EOL> def write_confusion ( confusion , stream ) : <EOL> for ( a , b ) , v in confusion . items ( ) : <EOL> print >> stream , '<STR_LIT:\t>' . join ( [ '<STR_LIT:U+0020>' . join ( a ) , '<STR_LIT:U+0020>' . join ( b ) , str ( v ) ] ) </s>
