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

Source Code for Module jazzparser.parsers.fail.parser

  1  """A parser that always fails to find a result. 
  2   
  3  This is a dummy parser that allows other components to be tested in their  
  4  natural habitat without a parser getting in the way. 
  5   
  6  It just keeps requesting signs from the supertagger until it has none  
  7  left and then fails. 
  8   
  9  """ 
 10  """ 
 11  ============================== License ======================================== 
 12   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 13    
 14   This file is part of The Jazz Parser. 
 15    
 16   The Jazz Parser is free software: you can redistribute it and/or modify 
 17   it under the terms of the GNU General Public License as published by 
 18   the Free Software Foundation, either version 3 of the License, or 
 19   (at your option) any later version. 
 20    
 21   The Jazz Parser is distributed in the hope that it will be useful, 
 22   but WITHOUT ANY WARRANTY; without even the implied warranty of 
 23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 24   GNU General Public License for more details. 
 25    
 26   You should have received a copy of the GNU General Public License 
 27   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
 28   
 29  ============================ End license ====================================== 
 30   
 31  """ 
 32  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
 33   
 34  import logging 
 35   
 36  from jazzparser.utils.base import filter_latex, ExecutionTimer 
 37  from jazzparser.parsers.cky.parser import CkyParser, ParserTimeout 
 38   
 39  from jazzparser import settings 
 40   
 41  # Get the logger from the logging system 
 42  logger = logging.getLogger("main_logger") 
 43   
44 -class FailParser(CkyParser):
45 """ 46 A dummy parser that never does any actual parsing. 47 48 """
49 - def parse(self, derivations=False, summaries=False, inspect=False):
50 # Find out from the tagger how long the input it read in was 51 input_length = self.tagger.input_length 52 # Create and initialise a chart for parsing 53 # Don't initialise the chart with signs - we'll add signs gradually instead 54 chart = self._create_chart( 55 [[]]*input_length, 56 derivations=derivations) 57 58 # Stop after a given number of iterations 59 if self.options['max_iter'] == 0: 60 max_iter = None 61 else: 62 max_iter = self.options['max_iter'] 63 timeout = 60*self.options['timeout'] 64 check_timeout = timeout>0 65 66 # This is where progress output will go 67 # Note that it's not the same as logger, which is the main system logger 68 prog_logger = self.logger 69 70 if check_timeout: 71 prog_logger.info("Due to timeout after %d mins" % self.options['timeout']) 72 73 ################################################## 74 ### The dummy parse loop 75 # Keep track of how long since we started for timing out 76 timeout_timer = ExecutionTimer() 77 78 signs_added = True 79 offset = 0 80 try: 81 # Keep adding signs until none left 82 while len(chart.get_signs(0, input_length)) == 0 and signs_added: 83 if max_iter is not None and offset >= max_iter: 84 # Exceded maximum number of iterations: give up 85 prog_logger.info("Reached maximum number of iterations: "\ 86 "continuing to backoff/fail") 87 break 88 prog_logger.info(">>> Parsing iteration: %d" % (offset+1)) 89 90 # Get new signs from the tagger 91 added = self._add_signs(offset=offset) 92 # Note whether we added anything new 93 signs_added = bool(added) 94 95 # Check whether the timeout has expired 96 if check_timeout: 97 if int(timeout_timer.get_time()) > timeout: 98 raise ParserTimeout 99 # Don't do any parsing: just go round again to get more signs 100 offset += 1 101 except ParserTimeout: 102 # The given timeout elapsed: just continue with no parses 103 logger.debug("Parse loop timeout (%d mins) expired" % self.options['timeout']) 104 prog_logger.info("Parse timeout (%d mins) expired: continuing "\ 105 "to backoff/fail" % self.options['timeout']) 106 107 parses = [] 108 # If a backoff model was given, always use it now 109 if self.backoff is not None: 110 backoff_results = self.run_backoff() 111 if len(backoff_results) > 0: 112 for res in backoff_results: 113 # Put the semantics result into a sign, with a dummy 114 # syntactic category 115 sign = self.grammar.formalism.Syntax.Sign( 116 self.grammar.formalism.Syntax.DummyCategory(), 117 res) 118 # If the semantics has a probability, put this on the sign 119 if hasattr(res, "probability"): 120 sign.probability = res.probability 121 parses.append(sign) 122 123 return parses
124