def __init__( self, resource_loader=None, config=None, allow_relaxed=True, domain=None, intent=None, ): """Initializes the parser Args: resource_loader (ResourceLoader): An object which can load resources for the parser. config (dict, optional): The configuration for the parser. If none is provided the app config will be loaded. """ if not resource_loader and not config: raise ValueError( "Parser requires either a configuration or a resource loader") app_path = resource_loader.app_path if resource_loader else None try: entity_types = path.get_entity_types(app_path) + ["unk"] except TypeError: entity_types = {"unk"} self._resource_loader = resource_loader self.config = get_parser_config(app_path, config, domain, intent) or {} configured_entities = set() for entity_type, entity_config in self.config.items(): configured_entities.add(entity_type) configured_entities.update(entity_config.keys()) self._configured_entities = configured_entities rules = generate_grammar(self.config, entity_types) self._grammar = FeatureGrammar.fromstring(rules) self._parser = FeatureChartParser(self._grammar) if allow_relaxed: relaxed_rules = generate_grammar(self.config, entity_types, relaxed=True) self._relaxed_grammar = FeatureGrammar.fromstring(relaxed_rules) self._relaxed_parser = FeatureChartParser(self._relaxed_grammar) else: self._relaxed_grammar = None self._relaxed_parser = None
def main(): ugrammar = FeatureGrammar.fromstring(rules) uparser = FeatureChartParser(ugrammar) index = 0 for sent in text: index += 1 print_tree(sent, uparser, index) print "Input testing sentece or the number of the above one: (q to quit)" str = sys.stdin.readline().strip() while str != "q": try: index = int(str) print_tree(text[index], uparser, index) except IndexError: print "Index out of range. Please check." except ValueError: print_tree(str, uparser, -1) print "Input testing sentece or the number of the above one: (q to quit)" str = sys.stdin.readline().strip()
def main(argv): """ Loads grammar files from command-line args then parses lines from standard input. """ parser = argparse.ArgumentParser() parser.add_argument(dest='grammars', nargs='+', help='Grammar file path(s)') parser.add_argument('--draw', dest='draw', action='store_true', help='Draw trees') parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Be verbose') args = parser.parse_args(argv) grammar = load_grammars(args.grammars) parser = FeatureChartParser(grammar, trace=args.verbose, trace_chart_width=80) line = sys.stdin.readline() while line: if line[0] == '#': continue tokens = line.lower().strip().split() if len(tokens) == 0: continue trees = list(parser.parse(tokens)) print('*** {} ***'.format(tokens)) if trees: for tree in trees: print(tree.pformat(margin=80)) if args.draw: tree.draw() # print(TreePrettyPrinter(tree).text()) else: print('Could not parse {}'.format(tokens)) print('\n') line = sys.stdin.readline()
ADV -> 'always' | 'never' | 'intensely' ############## Preposition ################ P -> 'in' | 'before' | 'after' | 'when' | 'on' | 'beyond' | 'from' | 'to' | 'at' ######## Auxiliary ################# AUX[FORM=base, NUM=plur] -> 'do' AUX[FORM=vbz, NUM=sing] -> 'does' AUX[FORM=pret] -> 'did' AUX[FORM=pastpart, NUM=sing] -> 'has' AUX[FORM=pastpart, NUM=plur] -> 'have' """) uparser = FeatureChartParser(ugrammar) text = """\ Bart laughs Homer laughed Bart and Lisa drink milk Bart wears blue shoes Lisa serves Bart a healthy green salad Homer serves Lisa Bart always drinks milk Lisa thinks Homer thinks Bart drinks milk Homer never drinks milk in the kitchen before midnight when Homer drinks milk Bart laughs when does Lisa drink the milk on the table when do Lisa and Bart wear shoes Bart thinks Lisa drinks milk on the table
import nltk from nltk import FeatureChartParser fcfg = nltk.data.load('P2.fcfg') parser = FeatureChartParser(fcfg) def parse_text(text): examples = text.splitlines() for sent in examples: print(sent) parses = parser.parse(sent.split()) for tree in parses: print(tree) def parse_file(name): f = open(name, 'r') text = f.read() f.close() parse_text(text) print("================ Positive examples ================") parse_file('P2.pos') print("================ Negative examples ================") parse_file('P2.neg')
def unification_grammar(): ugrammar = FeatureGrammar.fromstring("""\ ################### RULES ################# S -> NP[NUM=?n] VP[NUM=?n] S -> PREP_P S S -> Wh_P AUX[NUM=?n] NP[NUM=?n] VP NP[NUM=?n] -> ProperNoun[NUM=?n] NP[NUM=?n] -> N[NUM=?n] | ADJ_P NP[NUM=?n] | DET[NUM=?n] NP[NUM=?n] | N[NUM=?n] PREP_P | ADJ_P NP[NUM=?n] -> ProperNoun[NUM=?n] GER_P | GER_P NP[NUM=pl] -> NP[NUM=?n] CC NP[NUM=?n] VP[SUBCAT=?rest, NUM=?n] -> V[NUM=?n, SUBCAT=?rest] | VP[NUM=?n, TENSE=?t, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=?rest, NUM=?n] -> ADV_P V[NUM=?n, SUBCAT=?rest] | V[NUM=?n, SUBCAT=?rest] ADV_P VP[SUBCAT=?rest, NUM=?n] -> MOD_P VP[TENSE=?t, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=?rest, NUM=?n] -> VTB[NUM=?n, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=?rest, NUM=?n] -> VTB VP[SUBCAT=?rest] GER_P -> GER NP ADJ_P -> ADJ | ADJ ADJ_P ADV_P -> ADV | ADV ADV_P PREP_P -> PREP NP | PREP S MOD_P -> MOD AUX[NUM=pl] | MOD ADV AUX[NUM=pl] Wh_P -> Wh | Wh ARG[CAT=?arg] ARG[CAT=np] -> NP ARG[CAT=pp] -> PREP_P ARG[CAT=s] -> S ################# Lexicons ################# ################## NOUN ################### ########################################### ProperNoun[NUM=sg] -> 'Homer' | 'Bart' | 'Lisa' N[NUM=sg] -> 'milk' | 'salad' | 'midnight' | 'kitchen' | 'table' N[NUM=pl] -> 'shoes' | 'tables' ################# VERB #################### ########################################### ############### PRESENT ################### #########----- Intransitive -----########## V[TENSE=pres, NUM=sg, SUBCAT=nil]-> 'laughs' | 'smiles' | 'walks' | 'serves' | 'drinks' V[TENSE=pres, NUM=pl, SUBCAT=nil] -> 'laugh' | 'smile' | 'walk' | 'serve' |'drink' #########----- Transitive ------########### V[TENSE=pres, NUM=sg, SUBCAT=[HEAD=s,TAIL=nil]] -> 'thinks' | 'believes' V[TENSE=pres, NUM=pl, SUBCAT=[HEAD=s,TAIL=nil]] -> 'think' | 'believe' V[TENSE=pres, NUM=sg, SUBCAT=[HEAD=np,TAIL=nil]] ->'serves' | 'drinks' | 'wears' | 'likes' V[TENSE=pres, NUM=pl, SUBCAT=[HEAD=np,TAIL=nil]] ->'serve' | 'drink' | 'wear' | 'like' V[TENSE=pres, NUM=sg, SUBCAT=[HEAD=pp,TAIL=nil]] ->'walks' | 'teaches' V[TENSE=pres, NUM=pl, SUBCAT=[HEAD=pp,TAIL=nil]] ->'walk' | 'teach' ######### primary & secondary ######## V[TENSE=pres, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=np,TAIL=nil]]] -> 'serves' V[TENSE=pres, NUM=pl, SUBCAT=[HEAD=np, TAIL=[HEAD=np,TAIL=nil]]] -> 'serve' V[TENSE=pres, NUM=pl, SUBCAT=[HEAD=s, TAIL=[HEAD=np,TAIL=nil]]] -> 'think' | 'believe' ################# Past #################### #########----- Intransitive -----########## V[TENSE=past, SUBCAT=nil] -> 'laughed' | 'smiled' | 'walked' #########----- Transitive ------########### V[TENSE=past, SUBCAT=[HEAD=np,TAIL=nil]] -> 'drank' | 'wore' | 'served' V[TENSE=pastpart, SUBCAT=[HEAD=np,TAIL=nil]] ->'drunk' | 'worn' | 'served' | 'seen' ############### PRESENT CONT. ############# V[TENSE=prescon, FORM=prespart , SUBCAT=[HEAD=np,TAIL=nil]] -> 'drinking' | 'wearing' V[TENSE=prescon, FORM=prespart , SUBCAT=[HEAD=pp,TAIL=nil]] -> 'drinking' ################ Determiner ############### DET[NUM=sg] -> 'a' | 'the' | 'that' DET[NUM=pl] -> 'the' | 'these' | 'those' ################ Conjunction ############## CC -> 'and' ################## Modal ################## MOD -> 'may' ################# Gerund ################# GER -> 'drinking' ############ Adverb & Adjective ############ ADJ -> 'blue' | 'healthy' | 'green' | 'same' ADV -> 'always' | 'never' | 'not' | 'yesterday' ############## Preposition ################## PREP -> 'in' | 'before' | 'when' | 'on' AUX[NUM=sg] -> 'does' | 'has' AUX[NUM=pl] -> 'do' | 'have' VTB[NUM=sg] -> 'is' VTB[NUM=pl] -> 'are' Wh -> 'when' | 'what' | 'where' | 'whom' """) uparser = FeatureChartParser(ugrammar) sents = text_extended.splitlines() for sent in sents: parses = uparser.parse(sent.split()) print(sent) for tree in parses: print(tree)
V[FORM=?vbz] -> 'laughs' | 'wears' | 'serves' | 'drinks' | 'thinks' | 'does' | 'do' V[FORM=?pret] -> 'laughed' V[FORM=?base] -> 'drink' | 'wear' | 'laugh' DET[NUM=?sg] -> 'a' | 'the' Adjective[CAT=?des] -> 'healthy' Adjective[CAT=?col] -> 'blue' | 'green' Adverb[T=?des] -> 'always' | 'never' | 'before' Preposition[T=?gen] -> 'in' | 'on' NN[NUM=?sg] -> 'shoes' | 'salad' | 'milk' | 'kitchen' | 'midnight' | 'table' NN[NUM=?pl] -> 'milk' | 'shoes' ConjuctiveJoin[NUM=?pl] -> 'and' ConjuctiveConditional[T=?con] -> 'when' ConjuctiveConditional[T=?quest] -> 'when' """) grammar_feature_parser = FeatureChartParser(grammar_feature) #''' def _parse_and_print_unification(sents): for i in range(len(sents)): parses = grammar_feature_parser.parse(sents[i].split()) print("\r\n") print "Sentence", i + 1, " : ", print(sents[i]) for tree in parses: print(tree) _parse_and_print_unification(sents_split)