Esempio n. 1
0
    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 **"
Esempio n. 2
0
    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
Esempio n. 3
0
    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