Package jazzparser :: Package shell :: Module gstools
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.shell.gstools

  1  """Gold standard comparison shell tools 
  2   
  3  Some additional tools for the Jazz Parser interactive shell for  
  4  comparing results to a gold standard. 
  5   
  6  """ 
  7  """ 
  8  ============================== License ======================================== 
  9   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 10    
 11   This file is part of The Jazz Parser. 
 12    
 13   The Jazz Parser is free software: you can redistribute it and/or modify 
 14   it under the terms of the GNU General Public License as published by 
 15   the Free Software Foundation, either version 3 of the License, or 
 16   (at your option) any later version. 
 17    
 18   The Jazz Parser is distributed in the hope that it will be useful, 
 19   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 21   GNU General Public License for more details. 
 22    
 23   You should have received a copy of the GNU General Public License 
 24   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 25   
 26  ============================ End license ====================================== 
 27   
 28  """ 
 29  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 30   
 31  from .tools import Tool 
 32   
33 -class LoadGoldStandardTool(Tool):
34 """ 35 Load an annotated chord sequence file. 36 """ 37 name = "Gold Standard" 38 commands = ['loadgs', 'lgs'] 39 usage = ("loadgs <filename>", "load an annotated data file for use by other GS tools") 40 help = """ 41 Given a filename of a db mirror data file, loads the data ready for 42 use by other GS comparison tools. 43 """ 44
45 - def run(self, args, state):
46 from jazzparser.data.db_mirrors import SequenceIndex 47 from .shell import ShellError 48 if len(args) < 1: 49 raise ShellError, "Please specify a file to load" 50 filename = args[0] 51 # Load the data file 52 si = SequenceIndex.from_file(filename) 53 # Store it in the state 54 state.gs_sequences = si 55 print "Loaded %d gold standard sequences from %s" % (len(si),filename)
56
57 -class MarkCorrectTool(Tool):
58 name = "Mark Correct" 59 commands = ['markcorrect', 'mark'] 60 usage = ("markcorrect", "marks all the lexical entries in the "\ 61 "chart that are correct according to the gold standard") 62 help = """ 63 Goes through the chart and marks those lexical signs that were the 64 correct choice according to the gold standard. Each correct sign has its 65 __str__ wrapped so it will display a marker of its correctness when 66 printed. 67 """ 68
69 - def run(self, args, state):
70 from .shell import ShellError 71 # Only allow this to be run once 72 if hasattr(state, '_markcorrect_run'): 73 raise ShellError, "Correct entries have already been marked" 74 id = _get_seq_id(state) 75 _check_gs_has_sequence(state, id) 76 # We've got an id and the right GS sequence 77 gs_seq = state.gs_sequences.sequence_by_id(id) 78 79 # Decorate the str method of Sign so that we can put in an extra marker 80 def _mark_str(str_fn): 81 def _correct_str(obj): 82 prepend = getattr(obj, '_str_prepend', '') 83 return "%s%s" % (prepend, str_fn(obj))
84 return _correct_str
85 state.formalism.Syntax.Sign.__str__ = _mark_str(state.formalism.Syntax.Sign.__str__) 86 87 # Go through each chord in the gold standard 88 for i,chord in enumerate(gs_seq.iterator()): 89 tag = chord.category 90 found = False 91 for sign in state.parser.chart.get_signs(i, i+1): 92 # If the sign stores a tag we can check whether it was right 93 if hasattr(sign, 'tag') and sign.tag == tag: 94 found = True 95 print "Found correct tag for position %d: %s" % (i,sign) 96 sign._str_prepend = "***" 97 if not found: 98 print "Correct tag not found for position %d" % i 99 state._markcorrect_run = True 100
101 -def _check_gs_loaded(state):
102 from .shell import ShellError 103 if not hasattr(state, 'gs_sequences'): 104 raise ShellError, "The no gold standard has been loaded. Use the loadgs command to load a file."
105
106 -def _check_gs_has_sequence(state, id):
107 _check_gs_loaded(state) 108 if id not in state.gs_sequences.ids: 109 raise ShellError, "The loaded gold standard doesn't include the sequence with id %s" % id
110
111 -def _get_seq_id(state):
112 from .shell import ShellError 113 if state.seq_id is None: 114 raise ShellError, "Not sequence id available for parsed sequence: cannot run this tool" 115 return state.seq_id
116