def grammar(**kwargs): return recursive.grammar(**kwargs) | { Rule('SPECIFIC_CLAIM', [RuleRef('BLOB')], blob_specific_claim), Rule('CONDITIONAL_CLAIM', [RuleRef('BLOB')], blob_conditional_claim), Rule('BLOB_WORD', [Expression(r'^(?!because|but|except)$')], lambda state, data: data[0].local), Rule('BLOB', [RuleRef('BLOB_WORD')], lambda state, data: [data[0]]), Rule('BLOB', [RuleRef('BLOB'), RuleRef('BLOB_WORD')], lambda state, data: data[0] + [data[1]]), }
def grammar(**kwargs): return category.grammar(**kwargs) | prototype.grammar(**kwargs) | verb.grammar(**kwargs) | { Rule('CATEGORY', [Expression(r'^not?$'), RuleRef('CATEGORY')], Negation.from_rule), Rule('PROTOTYPE', [Expression(r'^not$'), RuleRef('PROTOTYPE')], Negation.from_rule), Rule('PROTOTYPES', [Expression(r'^not$'), RuleRef('PROTOTYPES')], Negation.from_rule), Rule('VERB_INF', [Expression(r'^not$'), RuleRef('VERB_INF')], Negation.from_rule) }
def grammar(**kwargs): return noun.grammar(**kwargs) | { Rule( "PROTOTYPE", [Expression(r'^every|an?$'), RuleRef("NOUN")], lambda state, data: data[1] + Interpretation(local=Prototype( data[1].local, article=data[0].local))), Rule( "PROTOTYPES", [RuleRef("NOUNS")], lambda state, data: data[0] + Interpretation(local=Prototype(data[0].local))), Rule( "PROTOTYPES", [Expression(r'^all|most|many|some$'), RuleRef("NOUNS")], lambda state, data: data[0] + Interpretation( local=Prototype(data[1].local, article=data[0].local))), Rule("PROTOTYPE*", [RuleRef("PROTOTYPE")], passthru), Rule("PROTOTYPE*", [RuleRef("PROTOTYPES")], passthru), }
def test_claims(): grammar = general.grammar | specific.grammar | negation.grammar | { Rule('ARGUMENT', [RuleRef('GENERAL_CLAIM')], passthru), Rule('ARGUMENT', [RuleRef('SPECIFIC_CLAIM')], passthru), } sentences = map(tokenize, [ 'Tweety is a bird', 'birds can fly', 'the birds can fly', 'Tweety can fly', 'Tweety can not fly', 'Tweety and Birdy are pretty', ]) # Uncomment this call to see the parse trees. # override_callbacks(grammar) parser = Parser(grammar, 'ARGUMENT') test(parser, sentences)
def grammar(anaphora=True, **kwargs): if anaphora: singular = Instance plural = GroupInstance else: singular = DumbInstance plural = DumbGroupInstance return name.grammar(**kwargs) | noun.grammar(**kwargs) | pronoun.grammar(**kwargs) | { # Singular Rule("INSTANCE", [RuleRef("PRONOUN")], singular.from_pronoun_rule), Rule("INSTANCE", [RuleRef("NAME")], singular.from_name_rule), Rule("INSTANCE", [Expression(r"^[Tt]he$"), RuleRef("NOUN")], singular.from_noun_rule), Rule("INSTANCE", [Expression(r"^[Hh]is|[Hh]er|[Tt]heir$"), RuleRef("NOUN")], singular.from_noun_rule), # Plural Rule("INSTANCES", [RuleRef("PRONOUNS")], plural.from_pronoun_rule), Rule("INSTANCES", [RuleRef("NAMES")], plural.from_names_rule), Rule("INSTANCES", [Expression(r"^[Tt]he$"), RuleRef("NOUNS")], plural.from_noun_rule), Rule("INSTANCES", [Expression(r"^[Hh]is|[Hh]er|[Tt]heir$"), RuleRef("NOUNS")], plural.from_noun_rule), # Shortcuts Rule("INSTANCE*", [RuleRef("INSTANCE")], passthru), Rule("INSTANCE*", [RuleRef("INSTANCES")], passthru), }
def grammar(**kwargs): return { Rule('PREPOSITION', [PrepositionSymbol()], passthru), Rule( 'PP', [RuleRef('PREPOSITION'), RuleRef('INSTANCE*')], lambda state, data: data[1] + Interpretation( local=PrepositionPhrase(data[0].local, data[1].local))), Rule( 'PP', [RuleRef('PREPOSITION'), RuleRef('PROTOTYPE*')], lambda state, data: data[1] + Interpretation( local=PrepositionPhrase(data[0].local, data[1].local))), Rule( 'PP', [RuleRef('PREPOSITION'), RuleRef('CATEGORY')], lambda state, data: data[1] + Interpretation( local=PrepositionPhrase(data[0].local, data[1].local))), }
def and_rules(name: str, singleton: str, accept_singular: bool = False, first_singleton: Optional[str] = None, last_singleton: Optional[str] = None) -> Set[Rule]: """ Creates a mini-grammar of rules that are needed to parse 'A and B', 'A, B and C', 'A, B, C and D', etc. where A, B, C and D all are parseable using the rule name passed using the singleton argument. Grammar: <As> ::= A_helper `and' A_last <A_helper> ::= A_helper `,' A <A_helper> ::= A_first <As> ::= A """ if last_singleton is None: last_singleton = singleton if first_singleton is None: first_singleton = singleton helper = name + "_" rules = { # _ and C Rule(name, [RuleRef(helper), Literal('and'), RuleRef(last_singleton)], lambda state, data: data[0] + data[2] + Interpretation(local=data[0].local | OrderedSet([data[2].local]))), # A, B # (allows for 'A, B and C') Rule(helper, [RuleRef(helper), Literal(','), RuleRef(singleton)], lambda state, data: data[0] + data[2] + Interpretation(local=data[0].local | OrderedSet([data[2].local]))), # A (allows for 'A and B') Rule(helper, [RuleRef(first_singleton)], lambda state, data: data[0] + Interpretation(local=OrderedSet([data[0].local]))) } if accept_singular: rules |= { Rule(name, [RuleRef(singleton)], lambda state, data: data[0] + Interpretation(local=OrderedSet([data[0].local]))) } return rules
def grammar(**kwargs): return adjective.grammar(**kwargs) | preposition.grammar(**kwargs) | { # Raw nouns Rule("NOUN^", [NounParser(is_plural=False)], passthru), Rule("NOUNS^", [NounParser(is_plural=True)], passthru), # Nouns without prepositions Rule("NOUN", [RuleRef('NOUN^')], passthru), Rule("NOUNS", [RuleRef('NOUNS^')], passthru), # Nouns with prepositions (the car of (the owner of the building)) Rule("NOUN", [RuleRef('NOUN^'), RuleRef('PP')], lambda state, data: data[1] + Interpretation(local=data[0].local.with_preposition_phrase(data[1].local))), Rule("NOUNS", [RuleRef('NOUNS^'), RuleRef('PP')], lambda state, data: data[1] + Interpretation(local=data[0].local.with_preposition_phrase(data[1].local))), Rule("NOUN", [RuleRef('ADJECTIVE'), RuleRef('NOUN')], lambda state, data: data[1] + Interpretation(local=data[1].local.with_adjective(data[0].local))), Rule("NOUNS", [RuleRef('ADJECTIVE'), RuleRef('NOUNS')], lambda state, data: data[1] + Interpretation(local=data[1].local.with_adjective(data[0].local))), Rule("NOUN*", [RuleRef('NOUN')], passthru), Rule("NOUN*", [RuleRef('NOUNS')], passthru), }
def grammar(**kwargs): return pronoun.grammar(**kwargs) \ | category.grammar(**kwargs) \ | prototype.grammar(**kwargs) \ | verb.grammar(**kwargs) \ | specific.grammar(**kwargs) \ | and_rules('SPECIFIC_CLAIMS', 'SPECIFIC_CLAIM', accept_singular=True) \ | { # x is an A when x is a B Rule('CONDITIONAL_CLAIM', [RuleRef('SPECIFIC_CLAIM'), Expression(r'^if|when$'), RuleRef('SPECIFIC_CLAIMS')], undetermined_claim), Rule('CONDITIONAL_CLAIM', [RuleRef('GENERAL_CLAIM'), Expression(r'^if|when$'), RuleRef('SPECIFIC_CLAIMS')], expanded_general_claim), Rule('CONDITIONAL_CLAIM', [RuleRef('GENERAL_CLAIM')], passthru), # an A is a B Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPE'), VerbParser(r'^is|has|was$'), RuleRef('CATEGORY')], general_claim_singular), Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPE'), VerbParser(r'^is|has|was$'), RuleRef('PROTOTYPE')], general_claim_singular), Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPE'), VerbParser(r'^can|may|should$'), RuleRef('VERB_INF')], general_claim_singular), # A's are B's Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPES'), VerbParser(r'^are|have$'), RuleRef('CATEGORY')], general_claim_plural), Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPES'), VerbParser(r'^are|have$'), RuleRef('PROTOTYPES')], general_claim_plural), Rule('GENERAL_CLAIM', [RuleRef('PROTOTYPES'), VerbParser(r'^can|may|should$'), RuleRef('VERB_INF')], general_claim_plural), }
def grammar(**kwargs): return instance.grammar(**kwargs) | category.grammar(**kwargs) | prototype.grammar(**kwargs) | action.grammar(**kwargs) | verb.grammar(**kwargs) | { Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCE'), VerbParser(r'is|has|was'), RuleRef('CATEGORY')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCE'), VerbParser(r'is|has|was'), RuleRef('PROTOTYPE')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCE'), VerbParser(r'is|has|was'), RuleRef('INSTANCE*')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCE'), VerbParser(r'has|was'), RuleRef('ACTION_PP')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCES'), VerbParser('are|have'), RuleRef('CATEGORY')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCES'), VerbParser('are|have'), RuleRef('PROTOTYPE')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCE'), VerbParser('can|may|must|should'), RuleRef('ACTION_INF')], SpecificClaim.from_rule), Rule('SPECIFIC_CLAIM', [RuleRef('INSTANCES'), VerbParser('can|may|must|should'), RuleRef('ACTION_INF')], SpecificClaim.from_rule), }
def grammar(**kwargs): return { # Rule("CATEGORY", [Expression(r'.+')], # lambda state, data: data[0] + Interpretation(local=Category(data[0].local))) Rule('CATEGORY', [RuleRef('NOUN*')], passthru), }
previous_frame = inspect.currentframe().f_back (filename, line_number, function_name, lines, index) = inspect.getframeinfo(previous_frame) rule = Rule(name, symbols, file=filename, line=line_number) self.rules.append(rule) def wrapper(callback): rule.callback = lambda state, data: callback(*data) return callback return wrapper en_grammar = Grammar([ Rule('name', [Tag('NNP')], merge), Rule('name', [RuleRef('name'), Tag('NNP')], merge), # longer names Rule('name', [RuleRef('name'), Tag('NNPS')], merge), # Catholic Swedes Rule('name', [Literal('Tweety')], merge), Rule('instance', [RuleRef('name')], lambda state, data: Entity(name=data[0])), Rule( 'instance', [Tag('PRP')], # "I" lambda state, data: Entity(pronoun=data[0])), Rule('instance', [RuleRef('def-dt'), RuleRef('adjectives?'), RuleRef('noun')], lambda state, data: Entity(noun=data[0] + data[1] + data[2])), Rule( 'instance', [
def grammar(**kwargs): return and_rules('SPECIFIC_CLAIMS', 'SPECIFIC_CLAIM', accept_singular=True) \ | and_rules('SPECIFIC_CLAIMS_CONDITIONAL_FIRST', 'SPECIFIC_CLAIM', first_singleton='CONDITIONAL_CLAIM') \ | and_rules('SPECIFIC_CLAIMS_CONDITIONAL_LAST', 'SPECIFIC_CLAIM', last_singleton='CONDITIONAL_CLAIM') \ | { Rule('ARGUMENT', [RuleRef('SENTENCE')], lambda state, data: data[0]), Rule('ARGUMENT', [RuleRef('ARGUMENT'), RuleRef('SENTENCE')], lambda state, data: data[0] + data[1]), Rule('SENTENCE', [RuleRef('SUPPORTED_CLAIM'), Literal('.')], lambda state, data: data[0]), Rule('SENTENCE', [RuleRef('ATTACKED_CLAIM'), Literal('.')], lambda state, data: data[0]), Rule('SUPPORTED_CLAIM', [RuleRef('SPECIFIC_CLAIM'), Literal('because'), RuleRef('SPECIFIC_CLAIMS')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.SUPPORT, data[0].local, specifics=data[2].local), local=data[0].local)), Rule('SUPPORTED_CLAIM', [RuleRef('SUPPORTED_CLAIM_WITH_WARRANT')], lambda state, data: data[0]), Rule('SUPPORTED_CLAIM_WITH_WARRANT', [RuleRef('SPECIFIC_CLAIM'), Literal('because'), RuleRef('SPECIFIC_CLAIMS_CONDITIONAL_FIRST')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.SUPPORT, data[0].local, general=data[2].local[0], specifics=data[2].local[1:]), local=data[0].local)), Rule('SUPPORTED_CLAIM_WITH_WARRANT', [RuleRef('SPECIFIC_CLAIM'), Literal('because'), RuleRef('SPECIFIC_CLAIMS_CONDITIONAL_LAST')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.SUPPORT, data[0].local, general=data[2].local[-1], specifics=data[2].local[:-1]), local=data[0].local)), Rule('ATTACKED_CLAIM', [RuleRef('SPECIFIC_CLAIM'), Expression(r'^but|except$'), RuleRef('SPECIFIC_CLAIMS')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.ATTACK, data[0].local, specifics=data[2].local), local=data[0].local)), Rule('ATTACKED_CLAIM', [RuleRef('ATTACKED_CLAIM_WITH_WARRANT')], lambda state, data: data[0]), Rule('ATTACKED_CLAIM_WITH_WARRANT', [RuleRef('SPECIFIC_CLAIM'), Expression(r'^but|except$'), RuleRef('SPECIFIC_CLAIMS_CONDITIONAL_FIRST')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.ATTACK, data[0].local, general=data[2].local[0], specifics=data[2].local[1:]), local=data[0].local)), Rule('ATTACKED_CLAIM_WITH_WARRANT', [RuleRef('SPECIFIC_CLAIM'), Expression(r'^but|except$'), RuleRef('SPECIFIC_CLAIMS_CONDITIONAL_LAST')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.ATTACK, data[0].local, general=data[2].local[-1], specifics=data[2].local[:-1]), local=data[0].local)), # Experimental, don't know if I want this # Rule('SUPPORTED_CLAIM', [RuleRef('SPECIFIC_CLAIM'), Literal('because'), RuleRef('CONDITIONAL_CLAIM')], # lambda state, data: data[0] + data[2] + assume(data[0].local, data[2].local)), # Attacking a warrant? Rule('SUPPORTED_CLAIM', [RuleRef('SUPPORTED_CLAIM'), Expression(r'^but|except$'), RuleRef('SPECIFIC_CLAIMS')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.ATTACK, data[0].local, specifics=data[2].local))), Rule('SUPPORTED_CLAIM', [RuleRef('SUPPORTED_CLAIM_WITH_WARRANT'), Expression(r'^but|except$'), RuleRef('SPECIFIC_CLAIMS')], lambda state, data: data[0] + data[2] + Interpretation(argument=relation(Relation.ATTACK, warrant_relation(data[0]), specifics=data[2].local))) }
def grammar(assumptions=True, **kwargs): return and_rules('EXPANDED_SPECIFIC_CLAIMS', 'EXPANDED_SPECIFIC_CLAIM', accept_singular=True) \ | and_rules('EXPANDED_SPECIFIC_CLAIMS_GENERAL_FIRST', 'EXPANDED_SPECIFIC_CLAIM', first_singleton='EXPANDED_GENERAL_CLAIM') \ | and_rules('EXPANDED_SPECIFIC_CLAIMS_GENERAL_LAST', 'EXPANDED_SPECIFIC_CLAIM', last_singleton='EXPANDED_GENERAL_CLAIM') \ | and_rules('SUPPORTS', 'SUPPORT', accept_singular=True) \ | and_rules('ATTACKS', 'ATTACK', accept_singular=True) \ | { Rule('ARGUMENT', [RuleRef('SENTENCE')], lambda state, data: data[0]), Rule('ARGUMENT', [RuleRef('ARGUMENT'), RuleRef('SENTENCE')], lambda state, data: data[0] + data[1]), Rule('SENTENCE', [RuleRef('EXPANDED_SPECIFIC_CLAIM'), Literal('.')], lambda state, data: data[0]), Rule('EXPANDED_SPECIFIC_CLAIM', [RuleRef('SPECIFIC_CLAIM'), RuleRef('SUPPORTS'), RuleRef('ATTACKS')], expanded_claim), # We use CONDITIONAL_CLAIM instead of GENERAL_CLAIM here because a conditional claim is a more specific # structure that can contain a general claim, and that is why it has to be above general claim in the # hierarchy. And hey, in a sense a general claim is also a conditional claim because for a general claim # to be applicable the subject has to match the group of the subject of the general claim, making it # more or less a conditional claim, right? Rule('EXPANDED_GENERAL_CLAIM', [RuleRef('CONDITIONAL_CLAIM'), RuleRef('SUPPORTS'), RuleRef('ATTACKS')], expanded_claim), Rule('SUPPORT', [Literal('because'), RuleRef('EXPANDED_SPECIFIC_CLAIMS')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.SUPPORT, specifics=data[1].local, make_assumptions=assumptions))), Rule('SUPPORT', [Literal('because'), RuleRef('EXPANDED_SPECIFIC_CLAIMS_GENERAL_FIRST')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.SUPPORT, conditional=data[1].local[0], specifics=data[1].local[1:], make_assumptions=assumptions))), Rule('SUPPORT', [Literal('because'), RuleRef('EXPANDED_SPECIFIC_CLAIMS_GENERAL_LAST')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.SUPPORT, conditional=data[1].local[-1], specifics=data[1].local[0:-1], make_assumptions=assumptions))), Rule('SUPPORT', [Literal('because'), RuleRef('EXPANDED_GENERAL_CLAIM')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.SUPPORT, conditional=data[1].local, make_assumptions=assumptions))), Rule('SUPPORTS', [], lambda state, data: Interpretation()), Rule('ATTACK', [Expression(r'^except|but$'), RuleRef('EXPANDED_SPECIFIC_CLAIMS')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.ATTACK, specifics=data[1].local, make_assumptions=assumptions))), Rule('ATTACK', [Expression(r'^except|but$'), RuleRef('EXPANDED_SPECIFIC_CLAIMS_GENERAL_FIRST')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.ATTACK, conditional=data[1].local[0], specifics=data[1].local[1:], make_assumptions=assumptions))), Rule('ATTACK', [Expression(r'^except|but$'), RuleRef('EXPANDED_SPECIFIC_CLAIMS_GENERAL_LAST')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.ATTACK, conditional=data[1].local[-1], specifics=data[1].local[0:-1], make_assumptions=assumptions))), Rule('ATTACK', [Expression(r'^except|but$'), RuleRef('EXPANDED_GENERAL_CLAIM')], lambda state, data: data[1] + Interpretation(local=PartialRelation(Relation.ATTACK, conditional=data[1].local, make_assumptions=assumptions))), Rule('ATTACKS', [], lambda state, data: Interpretation()), }