Package jazzparser :: Package parsers :: Package pcfg :: Module tools
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.parsers.pcfg.tools

  1  """Interactive shell tools for the PCFG parser. 
  2   
  3  This provides tools for the debugging shell that are specific to the  
  4  PCFG parser. This parser also uses tools from the CKY parser (since it  
  5  is merely a subclass of it). 
  6   
  7  """ 
  8  """ 
  9  ============================== License ======================================== 
 10   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 11    
 12   This file is part of The Jazz Parser. 
 13    
 14   The Jazz Parser is free software: you can redistribute it and/or modify 
 15   it under the terms of the GNU General Public License as published by 
 16   the Free Software Foundation, either version 3 of the License, or 
 17   (at your option) any later version. 
 18    
 19   The Jazz Parser is distributed in the hope that it will be useful, 
 20   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 21   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 22   GNU General Public License for more details. 
 23    
 24   You should have received a copy of the GNU General Public License 
 25   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 26   
 27  ============================ End license ====================================== 
 28   
 29  """ 
 30  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 31   
 32  import sys 
 33  from jazzparser.shell.tools import Tool 
 34  from jazzparser.utils.strings import fmt_prob 
 35  from jazzparser.utils.tableprint import pprint_table 
 36   
37 -class ProbabilisticResultListTool(Tool):
38 """ 39 Prints out a particular range of results, or the whole list. 40 """ 41 name = "Result Slice with Probabilities" 42 commands = ['pres'] 43 usage = ("pres [<start> <end>]", "show the results list again, with probabilities, optionally giving a range.") 44 help = """ 45 Prints out the current result list, with result numbers and 46 probabilities. 47 Optionally, you may specify a valid range of result numbers to display. 48 49 This functions in the same way as the 'res' command, but shows 50 probabilities with the results. 51 52 The columns in the list are: 53 * result number 54 * probability 55 * ratio of probability to highest probability 56 * sign 57 58 See also: 59 res, the basic result printing command. 60 """
61 - def run(self, args, state):
62 if len(args) == 1: 63 raise ShellError, "You must specify a start and an end of a range" 64 elif len(args): 65 start,end = int(args[0]), int(args[1]) 66 print "Showing results in range [%s:%s]" % (start,end) 67 result_list = state.results[start:end] 68 else: 69 result_list = state.results 70 # Display results again 71 list_results(result_list, state.options.silent)
72 73
74 -class ProbabilityTool(Tool):
75 """ 76 Outputs the probability of a sign. 77 """ 78 name = "Show Probability" 79 commands = ['prob'] 80 usage = ("prob <sign>", "show the probability of the given sign.") 81 help = """ 82 Simply outputs the probability of a sign in the chart. 83 """ 84
85 - def run(self, args, state):
86 from jazzparser.shell.shell import ShellError 87 if len(args) == 0: 88 raise ShellError, "You must specify a sign from the chart. Specify a sign in the form 'arc_start/arc_end/index'." 89 # Get arcs from the chart 90 parts = args[0].split("/") 91 if len(parts) != 3: 92 raise ShellError, "%s is not a valid chart arc. Specify a sign in the form 'arc_start/arc_end/index'." % args[0] 93 parts = [int(p) for p in parts] 94 # Get the sign 95 sign = state.parser.chart.get_signs(parts[0], parts[1])[parts[2]] 96 print "%s, %s" % (sign, fmt_prob(sign.probability))
97 98
99 -class ProbabilisticChartTool(Tool):
100 """ 101 Outputs an arc from the chart, ranked by probability 102 """ 103 name = "Probabilistic Chart Arc" 104 commands = ['pchart'] 105 usage = ("pchart <start> <end>", "show the signs on a given arc in the chart ranked by probability.") 106 help = """ 107 Show the signs of an arc in the chart ranked by probabilities. 108 Specify an arc by its start and end nodes (as with the chart tool). 109 110 See also: 111 chart, for displaying the whole chart or parts of it. 112 """ 113
114 - def run(self, args, state):
115 from jazzparser.shell.shell import ShellError 116 if len(args) != 2: 117 raise ShellError, "You must give a start and end node for the arc." 118 # Get arcs from the chart 119 start,end = int(args[0]), int(args[1]) 120 signs = state.parser.chart.get_signs(start, end) 121 signs = list(reversed(sorted(signs, key=lambda s: s.probability))) 122 print "\n".join(["%s %s" % (fmt_prob(sign.probability), sign) for sign in signs])
123 124
125 -class ProbabilisticDerivationTraceTool(Tool):
126 """ 127 Outputs an arc from the chart, ranked by probability 128 """ 129 name = "Probabilistic Derivation Trace" 130 commands = ['pderiv'] 131 usage = ('pderiv <res>', 'show derivation of numbered result, including '\ 132 'probabilities of each sign') 133 help = """ 134 Just like deriv, but displays probabilities on signs. 135 136 See also: 137 deriv, the standard derivation trace tool. 138 """ 139
140 - def run(self, args, state):
141 results = state.results 142 # We must have an argument 143 from jazzparser.shell.shell import ShellError 144 if len(args) == 0: 145 raise ShellError, "You must specify the number of a result" 146 # Display the trace for this result 147 result_num = int(args[0]) 148 # Use custom formatting of the categories 149 def _signfmt(sign): 150 return "%s, P=%s" % (sign, sign.probability)
151 if result_num < len(results): 152 if results[result_num].derivation_trace is None: 153 raise ShellError, "Derivation traces have not been stored. Run parser with -d flag to create them" 154 else: 155 print "Probabilistic derivation trace for result %d: %s" % (result_num,results[result_num]) 156 print "\n%s" % \ 157 results[result_num].derivation_trace.str_indent(\ 158 signfmt=_signfmt) 159 else: 160 raise ShellError, "There are only %d results" % len(results)
161 162
163 -def list_results(results, silent):
164 """ 165 Like jazzparser.parser.list_results, but shows probabilities. 166 167 Note this doesn't obey the Latex option because I couldn't be 168 bothered. 169 170 """ 171 import math 172 def _fmt_index(i): 173 return format(i, " >3d")
174 175 if len(results) == 0: 176 if not silent: 177 print "No results" 178 elif silent: 179 # Only print the results themselves if we're in silent mode 180 for i in range(len(results)): 181 print "%s, %s" % (results[i], fmt_prob(results[i].probability)) 182 else: 183 previous_prob = None 184 # Get the highest scoring probability to compute the ratio of the others 185 if len(results): 186 log_highest_prob = results[0].probability 187 print "Log highest prob: %s" % log_highest_prob 188 table = [["", "", "Prob", "Ratio", "Sign"]] 189 for i in range(len(results)): 190 # Mark where probabilities are identical 191 if previous_prob == results[i].probability: 192 same_marker = "*" 193 else: 194 same_marker = " " 195 # Compute the ratio to the highest probability 196 prob_ratio = math.exp(results[i].probability - log_highest_prob) 197 table.append(["%s>" % _fmt_index(i), same_marker, fmt_prob(math.exp(results[i].probability)), "%.4f" % prob_ratio, str(results[i])]) 198 previous_prob = results[i].probability 199 pprint_table(sys.stdout, table, justs=[True,True,True,True,True]) 200