Source code for decomp.semantics.predpatt.rules.base
"""Base rule classes for PredPatt extraction system.This module defines the abstract base classes for all rules used in PredPatt.Rules track the logic behind extraction decisions and provide explanations."""from__future__importannotationsfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:from..core.tokenimportToken
[docs]classRule:"""Abstract base class for all PredPatt rules. Rules are used to track extraction logic and provide explanations for why certain tokens were identified as predicates or arguments. """
[docs]def__repr__(self)->str:"""Return string representation of the rule. Returns ------- str The rule's name by default. """returnself.name()
[docs]@classmethoddefname(cls)->str:"""Get the rule's name. Returns ------- str The class name without module prefix, converted to lowercase for backward compatibility with expected outputs. """# convert PascalCase to lowercase/snake_case for output compatibilityname=cls.__name__.split('.')[-1]# base classes keep their PascalCase namesbase_classes={'Rule','PredicateRootRule','ArgumentRootRule','PredConjRule','ArgumentResolution','ConjunctionResolution','SimplifyRule','PredPhraseRule','ArgPhraseRule','LanguageSpecific','EnglishSpecific'}ifnameinbase_classes:returnname# handle RuleI -> i special caseifname=='RuleI':return'i'# handle single letter rules (A1 -> a1, G1 -> g1, etc.)iflen(name)<=2andname[0].isupper():returnname.lower()# handle PascalCase rules (PredConjBorrowAuxNeg -> pred_conj_borrow_aux_neg)# insert underscore before uppercase lettersresult=[]fori,charinenumerate(name):ifi>0andchar.isupper()and(i==0ornotname[i-1].isupper()):result.append('_')result.append(char.lower())return''.join(result)
[docs]@classmethoddefexplain(cls)->str:"""Get explanation of what this rule does. Returns ------- str The rule's docstring explaining its purpose. """returncls.__doc__or""
[docs]def__eq__(self,other:object)->bool:"""Compare rules for equality. Parameters ---------- other : object Another object to compare with. Returns ------- bool True if rules are of the same type. """returnisinstance(other,self.__class__)
[docs]def__hash__(self)->int:"""Get hash of rule for use in sets/dicts. Returns ------- int Hash based on class name. """returnhash(self.__class__.__name__)
[docs]classPredicateRootRule(Rule):"""Base class for rules that identify predicate root tokens. These rules are applied during the predicate extraction phase to identify which tokens should be considered predicate roots. """rule_type:str='predicate_root'
[docs]classArgumentRootRule(Rule):"""Base class for rules that identify argument root tokens. These rules are applied during the argument extraction phase to identify which tokens should be considered argument roots. """rule_type:str='argument_root'
[docs]classPredConjRule(Rule):"""Base class for rules handling predicate conjunctions. These rules manage how conjoined predicates share or borrow elements like auxiliaries and negations. """type:str='predicate_conj'
[docs]classArgumentResolution(Rule):"""Base class for rules that resolve missing or borrowed arguments. These rules handle cases where predicates need to borrow arguments from other predicates or resolve missing arguments. """type:str='argument_resolution'
[docs]classConjunctionResolution(Rule):"""Base class for rules handling argument conjunctions. These rules manage how conjoined arguments are processed and expanded. """type:str='conjunction_resolution'
[docs]classSimplifyRule(Rule):"""Base class for rules that simplify patterns. These rules are applied when options.simple=True to create simpler predicate-argument patterns. """type:str='simple'
[docs]classPredPhraseRule(Rule):"""Base class for rules that build predicate phrases. These rules determine which tokens from the dependency subtree should be included in the predicate phrase. """type:str='pred_phrase'
[docs]def__init__(self,x:Token)->None:"""Initialize with the token being processed. Parameters ---------- x : Token The token being considered for the predicate phrase. """self.x=xsuper().__init__()
[docs]classArgPhraseRule(Rule):"""Base class for rules that build argument phrases. These rules determine which tokens from the dependency subtree should be included in the argument phrase. """type:str='arg_phrase'
[docs]classLanguageSpecific(Rule):"""Base class for language-specific rules. These rules apply only to specific languages and handle language-specific phenomena. """lang:str|None=None
[docs]classEnglishSpecific(LanguageSpecific):"""Base class for English-specific rules. These rules handle English-specific phenomena like possessives and certain syntactic constructions. """lang:str='English'