Source code for decomp.semantics.predpatt.core.argument
"""Argument representation for predicate-argument structures.This module provides the Argument class, which representsarguments extracted from dependency parse trees in the PredPatt semanticextraction system. Arguments are the participants in predicate-argumentstructures, such as subjects, objects, and other dependents of predicates.Arguments can be simple (single tokens) or complex (multi-token phrases),and support operations like copying, creating references (for sharedarguments), and expanding coordinated structures.Classes-------Argument The main class representing predicate arguments.Functions---------sort_by_position Utility function for sorting items by position."""from__future__importannotationsfromtypingimportTYPE_CHECKINGfrom..typingimportTfrom..utils.ud_schemaimportdep_v1from.tokenimportTokenifTYPE_CHECKING:from..rules.baseimportRulefrom..typingimportUDSchema
[docs]defsort_by_position(x:list[T])->list[T]:"""Sort items by their position attribute."""returnsorted(x,key=lambday:y.position)
[docs]classArgument:"""Represents an argument of a predicate. Arguments are extracted from dependency parse trees and represent the participants in predicate-argument structures. Parameters ---------- root : Token The root token of the argument. ud : module, optional The Universal Dependencies module to use (default: dep_v1). rules : list, optional List of rules that led to this argument's extraction. Attributes ---------- root : Token The root token of the argument. rules : list List of extraction rules applied. position : int Position of the root token (copied from root.position). ud : module The UD version module being used. tokens : list[Token] List of tokens forming the argument phrase. share : bool Whether this is a shared/borrowed argument (default: False). """
[docs]def__init__(self,root:Token,ud:UDSchema=dep_v1,rules:list[Rule]|None=None,share:bool=False,)->None:"""Initialize an Argument. Parameters ---------- root : Token The root token of the argument. ud : module, optional The Universal Dependencies module to use. rules : list, optional List of rules that led to this argument's extraction. WARNING: Default is mutable list - modifying one argument's rules may affect others if default is used. This behavior is intentional to match the original PredPatt implementation. """# maintain exact initialization order as originalself.root=rootself.rules=rulesifrulesisnotNoneelse[]self.position=root.positionself.ud=udself.tokens:list[Token]=[]self.share=shareself.type:str|None=None
[docs]def__repr__(self)->str:"""Return string representation. Returns ------- str String in format 'Argument(root)'. """returnf"Argument({self.root})"
[docs]defcopy(self)->Argument:"""Create a copy of this argument. Creates a new Argument with the same root and copied lists for rules and tokens. The share flag is not copied. Returns ------- Argument A new argument with copied rules and tokens lists. """x=Argument(self.root,self.ud,self.rules[:])x.tokens=self.tokens[:]returnx
[docs]defreference(self)->Argument:"""Create a reference (shared) copy of this argument. Creates a new Argument marked as shared (share=True) with the same tokens list (not copied). Used for borrowed arguments. Returns ------- Argument A new argument with share=True and shared tokens list. """x=Argument(self.root,self.ud,self.rules[:])x.tokens=self.tokens# share the same listx.share=Truereturnx
[docs]defis_reference(self)->bool:"""Check if this is a reference (shared) argument. Returns ------- bool True if share attribute is True. """returnself.share
[docs]defisclausal(self)->bool:"""Check if this is a clausal argument. Clausal arguments are those with governor relations indicating embedded clauses: ccomp, csubj, csubjpass, or xcomp. Returns ------- bool True if the argument root has a clausal governor relation. """returnself.root.gov_relin{self.ud.ccomp,self.ud.csubj,self.ud.csubjpass,self.ud.xcomp}
[docs]defphrase(self)->str:"""Get the argument phrase. Joins the text of all tokens in the argument with spaces. The tokens are joined in the order they appear in the tokens list, which may be sorted by position during phrase extraction. Returns ------- str Space-joined text of all tokens in the argument. """return" ".join(x.textforxinself.tokens)
[docs]defcoords(self)->list[Argument]:"""Get coordinated arguments including this one. Expands coordinated structures by finding conjunct dependents of the root token. Does not expand ccomp or csubj arguments. Returns ------- list[Argument] List of arguments including self and any conjuncts, sorted by position. """# import here to avoid circular dependencyfrom..importrulesasR# noqa: N812coords=[self]# don't consider the conjuncts of ccomp, csubj and amodifself.root.gov_relnotin{self.ud.ccomp,self.ud.csubj}:ifself.root.dependentsisNone:raiseTypeError(f"Cannot find coordinated arguments for argument {self}: "f"root token has no dependency information",)foreinself.root.dependents:ife.rel==self.ud.conj:coords.append(Argument(e.dep,self.ud,[R.m()]))returnsort_by_position(coords)