def run(self, depth, wgt=None, duration=None): rec = 0 timer = False if duration is not None: timer = True start = time.time() while (not timer) or (not Utils.time_elapsed(start, duration)): sys.stdout.write(".") sys.stdout.flush() try: t2 = time.time() s = self.next(depth, wgt) t3 = time.time() out = Accent.run(self._sin.parser, s) t4 = time.time() if Accent.was_ambiguous(out): print "\n==> sentence[gen=%.6f parse=%.6f (secs)]: %s" % ((t3 - t2), (t4 - t3), s) print print "".join(out) return True, s, out except RuntimeError as err: if "maximum recursion depth exceeded" in err.message: rec += 1 sys.stderr.write("\nr:%s\n" % rec) sys.stderr.flush() else: # track other errors print "error: \n" print traceback.format_exc() sys.exit(2) return False, None, None
def verify_ambiguity(self, mingp, minlp, minsen, duration=None): print "==> verify grammar %s with minimiser %s \n" % \ (mingp, self._sin.minp) self._sin.lex = Lexer.parse(open(self._sin.lp, 'r').read()) self._sin.cfg = CFG.parse(self._sin.lex, open(self._sin.gp, "r").read()) self._sin.parser = Accent.compile(self._sin.gp, self._sin.lp) minlex = Lexer.parse(open(minlp, 'r').read()) mincfg = CFG.parse(minlex, open(mingp, 'r').read()) seq = mincfg.get_rule('root').seqs[0] # check if the root rule of minimised cfg == root of original cfg if (len(seq) == 1) and (str(seq[0]) == self._sin.cfg.start_rulen): out = Accent.run(self._sin.parser, minsen) if Accent.was_ambiguous(out): print "** verified **" minbend = "%sm" % self._sin.backend if minbend in Backends.BACKENDS: bend = Backends.BACKENDS[minbend](self._sin, mincfg, minsen) else: bend = Backends.WGTBACKENDS[minbend](self._sin, mincfg, minsen) # we keep trying until we hit the subseq while not bend.found: bend.run(self._sin.t_depth, self._sin.wgt, duration) print "** verified **"
def run_accent(self, sen, gp, lp): """ build parser in td using gp+lp, and parse sentence sen.""" parser = Accent.compile(gp, lp) ptrees = Accent.run(parser, sen) ambi_parse = AmbiParse.parse(lp, self._sin.lex_ws, ptrees) _gp = tempfile.mktemp('.acc', dir=self._sin.td) _lp = tempfile.mktemp('.lex', dir=self._sin.td) self.write_cfg_lex(ambi_parse, _gp, _lp) return _gp, _lp, ambi_parse
def run_accent(self, sen, gp, lp, td): """ build parser in td using gp+lp, and parse sentence sen.""" parser = Accent.compile(gp, lp) out = Accent.run(parser, sen) ambiparse = AmbiParse.parse(self, out) _gp = tempfile.mktemp('.acc', dir=td) _lp = tempfile.mktemp('.lex', dir=td) MiniUtils.write_cfg_lex(ambiparse.min_cfg, _gp, lp, _lp) return _gp, _lp
def run_accent(self, sen, gf, lf): """ build parser in td using gf+lf, and parse sentence sen.""" parser = Accent.compile(gf, lf) ptrees = Accent.run(parser, sen) ambi_parse = AmbiParse.parse(lf, self._sin.lex_ws, ptrees, sen) #_gp = tempfile.mktemp('.acc', dir=self._sin.td) #_lp = tempfile.mktemp('.lex', dir=self._sin.td) #_ambstrp = os.path.join(self._sin.td, "%s.ambs" % 'accent') #self.write_cfg_lex(ambi_parse, _gp, _lp, _ambstrp) return ambi_parse
def run(self, parserp, depth, wgt=None, duration=None): rec = 0 timer = False if duration is not None: timer = True start = time.time() while ((not timer) or (not Utils.time_elapsed(start, duration))): sys.stdout.write(".\n=====>\n") sys.stdout.flush() try: t2 = time.time() s = self.next(depth, wgt) t3 = time.time() out = Accent.run(parserp, s) t4 = time.time() if Accent.was_ambiguous(out): print "\n==> sentence[gen=%.6f parse=%.6f (secs)]: %s" % \ ((t3-t2), (t4-t3), s) print print "".join(out) return True, s, out except RuntimeError as err: if "maximum recursion depth exceeded" in err.message: rec += 1 sys.stderr.write("\nr:%s\n" % rec) sys.stderr.flush() # useful to know what dynamic3 found before hitting recursion if self._sin.backend in ['dynamic3']: print "=======" for rule in self._cfg.rules: if rule.finite_depth is not None: print "[%s], %s" % (rule.finite_depth, rule) else: # track other errors print "error: \n" print traceback.format_exc() sys.exit(2) return False, None, None