def __init__(self, discourse_id=None): """ This class is used to parse the Prolog DRS output from Boxer into a hierarchy of python objects. """ DrtParser.__init__(self) self.discourse_id = discourse_id self.sentence_id_offset = None self.quote_chars = [("'", "'", "\\", False)]
def drs_matcher(s, p, QA_services, debug="no"): res = [] parser = load_parser(p, trace=0, logic_parser=DrtParser()) #NotImlementedError try: for tree in parser.parse(s.split()): if ( debug == "yes" ): #forwarding output to do further testing when developing new grammar rules res.append(tree) elif (debug == "no"): res.append(str(tree.label()['SEM'].simplify())) #if we have a failure in the parser tree, return False or rethrow exception except: print('exception occurred') if ( debug == "yes" ): #forwarding output to do further testing when developing new grammar rules raise elif (debug == "no"): return False if ( debug == "yes" ): #forwarding output to do further testing when developing new grammar rules return res elif (debug == "no"): if res != []: #return the DRS obtained from the first tree of the forest, if it exist #TODO: look for a tree with same structure as published by GNLP (provided it exist and it makes sense) DRS = res[0] QA_services.write_to_KB(DRS, TAG_DRS) return True else: return False
def __init__(self, discourse_id=None): DrtParser.__init__(self) self.discourse_id = discourse_id
def parse(self, data, signature=None): self._label_counter = Counter(-1) return DrtParser.parse(self, data, signature)
def parse(self, data, signature=None): return DrtParser.parse(self, data, signature)
print(dexpr(s)) # The fol() method converts DRSs into FOL formulae. print(dexpr(r'([x],[man(x), walks(x)])').fol()) print(dexpr(r'([],[(([x],[man(x)]) -> ([],[walks(x)]))])').fol()) # In order to visualize a DRS, the pretty_format() method can be used. print(drs3.pretty_format()) # PARSE TO SEMANTICS # DRSs can be used for building compositional semantics in a feature based grammar. To specify that we want to use DRSs, the appropriate logic parser needs be passed as a parameter to load_earley() from nltk.parse import load_parser from nltk.sem.drt import DrtParser parser = load_parser('grammars/book_grammars/drt.fcfg', trace=0, logic_parser=DrtParser()) for tree in parser.parse('a dog barks'.split()): print(tree.label()['SEM'].simplify()) # Alternatively, a FeatStructReader can be passed with the logic_parser set on it from nltk.featstruct import FeatStructReader from nltk.grammar import FeatStructNonterminal parser = load_parser('grammars/book_grammars/drt.fcfg', trace=0, fstruct_reader=FeatStructReader( fdict_class=FeatStructNonterminal, logic_parser=DrtParser())) for tree in parser.parse('every girl chases a dog'.split()): print(tree.label()['SEM'].simplify().normalize())