Package jazzparser :: Package utils :: Module output
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.utils.output

  1  """Command-line output printing utilities. 
  2   
  3  Utilities for formatting output to be printed to the command line. 
  4   
  5  """ 
  6  """ 
  7  ============================== License ======================================== 
  8   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
  9    
 10   This file is part of The Jazz Parser. 
 11    
 12   The Jazz Parser is free software: you can redistribute it and/or modify 
 13   it under the terms of the GNU General Public License as published by 
 14   the Free Software Foundation, either version 3 of the License, or 
 15   (at your option) any later version. 
 16    
 17   The Jazz Parser is distributed in the hope that it will be useful, 
 18   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20   GNU General Public License for more details. 
 21    
 22   You should have received a copy of the GNU General Public License 
 23   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 24   
 25  ============================ End license ====================================== 
 26   
 27  """ 
 28  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 29   
30 -def confusion_matrix(matrix):
31 """ 32 Given a confusion matrix as a dictionary, outputs it as a table. 33 34 The matrix should be in the format of a dictionary, keyed by 35 correct values (strings), containing dictionaries, keyed by 36 incorrect values (string), of integers. The integers represent 37 the number of times the incorrect value was mistaken for the 38 correct value. 39 40 """ 41 from jazzparser.utils.tableprint import pprint_table 42 import sys 43 # Convert the matrix into table data 44 rows = [] 45 for cor,incor_table in matrix.items(): 46 for incor,count in incor_table.items(): 47 rows.append([cor,incor,count]) 48 rows = list(reversed(sorted(rows, key=lambda r:r[2]))) 49 rows = [[cor,incor,str(count)] for cor,incor,count in rows] 50 header = [['Correct','Incorrect','Count'],['','','']] 51 return pprint_table(sys.stdout, header+rows, separator=" | ", outer_seps=True, justs=[True,True,False])
52 53 global __colorama_inited 54 __colorama_inited = False 55 __COLORAMA_PARAMS = { 56 'autoreset' : True, # Not colorama default 57 'strip' : None, # Colorama default 58 'convert' : None, # Colorama default 59 'wrap' : True, # Colorama default 60 } # Default parameters for initing colorama: not the same as colorama's own defaults 61
62 -def init_colors(**kwargs):
63 """ 64 Initializes colorama - terminal output color package. Only initializes it 65 once. If called more than once, does nothing after the first time. 66 67 Use kwargs to override colorama init params. 68 69 """ 70 global __colorama_inited 71 if not __colorama_inited: 72 from colorama import init 73 params = __COLORAMA_PARAMS.copy() 74 params.update(kwargs) 75 init(**params) 76 __colorama_inited = True
77
78 -def deinit_colors():
79 """ 80 De-initializes colorama. See the colorama docs for why you'd want to do 81 this. Only does anything if colorama has been inited using L{init_colors}. 82 83 @see: http://pypi.python.org/pypi/colorama 84 85 """ 86 global __colorama_inited 87 if __colorama_inited: 88 from colorama import deinit 89 deinit() 90 __colorama_inited = False
91
92 -def remove_ansi_colors(string):
93 """ 94 Strip all ANSI color commands from the string. 95 96 """ 97 import re 98 ansisequence= re.compile(r'\x1B\[[^A-Za-z]*[A-Za-z]') 99 return ansisequence.sub('', string)
100