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
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
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,
57 'strip' : None,
58 'convert' : None,
59 'wrap' : True,
60 }
61
77
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
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