def extend_grammar(g, p): '''Extend a FeatureGrammar object with a new (string represented) production.''' start = g._start old = g._productions new = FeatureGrammar.fromstring(p)._productions ret = FeatureGrammar(start, old + new) return ret
def __init__(self, start, productions): """ Create a new feature-based grammar, from the given start state and set of ``Productions``. :param start: The start symbol :type start: FeatStructNonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) """ FeatureGrammar.__init__(self, start, productions)
def __init__(self, start, productions): """ Create a new feature-based grammar, from the given start state and set of ``Productions``. :param start: The start symbol :type start: FGFeatStructNonterminal :param productions: The list of productions that defines the grammar :type productions: list(Production) """ FeatureGrammar.__init__(self, start, productions)
def demo_grammar(): from nltk.grammar import FeatureGrammar return FeatureGrammar.fromstring( """ S -> NP VP PP -> Prep NP NP -> NP PP VP -> VP PP VP -> Verb NP VP -> Verb NP -> Det[pl=?x] Noun[pl=?x] NP -> "John" NP -> "I" Det -> "the" Det -> "my" Det[-pl] -> "a" Noun[-pl] -> "dog" Noun[-pl] -> "cookie" Verb -> "ate" Verb -> "saw" Prep -> "with" Prep -> "under" """ )
def __init__(self): self.grammar = FeatureGrammar.fromstring(""" %start S S -> 'a' S 'b' S -> 'a' 'b' S -> B B -> 'a' 'b' """) self.earley = nltk.parse.EarleyChartParser(self.grammar)
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 load_grammars(grammar_paths, start_symbol=None): """Loads grammars from a list of files and combines them into a single feature grammar""" if start_symbol is None: start_symbol = FeatStructNonterminal('Sentence') productions = [] for path in grammar_paths: g = nltk.data.load(path, format='fcfg') productions.extend(g.productions()) grammar = FeatureGrammar(start_symbol, productions) return grammar
def load_grammar( grammar_file: str, lexicon_file: str = "grammars/lexicon.fcfg") -> FeatureGrammar: with open(grammar_file) as g_file: grammar_text = g_file.read() grammar_text += "\n\n" with open(lexicon_file) as l_file: grammar_text += l_file.read() return FeatureGrammar.fromstring(grammar_text)
def parseFoma(sentence): tokens = sentence.split() tokenAnalyses = {} rules = [] count = 0 for token in tokens: aVal = [] result = list(fst.apply_up(str.encode(token))) for r in result: elements = r.decode('utf8').split('+') print(r.decode('utf8')) lemma = elements[0] tokFeat = nltk.FeatStruct("[PRED=" + lemma + "]") cat = elements[1] if len(elements) > 2: feats = tuple(elements[2:]) else: feats = () for x in feats: fRes2 = feat2LFG(x) fRes = tokFeat.unify(fRes2) if fRes: tokFeat = fRes else: print("Error unifying:", tokFeat, fRes2) flatFStr = flatFStructure(tokFeat) aVal.append(cat + flatFStr) rules.append(cat + flatFStr + " -> " + "'" + token + "'") tokenAnalyses[count] = aVal count += 1 grammarText2 = grammarText + "\n" + "\n".join(rules) grammar = FeatureGrammar.fromstring(grammarText2) parser = nltk.parse.FeatureChartParser(grammar) result = list(parser.parse(tokens)) if result: for x in result: print(x) else: print("*", sentence)
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 demo_legacy_grammar(): """ Check that interpret_sents() is compatible with legacy grammars that use a lowercase 'sem' feature. Define 'test.fcfg' to be the following """ from nltk.grammar import FeatureGrammar g = FeatureGrammar.fromstring(""" % start S S[sem=<hello>] -> 'hello' """) print("Reading grammar: %s" % g) print("*" * 20) for reading in interpret_sents(["hello"], g, semkey="sem"): syn, sem = reading[0] print() print("output: ", sem)
def demo_legacy_grammar(): """ Check that interpret_sents() is compatible with legacy grammars that use a lowercase 'sem' feature. Define 'test.fcfg' to be the following """ from nltk.grammar import FeatureGrammar g = FeatureGrammar.fromstring(""" % start S S[sem=<hello>] -> 'hello' """) print("Reading grammar: %s" % g) print("*" * 20) for reading in interpret_sents(['hello'], g, semkey='sem'): syn, sem = reading[0] print() print("output: ", sem)
def parse_to_prolog(grammar_str: str) -> List[str]: """ Use to transform NLTK grammar into prolog predicates. Support both feature grammar and vanilla context free grammar. Grammar that has the form : # Rules s -> np vp np -> n | det n ... # Lexicon v -> 'want' | 'wants' Will be transform to a list of prolog predicates rule(s, [np, vp]) rule(np, [n]) rule(np, [det, n]) ... rule(v, ['want']) terminal('want) """ nltk_featgrammar = FeatureGrammar.fromstring(grammar_str) features_dict = compute_features_dict(nltk_featgrammar.productions()) pl_predicates = [] for production in nltk_featgrammar.productions(): lhs = parse_term(production.lhs(), features_dict) rhs = [parse_term(term, features_dict) for term in production.rhs()] pl_predicates.append("rule(%s, [%s])" % (lhs, ", ".join(rhs))) terminal_symbols = compute_terminal_symbols(nltk_featgrammar.productions()) for terminal in terminal_symbols: pl_predicates.append("terminal(%s)" % terminal.lower()) return pl_predicates
def getStolj(): # Gramatika za stoljeća # TODO pojednostiviti the of na poč i kraju kval STOLJG = '' for l in open('ld_grammar.txt'): if l[:1] != '#': STOLJG += l for i in range(1, 50): format_str = (i - 1, int_to_roman(i), int_to_roman(i), int_to_roman(i) + num2words(i, to='ordinal', lang='en')[-2:], i, i, num2words(i, to='ordinal_num', lang='en'), num2words(i, to='cardinal', lang='en'), num2words(i, to='ordinal', lang='en')) p = "STOLJ_S[NORM='{:02d}xx'] -> '{}.'|'{}'|'{}'|'{}.'|'{}'|'{}'|'{}'|'{}'".format( *format_str) STOLJG += p + '\n' #print(p) for i in range(-50, 0): format_str = (-i - 1, int_to_roman(-i), int_to_roman(-i), int_to_roman(-i) + num2words(-i, to='ordinal', lang='en')[-2:], -i, -i, num2words(-i, to='ordinal_num', lang='en'), num2words(-i, to='cardinal', lang='en'), num2words(-i, to='ordinal', lang='en')) p = "STOLJ_PNE[NORM='-{:02d}xx'] -> '{}.'|'{}'|'{}'|'{}.'|'{}'|'{}'|'{}'|'{}'".format( *format_str) STOLJG += p + '\n' #print(p) # generiranje grammar = FeatureGrammar.fromstring(STOLJG) return grammar, STOLJG
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)
ugrammar = FeatureGrammar.fromstring("""\ # ################### # Grammar Productions # ################### # Sentence expansion productions S -> NP[NUM=?n] VP[FORM=?f, NUM=?n, SUBCAT=nil] | AdvC S | WH Aux[NUM=?n] NP[NUM=?n] VP[FORM=?f, NUM=pl, SUBCAT=nil] # Noun Phrase expansion productions NP[NUM=?n] -> ProperNoun[NUM=?n] | ProNoun[NUM=?n] | Nominal | DET[NUM=?n] Nominal NP[NUM=pl] -> NP[NUM=?n] CP # Verb Phrase expansion productions VP[FORM=?f, NUM=?n, SUBCAT=?rest] -> VP[FORM=?f, NUM=?n, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[FORM=?f, NUM=?n, SUBCAT=?args] -> V[FORM=?f, NUM=?n, SUBCAT=?args] | ADV V[FORM=?f, NUM=?n, SUBCAT=?args] # Argument expansion productions ARG[CAT=np] -> NP[NUM=?n] ARG[CAT=pp] -> PP ARG[CAT=subc] -> S # Conjunction Phrase expansion productions CP -> CONJ NP[NUM=?n] # Prepositional Phrase expansion productions PP -> Prep NP[NUM=?n] AdvC -> CONJ S # Nominal expansion productions Nominal -> NOUN[NUM=?n] | Nominal PP | ADJ Nominal | Nominal NOUN[NUM=?n] # ################### # Lexical Productions # ################### # Determiners and Nouns DET[NUM=sg] -> 'a' DET[NUM=?n] -> 'the' ProperNoun[NUM=sg] -> 'Bart' | 'Homer' | 'Lisa' ProNoun[NUM=sg] -> 'he' NOUN[NUM=sg] -> 'milk' | 'salad' | 'kitchen' | 'midnight' | 'table' | 'bread' NOUN[NUM=pl] -> 'shoes' # Conjunctions and Adverbs CONJ -> 'and' | 'when' ADV -> 'always' | 'never' # Auxiliaries Aux[NUM=pl] -> 'do' Aux[NUM=sg] -> 'does' # ################### # Verbs # VI V[FORM=base, NUM=pl, SUBCAT=nil] -> 'laugh' V[FORM=vbz, NUM=sg, SUBCAT=nil] -> 'laughs' V[FORM=pret, NUM=?n, SUBCAT=nil] -> 'laughed' # VT + NP V[FORM=base, NUM=pl, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drink' | 'wear' V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=nil]] -> 'wears' | 'serves' | 'drinks' V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=subc, TAIL=nil]] -> 'thinks' # VT + NP + PP V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=pp, TAIL=nil]]] -> 'serves' | 'drinks' | 'puts' # VT + NP + NP V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=np, TAIL=nil]]] -> 'serves' # Adjectives, Prepositions and WHs ADJ -> 'blue' | 'healthy' | 'green' Prep -> 'in' | 'before' | 'on' WH -> 'when' """)
def load_parser (fn): return FeatureChartParser(FeatureGrammar.fromstring(open(fn).read()))
def from_string(cls, str_grammar: str, **kwargs) -> "FeatureGrammarNode": feature_grammar = FeatureGrammar.fromstring(str_grammar) return cls(feature_grammar.start(), feature_grammar, **kwargs)
ugrammar = FeatureGrammar.fromstring("""\ S -> NP[NUM=?n] VP[SUBCAT=nil,Form=?n] | NP[NUM=?n] V_specialP[SUBCAT=nil,Form=?n] | Multi_ProperNoun[NUM=plural] VP[SUBCAT=nil,Form=plural] | Wh_conj S S | Wh_adv AUX[NUM=?n] QS[NUM=?n] QS[NUM=?n] -> NP[NUM=?n] VP[SUBCAT=nil,Form=base] | Multi_ProperNoun[NUM=plural] VP[SUBCAT=nil,Form=base] AUX[NUM=third_singular_present]-> 'does' AUX[NUM=plural]-> 'do' NP[NUM=?n] -> DET Nominal[NUM=?n] | Nominal[NUM=?n] Nominal[NUM=?n] -> ProperNoun[NUM=?n] | Noun[NUM=?n] | Nominal[NUM=?n] PP | ADJ Nominal[NUM=?n] Multi_ProperNoun[NUM=plural] -> Nominal[NUM=?n] CONJ Nominal[NUM=?n] ProperNoun[NUM=third_singular_present] -> 'Homer' | 'Bart' | 'Lisa' ProperNoun[NUM=third_singular_past] -> 'Homer' | 'Bart' | 'Lisa' Noun[NUM=singular] -> 'milk' | 'salad' | 'midnight' | 'table' | 'kitchen' | 'bread' Noun[NUM=plural] -> 'shoes' ADJ -> 'blue' | 'healthy' | 'green' CONJ -> 'and' Wh_conj -> 'when' Wh_adv -> 'when' | 'what' | 'whom' DET -> 'a' | 'the' VP[SUBCAT=?rest,Form=?n] -> VP[SUBCAT=[HEAD=?arg, TAIL=?rest],Form=?n] ARG[CAT=?arg] VP[SUBCAT=?rest,Form=?n] -> ADV VP[SUBCAT=[HEAD=?arg, TAIL=?rest], Form=?n] ARG[CAT=?arg] VP[SUBCAT=?args,Form=?n] -> V[SUBCAT=?args,Form=?n] V_specialP[SUBCAT=?rest,Form=?n] -> V_specialP[SUBCAT=[HEAD=?arg, TAIL=?rest],Form=?n] ARG[CAT=?arg] V_specialP[SUBCAT=?args,Form=?n] -> V_special[SUBCAT=?args,Form=?n] PP -> PREP NP PREP -> 'on' | 'in' | 'before' ARG[CAT=NP] -> NP ARG[CAT=PP] -> PP ARG[CAT=S] -> S ADV -> 'always' | 'when' | 'never' V[SUBCAT=nil, Form=third_singular_present] -> 'laughs' V[SUBCAT=[HEAD=NP, TAIL=nil], Form=third_singular_present] -> 'drinks' | 'serves' | 'wears' V[SUBCAT=[HEAD=S, TAIL=nil],Form=third_singular_present] -> 'thinks' V[SUBCAT=[HEAD=NP, TAIL=[HEAD=NP, TAIL=nil]],Form=third_singular_present] -> 'serves' V_special[SUBCAT=[HEAD=V_ingP, TAIL=[HEAD=NP, TAIL=nil]],Form=third_singular_present] -> 'likes' V_ing -> 'drinking' V[SUBCAT=nil, Form=third_singular_past] -> 'laughed' V[SUBCAT=nil, Form=plural ] -> 'laugh'|'laughed'|'drink' | 'serve' | 'wear' V[SUBCAT=[HEAD=NP, TAIL=nil], Form=plural] -> 'drink' | 'serve' | 'wear' V[SUBCAT=[HEAD=S, TAIL=nil],Form=plural] -> 'think'|'laughed'|'drink' | 'serve' | 'wear' V[SUBCAT=[HEAD=NP, TAIL=[HEAD=NP, TAIL=nil]],Form=plural] -> 'serve' V[SUBCAT=nil, Form=base ] -> 'laugh'|'drink' | 'serve' | 'wear' V[SUBCAT=[HEAD=NP, TAIL=nil], Form=base] -> 'drink' | 'serve' | 'wear' V[SUBCAT=[HEAD=S, TAIL=nil],Form=base] -> 'think' V[SUBCAT=[HEAD=NP, TAIL=[HEAD=NP, TAIL=nil]],Form=base] -> 'serve' """)
know_gael_bas_justine = create_fact(know, Gael, [Bas, Justine]) # know(Gael, [Bas, Justine]) know_gael_bas = create_fact(know, Gael, Bas) # know(Gael, Bas) know_gael_justine = create_fact(know, Gael, Justine) # know(Gael, Justine) know_justine_bas = create_fact(know, Justine, Bas) # know(Justine, Bas) member_bas_club = create_fact(member, Bas, club) # member(Bas, club) member_gael_bas_club = create_fact(member, [Gael, Bas], club) # member([Gael, Bas], club) # LIST OF FACT THAT WILL BE INPUT TO THE GRAMMAR : list_of_facts = format_list_of_facts([know_gael_justine, member_gael_bas_club]) root = FeatStructNonterminal("Root") start_production = Production(lhs=root, rhs=[list_of_facts]) # CREATE A NEW GRAMMAR THAT COMBINE THE STATIC + DYNAMIC RULES with open("data/fcfg/feature_grammar.fcfg", "r") as grammar_file: grammar_str = grammar_file.read() static_grammar = FeatureGrammar.fromstring(grammar_str) all_productions = static_grammar.productions() + [start_production] + dynamic_productions dynamic_grammar = FeatureGrammar(start=root, productions=all_productions) print(dynamic_grammar) # SAMPLE SOME LEAF FROM THE ROOT feature_root = FeatureGrammarNode(symbols=root, feature_grammar=dynamic_grammar) random.seed(2) # print(feature_root.find_random_valid_leaf_debug()) for i in range(10): random.seed(i) print(feature_root.find_random_valid_leaf())
ugrammar = FeatureGrammar.fromstring("""\ ############################ nomal sentense ####################################### S -> NP[NUM=?n] VP[FORM=?f,NUM=?n,SUBCAT=nil] S -> CLAUSE S S -> WhNP Aux[NUM=?n,FORM=?f] QS[NUM=?n] ############################ questions sentence ############################### QS[NUM=?n] -> NP[NUM=?n] VP[FORM=base] ############################ Wh subject questions ############################### WhNP -> Wh NP|Wh ############################ VP ####################################### VP[FORM=?f,NUM=?n,SUBCAT=?args] -> V[FORM=?f,NUM=?n,SUBCAT=?args] VP[FORM=?f,NUM=?n,SUBCAT=?rest] -> VP[FORM=?f,NUM=?n,SUBCAT=[HEAD=?arg,TAIL=?rest]] ARG[CAT=?arg] VP[FORM=?f,NUM=?n] -> VP[FORM=?f,NUM=?n] ADJC VP[FORM=?f,NUM=?n] -> Adv VP[FORM=?f,NUM=?n] VP[FORM=?f,NUM=?n] -> Mod VP[FORM=base] | Mod PASTP[FORM=?f] | Mod 'not' PASTP[FORM=?f] VP[FORM=?f,NUM=?n] -> PASTP[FORM=?f] VP[FORM=?f,NUM=?n] -> Aux[FORM=?f] VP[FORM=?f] ############################ PASTP ####################################### PASTP[FORM=?f] -> Aux[FORM=?f] VP[FORM=?f] ############################ ARG ####################################### ARG[CAT=np]-> NP[NUM=?n] ARG[CAT=pp]-> PP ARG[CAT=clause]-> S ARG[CAT=clause]-> CLAUSE ############################ ADJUNCT ####################################### ADJC[CAT=pp] ->PP ############################# CLAUSE ############################# CLAUSE -> Comp S ############################ NP ####################################### NP[NUM=?n] -> NOM[NUM=?n] NP[NUM=?n] -> Det[NUM=?n] NOM[NUM=?n] NP[NUM=plur] -> NP Conj NP NP[NUM=?n] -> VP[FORM =prespart] ############################ NOM ####################################### NOM[NUM=?n] -> Noun[NUM=?n]| Noun[NUM=?n] ADJC NOM[NUM=?n] -> ProperNoun[NUM =?n] |ProNoun[NUM=?n] NOM[NUM=?n] -> Adj NOM[NUM=?n] ############################ PP ####################################### PP -> Prep NP ############################ word ####################################### #-------------vi--------------------# V[FORM=vbz,NUM =sing,SUBCAT=nil] -> 'laughs' V[FORM=base,NUM =plur,SUBCAT=nil] -> 'laugh' #-------------------------- vt + np ---------------------------------# V[FORM=vbz,NUM =sing,SUBCAT=[HEAD=np,TAIL=nil]] -> 'wears' | 'drinks'|'serves'|'likes' V[FORM=base,NUM =plur,SUBCAT=[HEAD=np,TAIL=nil]] -> 'wear' | 'drink' |'serve' #--------------------------vt + np + np ---------------------------------# V[FORM=vbz,NUM =sing,SUBCAT=[HEAD=np,TAIL=[HEAD=np,TAIL=nil]]] -> 'serves' #--------------------------vt + np + pp ---------------------------------# V[FORM=vbz,NUM =sing,SUBCAT=[HEAD=np,TAIL=[HEAD=pp,TAIL=nil]]] -> 'puts' #-------------------------- vt + clause ---------------------------------# V[FORM=vbz,NUM =sing,SUBCAT=[HEAD=clause,TAIL=nil]] -> 'thinks' V[FORM=base,NUM =plur,SUBCAT=[HEAD=clause,TAIL=nil]] -> 'think' #-------------------------- v-past ---------------------------------# V[FORM=pret,SUBCAT=nil] -> 'laughed' #--------------------------v-past Participle-----------------------------# V[FORM=pastpart,SUBCAT=[HEAD=np,TAIL=nil]] -> 'drunk' V[FORM=pastpart,SUBCAT=[HEAD=np,TAIL=np]] -> 'seen' #-------------------------- v-GERUND ---------------------------------# V[FORM=prespart,SUBCAT=[HEAD=np,TAIL=nil]] -> 'drinking' #-------------------------- Noun ---------------------------------# Noun[NUM =sing] -> 'milk' |'salad' |'kitchen'|'midnight'|'table'|'bread' Noun[NUM =plur] -> 'shoes' ProperNoun[NUM =sing] -> 'Homer'| 'Bart'| 'Lisa' ProNoun[NUM=sing] -> 'he' #-------------------------- AUX -------------------------------# 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' #-------------------------- others -------------------------------# Conj ->'and' Wh -> 'when'| 'what' | 'where' | 'whom' Mod -> 'may' Det[NUM =sing] -> 'a'|'the' Det[NUM =plur] -> 'the' Prep -> 'in' |'before' |'on' Adj ->'blue' |'healthy' |'green' Adv -> 'always'|'never'|'not' Comp -> 'when' """)
ugrammar = FeatureGrammar.fromstring("""\ ###################################################################################################### ################################################# SENTENCE ########################################### ###################################################################################################### S[NUM=?n] -> NP[NUM=?n] VP[NUM=?n] | PP S[NUM=?n] S[NUM=?n] -> VP[NUM=?n] S[NUM=?n] -> WH_NP AUX[FORM=?f, NUM=?n] NP[NUM=?n] VP[NUM=?n] S[NUM=?n] -> WH_NP VP[NUM=?n] S[NUM=?n] -> AUX[FORM=?f, NUM=?n] NP[NUM=?n] VP[NUM=?n] ######## WH-Question ########### WH_NP -> WH | WH ARG[CAT=?arg] WH -> 'when' | 'what' | 'where' | 'whom' ###################################################################################################### ############################################## NOUN PHRASE ########################################### ###################################################################################################### NP[NUM=?n] -> PRE_DET NP[NUM=plur] | PROP_N[NUM=?n] | PROP_N[NUM=?n] GER_P | DET[NUM=?n] NOM[NUM=?n] | NOM[NUM=?n] NP[NUM=plur] -> NP CONJ NP NOM[NUM=?n] -> ADJ_P NOM[NUM=?n] | QUAN NOM[NUM=plur] | NOM[NUM=?n] REL_CL[NUM=?n] | N[NUM=?n] | GER_P | NOM[NUM=?n] N[NUM=?n] | NOM[NUM=?n] PP ########## Predeterminer ########## PRE_DET -> 'all' | 'most' ########## Quantifier ########## QUAN -> 'some' | 'many' ############# Pronoun ############# PROP_N[NUM=sing] -> 'Homer' | 'Bart' | 'Lisa' ######## Relative Clause ########## REL_CL[NUM=?n] -> S[NUM=?n] | COMP S[NUM=?n] | COMP VP[NUM=?n] COMP -> 'that' | 'who' ############# Gerund ############## GER_P -> GER NP | GER NOM GER -> V[FORM=prespart, SUBCAT=nil] ########### Noun Lexicon ########## N[NUM=sing] -> 'salad' | 'midnight' | 'kitchen' | 'table' | 'plane' | 'house' | 'milk' | 'morning' | 'midnight' | 'Edinburgh' | 'London' | '8' |'9' | '10' | 'breakfast' N[NUM=plur] -> 'shoes' | 'tables' | 'trains' | 'flights' | 'people' | 'airlines' ###################################################################################################### ############################################## VERB PHRASE ########################################### ###################################################################################################### VP[SUBCAT=?rest, NUM=?n] -> VP[TENSE=?t, NUM=?n, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] | V[NUM=?n, SUBCAT=?rest] VP[SUBCAT=?rest, NUM=?n] -> ADV_P V[NUM=?n, SUBCAT=?rest] | V[NUM=?n, SUBCAT=?rest] ADV_P VP[SUBCAT=?rest, NUM=?n] -> MODP VP[TENSE=?t, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=?rest, NUM=?n] -> VTB[NUM=?n] VP[SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=?rest, NUM=?n] -> VTB[NUM=?n] VP[SUBCAT=?rest, NUM=?n] VP[SUBCAT=?rest, NUM=?n] -> VP[FORM=pres, NUM=plur, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[SUBCAT=nil, NUM=?n] -> VTB[NUM=?n] ADJ_P MODP -> MOD AUX[FORM=?f, NUM=plur] | MOD 'not' AUX[FORM=?f, NUM=plur] PP -> P NP | P S | P NOM ARG[CAT=np] -> NP ARG[CAT=pp] -> PP ARG[CAT=nom] -> NOM ARG[CAT=cl] -> REL_CL ######################################################################### ############################## PRESENT ################################## ######################################################################### ############## Intransitive ############### V[FORM=pres, NUM=sing, SUBCAT=nil]-> 'laughs' | 'smiles' | 'walks' | 'serves' | 'drinks' | 'leaves' V[FORM=pres, NUM=plur, SUBCAT=nil] -> 'laugh' | 'smile' | 'walk' | 'serve' |'drink' | 'leave' V[FORM=pres, NUM=sing, SUBCAT=[HEAD=pp, TAIL=nil]] -> 'leaves' | 'lives' | 'flies' V[FORM=pres, NUM=plur, SUBCAT=[HEAD=pp, TAIL=nil]] -> 'leave' | 'live' | 'fly' ############## Transitive ################ V[FORM=pres, NUM=sing, SUBCAT=[HEAD=cl,TAIL=nil]] -> 'thinks' | 'believes' V[FORM=pres, NUM=plur, SUBCAT=[HEAD=cl,TAIL=nil]] -> 'think' | 'believe' V[FORM=pres, NUM=sing, SUBCAT=[HEAD=np,TAIL=nil]] ->'serves' | 'drinks' V[FORM=pres, NUM=plur, SUBCAT=[HEAD=np,TAIL=nil]] ->'serve' | 'drink' V[FORM=pres, NUM=sing, SUBCAT=[HEAD=pp,TAIL=nil]] ->'walks' V[FORM=pres, NUM=plur, SUBCAT=[HEAD=pp,TAIL=nil]] ->'walk' V[FORM=pres, NUM=plur, SUBCAT=[HEAD=nom,TAIL=nil]] ->'drink' | 'wear' | 'serve' | 'like' V[FORM=pres, NUM=sing, SUBCAT=[HEAD=nom,TAIL=nil]] ->'drinks' | 'wears' | 'serves' | 'likes' ######################################################################### ################################ Past ################################### ######################################################################### ############## Intransitive ############### V[FORM=past, SUBCAT=nil] -> 'laughed' | 'smiled' | 'walked' ############### Transitive ################ V[FORM=past, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drank' | 'wore' | 'served' V[FORM=past, SUBCAT=[HEAD=nom,TAIL=nil]] -> 'drank' | 'wore' | 'served' V[FORM=pastpart, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drunk' | 'worn' | 'served' | 'seen' V[FORM=pastpart, SUBCAT=[HEAD=nom,TAIL=nil]] -> 'drunk' | 'worn' | 'served' | 'seen' ######################################################################### ############################## PRESENT CONT ############################# ######################################################################### V[FORM=prespart, SUBCAT=[HEAD=nom, TAIL=nil]] -> 'drinking' | 'wearing' | 'using' | 'fighting' V[FORM=prespart, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drinking' | 'wearing' | 'using' | 'fighting' V[FORM=prespart, SUBCAT=[HEAD=pp, TAIL=nil]] -> 'drinking' | 'fighting' | 'walking' ######################################################################### ################################## Gerund ############################### ######################################################################### V[FORM=prespart, SUBCAT=nil] -> 'drinking' | 'smiling' | 'wearing' | 'crying' | 'flying' ######################################################################### ################################## Clause ############################### ######################################################################### V[FORM=base, SUBCAT=[HEAD=cl, TAIL=nil]] -> 'say' | 'claim' V[FORM=pres, SUBCAT=[HEAD=cl, TAIL=nil]] -> 'says' | 'claims' V[FORM=vbz, SUBCAT=[HEAD=cl, TAIL=nil]] -> 'said' | 'claimed' V[FORM=pastpart, SUBCAT=[HEAD=cl, TAIL=nil]] -> 'said' | 'claimed' V[FORM=prespart, SUBCAT=[HEAD=cl, TAIL=nil]] -> 'saying' | 'claiming' ################# Verb To Be ############### VTB[NUM=sing] -> 'is' VTB[NUM=plur] -> 'are' ################## Modal ################## MOD -> 'may' ################ Determiner ############### DET[NUM=sing] -> 'a' | 'the' | 'that' DET[NUM=plur] -> 'the' | 'these' | 'those' ################ Conjunction ############## CONJ -> 'and' ################# Adjective ############### ADJ_P -> ADJ | ADJ ADJ_P ADJ -> 'blue' | 'healthy' | 'green' | 'same' | 'friendly' ################# Adverb ################## ADV_P -> ADV | ADV ADV_P 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' """)
""" fcfg_string_tv = """ ############################# # Grammar of transitive verbs and their lexical rules ############################# VP[NUM=?n,SEM=<?v(?obj)>] -> TV[NUM=?n,SEM=?v] NP[SEM=?obj] VP[NUM=?n,SEM=<?v(?obj,?pp)>] -> DTV[NUM=?n,SEM=?v] NP[SEM=?obj] PP[+TO,SEM=?pp] TV[NUM=sg,SEM=<\X x.X(\y.bite(x,y))>,TNS=pres] -> 'bites' TV[NUM=pl,SEM=<\X x.X(\y.bite(x,y))>,TNS=pres] -> 'bite' DTV[NUM=sg,SEM=<\Y X x.X(\z.Y(\y.give(x,y,z)))>,TNS=pres] -> 'gives' DTV[NUM=pl,SEM=<\Y X x.X(\z.Y(\y.give(x,y,z)))>,TNS=pres] -> 'give' """ syntax_notv = FeatureGrammar.fromstring(fcfg_string_notv) syntax = FeatureGrammar.fromstring(fcfg_string_notv + fcfg_string_tv) # don't change these functions def sem_parser(sents, syntax, verbose=False, is_cs=False): """ It parses sentences with an FDFG grammar and returns a dictionary of sentences to their semantic representations. Parameters: sents: a list of sentences to be parsed. fcfg_string: a string listing all fcfg rules with SEM for the FeatureGrammar. verbose: boolean value. default value is `False`. if verbose is True it prints results.
ugrammar = FeatureGrammar.fromstring("""\ # ################### # Grammar Productions # ################### # Sentence S -> NP[NUM=?n] VP[FORM=?f, NUM=?n, SUBCAT=nil] | AdvC S S -> NP[NUM=?n] MVP[FORM=?f, NUM=?n, SUBCAT=nil] S -> WhNP InvS S -> WH InvS # Inversed Sentence InvS -> Aux[NUM=?n] IS[NUM=pl] IS[NUM=pl] -> NP[NUM=?n] VP[FORM=?f, NUM=pl, SUBCAT=nil] # Noun Phrase NP[NUM=?n] -> ProperNoun[NUM=?n] | ProNoun[NUM=?n] NP[NUM=?n] -> Nominal[NUM=?n] | DET[NUM=?n] Nominal[NUM=?n] | NP[NUM=?n] GerundNP NP[NUM=pl] -> NP[NUM=?n] CP # WH WhNP -> WH NP[NUM=?n] # Verb Phrase VP[FORM=?f, NUM=?n, SUBCAT=?rest] -> VP[FORM=?f, NUM=?n, SUBCAT=[HEAD=?arg, TAIL=?rest]] ARG[CAT=?arg] VP[FORM=?f, NUM=?n, SUBCAT=?args] -> V[FORM=?f, NUM=?n, SUBCAT=?args] | ADV V[FORM=?f, NUM=?n, SUBCAT=?args] VP[FORM=?f, NUM=?n, SUBCAT=?args] -> Aux V[FORM=?f, NUM=?n, SUBCAT=?args] # Modal Phrase MVP[FORM=?f, NUM=?n, SUBCAT=nil] -> MP VP[FORM=?f, NUM=?n, SUBCAT=nil] # Modal Verb MP -> MV | MV ADV # Arguments ARG[CAT=np] -> NP[NUM=?n] ARG[CAT=pp] -> PP ARG[CAT=subc] -> S # Conjunction Phrase CP -> CONJ NP[NUM=?n] # Prepositional Phrase PP -> Prep NP[NUM=?n] # Adverbial Clause AdvC -> CONJ S # Nominal Nominal[NUM=?n] -> NOUN[NUM=?n] | Nominal[NUM=?n] PP | ADJ Nominal[NUM=?n] Nominal[NUM=?n] -> Nominal[NUM=?n] NOUN[NUM=?n] | GerundNP # Gerund expansion productions GerundNP -> GerundV NP GerundV -> V[FORM=presP] # ################### # Lexical Productions # ################### # Determiners and Nouns DET[NUM=sg] -> 'a' DET[NUM=?n] -> 'the' ProperNoun[NUM=sg] -> 'Bart' | 'Homer' | 'Lisa' ProNoun[NUM=sg] -> 'he' NOUN[NUM=sg] -> 'milk' | 'salad' | 'kitchen' | 'midnight' | 'table' | 'bread' NOUN[NUM=pl] -> 'shoes' # Conjunctions and Adverbs CONJ -> 'and' | 'when' ADV -> 'always' | 'never' | 'not' # Auxiliaries Aux[NUM=pl] -> 'do' | 'have' Aux[NUM=sg] -> 'does' # ################### # Verbs # VI V[FORM=base, NUM=pl, SUBCAT=nil] -> 'laugh' | 'think' | 'drink' | 'serve' V[FORM=vbz, NUM=sg, SUBCAT=nil] -> 'laughs' V[FORM=pret, NUM=?n, SUBCAT=nil] -> 'laughed' # VT + NP V[FORM=base, NUM=pl, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drink' | 'wear' | 'serve' V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=nil]] -> 'wears' | 'serves' | 'drinks' | 'thinks' | 'likes' V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=subc, TAIL=nil]] -> 'thinks' V[FORM=base, NUM=pl, SUBCAT=[HEAD=subc, TAIL=nil]] -> 'think' V[FORM=past, NUM=?n, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drunk' | 'seen' V[FORM=presP, NUM=?n, SUBCAT=[HEAD=np, TAIL=nil]] -> 'drinking' # VT + NP + PP V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=pp, TAIL=nil]]] -> 'serves' | 'drinks' | 'puts' # VT + NP + NP V[FORM=vbz, NUM=sg, SUBCAT=[HEAD=np, TAIL=[HEAD=np, TAIL=nil]]] -> 'serves' # Adjectives, Prepositions and WHs ADJ -> 'blue' | 'healthy' | 'green' Prep -> 'in' | 'before' | 'on' WH -> 'when' | 'what' | 'whom' MV -> 'may' """)