#NEWLINE# def print_template_statistics( self , test_stats = None , printunused = True ) : #NEWLINE# #INDENT# '\n        Print a list of all templates, ranked according to efficiency.\n\n        If test_stats is available, the templates are ranked according to their\n        relative contribution (summed for all rules created from a given template,\n        weighted by score) to the performance on the test set. If no test_stats, then\n        statistics collected during training are used instead. There is also\n        an unweighted measure (just counting the rules). This is less informative,\n        though, as many low-score rules will appear towards end of training.\n\n        :param test_stats: dictionary of statistics collected during testing\n        :type test_stats: dict of str -> any (but usually numbers)\n        :param printunused: if True, print a list of all unused templates\n        :type printunused: bool\n        :return: None\n        :rtype: None\n        ' #NEWLINE# tids = [ r . templateid for r in self . _rules ] #NEWLINE# train_stats = self . train_stats ( ) #NEWLINE# trainscores = train_stats [ 'rulescores' ] #NEWLINE# assert ( len ( trainscores ) == len ( tids ) ) , 'corrupt statistics: {0} train scores for {1} rules' . format ( trainscores , tids ) #NEWLINE# template_counts = Counter ( tids ) #NEWLINE# weighted_traincounts = Counter ( ) #NEWLINE# for ( tid , score ) in zip ( tids , trainscores ) : #NEWLINE# #INDENT# weighted_traincounts [ tid ] += score #NEWLINE# #UNINDENT# tottrainscores = sum ( trainscores ) #NEWLINE# def det_tplsort( tpl_value ) : #NEWLINE# #INDENT# return ( tpl_value [ 1 ] , repr ( tpl_value [ 0 ] ) ) #NEWLINE# #UNINDENT# def print_train_stats( ) : #NEWLINE# #INDENT# print ( 'TEMPLATE STATISTICS (TRAIN)  {0} templates, {1} rules)' . format ( len ( template_counts ) , len ( tids ) ) ) #NEWLINE# print ( 'TRAIN ({tokencount:7d} tokens) initial {initialerrors:5d} {initialacc:.4f} final: {finalerrors:5d} {finalacc:.4f} ' . format ( ** train_stats ) ) #NEWLINE# head = '#ID | Score (train) |  #Rules     | Template' #NEWLINE# print ( head , '\n' , ( '-' * len ( head ) ) , sep = '' ) #NEWLINE# train_tplscores = sorted ( weighted_traincounts . items ( ) , key = det_tplsort , reverse = True ) #NEWLINE# for ( tid , trainscore ) in train_tplscores : #NEWLINE# #INDENT# s = '{0} | {1:5d}   {2:5.3f} |{3:4d}   {4:.3f} | {5}' . format ( tid , trainscore , ( trainscore / tottrainscores ) , template_counts [ tid ] , ( template_counts [ tid ] / len ( tids ) ) , Template . ALLTEMPLATES [ int ( tid ) ] ) #NEWLINE# print ( s ) #NEWLINE# #UNINDENT# #UNINDENT# def print_testtrain_stats( ) : #NEWLINE# #INDENT# testscores = test_stats [ 'rulescores' ] #NEWLINE# print ( 'TEMPLATE STATISTICS (TEST AND TRAIN) ({0} templates, {1} rules)' . format ( len ( template_counts ) , len ( tids ) ) ) #NEWLINE# print ( 'TEST  ({tokencount:7d} tokens) initial {initialerrors:5d} {initialacc:.4f} final: {finalerrors:5d} {finalacc:.4f} ' . format ( ** test_stats ) ) #NEWLINE# print ( 'TRAIN ({tokencount:7d} tokens) initial {initialerrors:5d} {initialacc:.4f} final: {finalerrors:5d} {finalacc:.4f} ' . format ( ** train_stats ) ) #NEWLINE# weighted_testcounts = Counter ( ) #NEWLINE# for ( tid , score ) in zip ( tids , testscores ) : #NEWLINE# #INDENT# weighted_testcounts [ tid ] += score #NEWLINE# #UNINDENT# tottestscores = sum ( testscores ) #NEWLINE# head = '#ID | Score (test) | Score (train) |  #Rules     | Template' #NEWLINE# print ( head , '\n' , ( '-' * len ( head ) ) , sep = '' ) #NEWLINE# test_tplscores = sorted ( weighted_testcounts . items ( ) , key = det_tplsort , reverse = True ) #NEWLINE# for ( tid , testscore ) in test_tplscores : #NEWLINE# #INDENT# s = '{0:s} |{1:5d}  {2:6.3f} |  {3:4d}   {4:.3f} |{5:4d}   {6:.3f} | {7:s}' . format ( tid , testscore , ( testscore / tottestscores ) , weighted_traincounts [ tid ] , ( weighted_traincounts [ tid ] / tottrainscores ) , template_counts [ tid ] , ( template_counts [ tid ] / len ( tids ) ) , Template . ALLTEMPLATES [ int ( tid ) ] ) #NEWLINE# print ( s ) #NEWLINE# #UNINDENT# #UNINDENT# def print_unused_templates( ) : #NEWLINE# #INDENT# usedtpls = set ( [ int ( tid ) for tid in tids ] ) #NEWLINE# unused = [ ( tid , tpl ) for ( tid , tpl ) in enumerate ( Template . ALLTEMPLATES ) if ( tid not in usedtpls ) ] #NEWLINE# print ( 'UNUSED TEMPLATES ({0})' . format ( len ( unused ) ) ) #NEWLINE# for ( tid , tpl ) in unused : #NEWLINE# #INDENT# print ( '{0:03d} {1:s}' . format ( tid , tpl ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( test_stats is None ) : #NEWLINE# #INDENT# print_train_stats ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print_testtrain_stats ( ) #NEWLINE# #UNINDENT# print ( ) #NEWLINE# if printunused : #NEWLINE# #INDENT# print_unused_templates ( ) #NEWLINE# #UNINDENT# print ( )