Package jazzparser :: Package parsers :: Package base :: Module parser
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.parsers.base.parser

  1  """Parser module common base class. 
  2   
  3  This provides the common base classes for parser modules. 
  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  from jazzparser.utils.options import ModuleOption 
 31  from jazzparser.utils.loggers import create_plain_stderr_logger 
 32  from .. import ParseError 
33 34 -class Parser(object):
35 """ 36 Base class for all parsers. 37 """ 38 shell_tools = [] 39 # Parser-specific options. List of ModuleOptions 40 PARSER_OPTIONS = [] 41
42 - def __init__(self, grammar, tagger, options={}, backoff=None, 43 backoff_options={}, logger=None):
44 """ 45 @param grammar: the L{jazzparser.grammar.Grammar} instance to use for 46 parsing 47 @param tagger: the L{jazzparser.taggers.tagger.Tagger} subclass 48 instance to use to tag the input 49 @param backoff: an optional 50 L{jazzparser.backoff.base.BackoffBuilder} class 51 to use as a fallback if the parser returns no parses. Whether 52 this is used and in what circumstances depends on the type of 53 parser. 54 @param backoff_options: dictionary of options to pass to the backoff 55 model if it gets used. 56 @type logger: C{logging.Logger} 57 @param logger: a logger to which all progress information during 58 parsing will be written. By default, outputs to stderr. 59 60 """ 61 self.grammar = grammar 62 self.tagger = tagger 63 self.backoff_options = backoff_options 64 if backoff is not None: 65 # Look up the backoff model if one is requested 66 self.backoff = backoff 67 # Pre-check the options dict 68 # This will be done again by the module when instantiated, but 69 # we do it now to verify the options 70 ModuleOption.process_option_dict(backoff_options, 71 backoff.BUILDER_OPTIONS) 72 else: 73 self.backoff = None 74 # Initialize using parser-specific options 75 self.options = type(self).check_options(options) 76 77 if logger is None: 78 # Output to stderr instead 79 self.logger = create_plain_stderr_logger() 80 else: 81 self.logger = logger 82 83 self.timed_out = False
84
85 - def parse(self, derivations=False, summaries=False):
86 """ 87 Should be implemented by subclasses. 88 """ 89 raise NotImplementedError, "Parser.parse() should be implemented by parser subclasses."
90
91 - def run_backoff(self):
92 """ 93 Convenience method. Uses the backoff model on the input supplied 94 to the tagger and returns the results. If no backoff model has been 95 given, returns an empty list. 96 97 """ 98 if self.backoff is None: 99 return [] 100 if self.tagger.original_input is None: 101 raise ParseError, "tagger %s did not store the input in its "\ 102 "original form, so we can't now use a backoff model" % \ 103 type(self.tagger).__name__ 104 builder = self.backoff(self.tagger.original_input, 105 options=self.backoff_options, 106 logger=self.logger) 107 return builder.get_all_paths()
108 109 @classmethod
110 - def check_options(cls, options):
111 """ 112 In normal parser usage, the options dictionary is checked for 113 validity when the parser is instantiated. In this interface, you may 114 want to check the options before this point using this method. 115 116 """ 117 return ModuleOption.process_option_dict(options, cls.PARSER_OPTIONS)
118