Ejemplo n.º 1
0
    def run(self, hogwash_job):
        print >>sys.stderr, "Loading grammar", self.grammar

        grammar = HierGrammar(self.grammar)

        print >>sys.stderr, "Done"

        print >>sys.stderr, "Parse options:"
        print >>sys.stderr, self.parseOpts

        self.parseOpts["grammar"] = grammar

        if self.parserType == "standard":
            parser = Parser(**self.parseOpts)
        elif self.parserType == "ctf":
            parser = CTFParser(**self.parseOpts)
        else:
            raise TypeError("Don't know parser type %s" % self.parserType)

        print >>sys.stderr, "Parsing:", self.sentence

        try:
            final = parser.parse(self.sentence)
        except (ParseError, TypeError):
            #if psyco is active, throwing a parse error will fail
            #because psyco doesn't realize that exceptions can be
            #newstyle classes, because it's *old*
            #so we get a type error
            final = parser.parseFail(self.sentence)

        print treeToStr(normalizeTree(final.tree()))

        return final.tree()
Ejemplo n.º 2
0
    def run(self, hogwash_job):
        print >>sys.stderr, "Loading grammar", self.grammar

        grammar = HierGrammar(self.grammar)

        print >>sys.stderr, "Done"

        print >>sys.stderr, "Parse options:"
        print >>sys.stderr, self.parseOpts

        self.parseOpts["grammar"] = grammar

        parser = Parser(**self.parseOpts)

        print >>sys.stderr, "Parsing:", self.sentence

        try:
            final = parser.parse(self.sentence)
            res = treeToStr(normalizeTree(final.tree()))
        except (ParseError, TypeError):
            #if psyco is active, throwing a parse error will fail
            #because psyco doesn't realize that exceptions can be
            #newstyle classes, because it's *old*
            #so we get a type error
            final = parser.parseFail(self.sentence)
            res = treeToStr(normalizeTree(final.tree()))
        print res

        return res
Ejemplo n.º 3
0
    def afterGenerating(self, hyps, i, sentence):
        if not hyps[i + 1]:
            #fail?
            return

        for level,gamma in enumerate(self.gammas):
            if level == 0:

                if "level" in self.verbose:
                    print >>sys.stderr, "Specified", \
                          len(hyps[i + 1]), "hypotheses"
                    bestSub = hyps[i + 1][0]
                    print >>sys.stderr, "Best parse at level", level,\
                          treeToStr(bestSub.tree(allowPartial=True)),\
                          bestSub.prob
                    print >>sys.stderr, bestSub.expansionProfile(bestSub)

                continue

            if "level" in self.verbose:
                print >>sys.stderr, "Specifying at level", level

            self.specifyAtLevel(1, level, hyps[i + 1], 1.0, sentence)

            if "level" in self.verbose:
                gen = hyps[i + 1]

                try:
                    for subL in range(level):
                        gen = gen[0].subLevelHeap

                    bestSub = gen[0]
                    print >>sys.stderr, "Best parse at level", level,\
                          treeToStr(bestSub.tree(allowPartial=True)),\
                          bestSub.prob
                    print >>sys.stderr, bestSub.expansionProfile(bestSub)
                except:
                    print >>sys.stderr, \
                          "Warning: best hypothesis has no parse at level",\
                          level

                if self.verbose:
                    reportMatchingParses(self.verbose, hyps[i + 1], 0)
                    reportMatchingParses(self.verbose, hyps[i + 1], level)

        if "level" in self.verbose:
            bestSub = hyps[i + 1][0]
            print >>sys.stderr, "Best top-level parse",\
                  treeToStr(bestSub.tree(allowPartial=True)),\
                  bestSub.prob
            print >>sys.stderr, bestSub
            print >>sys.stderr, bestSub.expansionProfile(bestSub)
Ejemplo n.º 4
0
from DBGrammar import DBGrammar

if __name__ == "__main__":
    inf = sys.argv[1]

    print >>sys.stderr, "loading grammar", inf

    grammar = DBGrammar(inf)

    print >>sys.stderr, "done"

    debug = ["index", "pop", "push", "threshold"]
    parser = Parser(grammar, top="ROOT_0", mode="lex",
                    queueLimit=5e5,
                    verbose=["index"])

    sent = "The stocks fell ."
#    sent = "Members of the House Ways and Means Committee introduced legislation that would restrict how the new savings-and-loan bailout agency can raise capital , creating another potential obstacle to the government 's sale of sick thrifts ."
#    sent = "The government 's plan"
#    sent = "John Smith and Mary Roe are friends ."

    #import cProfile
    #final = cProfile.run('parser.parse(sent.split())', 'profile-out4')

    final = parser.parse(sent.split())
    print final
    print list(final.derivation())
    print treeToStr(final.tree())
    print treeToStr(normalizeTree(final.tree()))
    print treeToStr(final.tree(True))
Ejemplo n.º 5
0
        # queueLimit=5e5,
        verbose=["index", "level", tpar],
        gammas=[1e-11, 1e-10, 1e-9, 1e-8],
        deltas=[1e-5, 1e-5, 1e-5],
        stepExpansionLimit=100,
    )

    #    sent = "The stocks fell ."
    #    sent = "John Smith and Mary Roe are friends ."

    sent = "Food prices are expected to be unchanged , but energy costs jumped as much as 4 % , said Gary Ciminero , economist at Fleet\/Norstar Financial Group ."

    final = parser.parse(sent.split())
    print final
    print list(final.derivation())
    print treeToStr(final.tree())
    print treeToStr(normalizeTree(final.tree()))
    print treeToStr(final.tree(True))

    level = 0
    while final.subLevelHeap:
        level += 1
        final = final.subLevelHeap[0]
        print
        print
        print "Level", level
        print final
        print list(final.derivation())
        print treeToStr(final.tree())
        print treeToStr(normalizeTree(final.tree(), stripSub=False))
        print treeToStr(final.tree(True))
Ejemplo n.º 6
0
from Hogwash import Session #main hogwash class
from Hogwash.Results import ResultsFile #type for file created by hw job
from Hogwash.Action import Action #supertype for runnable objects
from Hogwash.Errors import BadExitCode #error if the program crashed
from waterworks.Processes import bettersystem #run an external command

import sys
from path import path
import os
from shutil import copy
from iterextras import batch

from StringIO import StringIO #store output of process

from distributedParser import Parse
from topdownParser import Parser, Grammar, treeToStr, normalizeTree

if __name__ == "__main__":
    session = Session(sys.argv[1], read_only=True, verbose=0)

    p = Parser(Grammar({}))

    for job in session[:100]:
        if job.status != "finished":
            sent = job.args[0].sentence
            fail = p.parseFail(sent)
            print treeToStr(normalizeTree(fail.tree(), unbinarize=True))
        else:
            print treeToStr(normalizeTree(job.results, unbinarize=False),
                            epsilonSym="EPSILON")