parser module
- class py_amr2fred.parser.Parser
Bases:
objectA class for parsing Abstract Meaning Representation (AMR) strings and transforming them into nodes representing logical structure.
This class provides methods to parse an AMR string, manipulate its nodes, and process multi-sentence, recursive, and logical error handling. It also integrates semantic information through specific transformations.
- Attributes:
nodes (list): A list of nodes generated during parsing.
nodes_copy (list): A copy of the node list.
couples (list): A list of couples (pairs of related nodes).
removed (list): A list of nodes removed during parsing.
to_add (list): A list of nodes to be added during parsing.
vars (list): A list of variables used during parsing.
root_copy (Node): A copy of the root node.
topic_flag (bool): A flag indicating whether the topic node should be added.
__parser (Parser): The singleton instance of the Parser class.
- add_parent_list(root: Node) Node
Recursively updates the given AMR tree by ensuring that all parent nodes referenced in parent_list are included in the main node_list.
This method identifies nodes with non-empty parent_list and verifies whether their parent nodes are already present in root.node_list. If a parent node is missing, it is added to maintain structural consistency. The function then recursively applies this process to all child nodes.
- static args(root: Node)
Process the arguments of the given root node and update the related nodes.
This method iterates through the node_list of the root node, and if any node’s relation matches the pattern defined in Glossary.AMR_ARG, it assigns the root’s verb to the node.
- Parameters:
root (Node) – The root node whose arguments are to be processed.
- Returns:
None
- check(root: Node) Node | None
Recursively checks the status of nodes in the given root node and removes those that are not in an “OK” status.
This method traverses the node list of the given root node, checking the status of each node. If a node’s status is not “OK”, it is removed from the list and added to a removed list. If the node has child nodes, the method is recursively applied to them as well. The method returns the updated root node, or None if the root node is not in an “OK” status.
- static check_for_amr_instances(root: Node) bool
Check if a root node has an AMR instance and a valid prefix.
This method checks whether the root node has an instance from the list of defined AMR instances and if the node has a prefix. It returns True if both conditions are satisfied, otherwise returns False.
- check_missing_instances(root: Node) Node
Checks for missing instances in the AMR and attempts to correct them.
- control_ops(root: Node) Node
Verifies and processes operational (:opX) relations in the AMR structure.
This method checks if the root node contains :opX relations and ensures they follow specific semantic rules.
- date_child_elaboration(child: Node)
Elaborate on a child node related to a date entity.
This method modifies the given child node by updating its relation to reflect the AMRB prefix and applying any necessary transformations using other_instance_elaboration_prefix. The status of the child node is set to NodeStatus.OK.
- Parameters:
child (Node) – The child node to be processed.
- date_entity(root: Node) Node
Elaborate on the date-related entities within the given root node.
This method processes the root node, checking if it represents a date entity. If the root node is identified as a date entity, it then processes each of its child nodes related to date components (e.g., era, decade, century, calendar, weekday, etc.) by invoking the date_child_elaboration method.
- static disambiguation(var: str) str
Disambiguate a variable based on a predefined list of terms.
This method checks if the given variable exists in the predefined list of DULs (Glossary.DULS_CHECK). If found, it returns the corresponding value from Glossary.DULS. Otherwise, it returns the variable prefixed with Glossary.FRED.
- dom_verify(root: Node) Node
Verifies and processes the AMR domain (:domain) for the given root node.
This method checks if the root node has a :domain child and processes it according to specific rules. If a domain node is found, it adjusts its relation and variable name, assigns appropriate semantic roles, and updates the root node accordingly. The method also ensures recursive verification of all child nodes.
- fred_translate(root: Node) Node
Translates the relations and values in the AMR string into FRED format.
This method processes the root node and its children by applying several translation checks and transformations to convert AMR-specific relations and values into the corresponding FRED format. It handles various aspects, including operations, list verifications, inverses, modifiable relations, and instance elaborations.
- get_equals(root: Node) list[py_amr2fred.node.Node]
Retrieves all nodes that are equal to the given root node.
This method checks for equality between the root node and other nodes in the current context and returns a list of nodes that are considered equal to the root.
- get_instance_alt(node_id) Node | None
Get the instance associated with the given node ID, if it exists.
- get_nodes(relation: str, amr_list: list[str]) Node | None
Retrieves nodes from the AMR string based on the provided top node relation.
- get_original(root: Node) Node | None
Retrieve the original node matching the root node.
This method searches for a node that matches the root node in terms of equality and ensures the node has a non-None instance. It returns the first matching node or None if no match is found.
- static get_parser()
Returns the singleton instance of the Parser class.
If an instance of the Parser class does not exist, it will create and return one.
- Return type:
- get_verb_ancestor(root: Node) Node | None
Retrieves the closest verb ancestor of the given node.
This method traverses up the node hierarchy to find the nearest ancestor whose instance matches a verb pattern defined in Glossary.AMR_VERB2. It follows these steps:
Starts from the given root node.
Moves up the hierarchy while the node ID is greater than 0 and the parent exists.
Checks if the parent’s instance matches the AMR verb pattern.
Returns the first matching parent node or the last traversed node if no match is found.
- instance_elaboration(root: Node) Node
Processes an AMR (Abstract Meaning Representation) tree node to refine its instance representation, adjust its status, and classify it based on predefined linguistic patterns.
This method ensures proper handling of AMR instances by: - Updating the status of nodes based on their relation type. - Identifying verb instances and adjusting their representation. - Modifying instance variables and assigning appropriate types. - Recursively processing child nodes.
- inverse_checker(root: Node) Node
Identifies and processes inverse relations within the AMR structure.
This method ensures that inverse relations (i.e., those ending with -of) are handled correctly. It performs the following operations:
Identifies nodes with inverse relations.
If the root node has a :top relation and only one inverse, it restructures the hierarchy.
Creates new nodes where necessary, ensuring proper relation consistency.
Adjusts parent-child relationships by reassigning inverse relations.
Recursively processes child nodes to handle all inverse relations.
- static is_verb(var, node_list: list[py_amr2fred.node.Node] | None = None) bool
Check if the provided variable corresponds to a verb.
This method checks if the given variable represents a verb by either searching Propbank frames or by checking a list of nodes if provided.
- li_verify(root: Node) Node
Processes and verifies :li (list item) relations in the AMR structure.
This method ensures that :li relations are properly handled by transforming them into a structured format. Recursively processes child nodes to ensure consistent list handling.
- list_elaboration(root: Node) Node
Processes an Abstract Meaning Representation (AMR) tree by refining its structure, updating node relations, and handling specific linguistic constructs.
This method applies various transformations and normalizations to the nodes of the input AMR tree, modifying their relations, variables, and statuses based on predefined rules. It ensures consistency in the representation and resolves linguistic constructs such as coordination, date intervals, pronouns, demonstratives, and modality markers.
The function performs the following operations:
Calls root_elaboration, date_entity, and prep_control to preprocess the root node.
Iterates through the child nodes (node_list) and applies transformation rules.
Handles special cases such as:
Converting Wikidata links.
Processing negation, modality, and polarity markers.
Handling personal pronouns and demonstratives.
Normalizing names and entity references.
Adjusting relations for AMR constructs like :quant, :age, and :degree.
Ensuring RDF/OWL compliance where necessary.
Updates self.to_add with new nodes when required.
- static manner_adverb(var: str) str
Find a matching manner adverb for a given variable.
This method searches through a list of defined manner adverbs and returns the first one that matches the given variable using regular expression matching. If no match is found, an empty string is returned.
- mod_verify(root: Node) Node
Verifies and adjusts modifier nodes in the AMR structure.
This method processes modifier nodes (:mod) within the given root node, ensuring proper categorization and transformation based on their type. The verification involves:
Checking if the root instance is a verb, which influences modifier handling.
Identifying domain (:domain) and modifier (:mod) child nodes.
Handling cases where the modifier includes :degree and :compared-to, leading to instance modification.
Differentiating between adjectives, demonstratives, and other modifiers to assign the appropriate RDF/OWL relation (dul:hasQuality, rdf:type, etc.).
Ensuring correct subclass and association relationships for non-verb modifiers.
- static normalize(amr: str) str
Normalizes an AMR string by replacing newline characters and adjusting spacing.
This method processes the input AMR string by performing several normalization steps:
Converts carriage returns and newline characters to spaces.
Strips leading and trailing whitespace.
Adds spaces around parentheses and slashes for consistent formatting.
Replaces tabs with spaces.
Collapses multiple consecutive spaces into a single space.
The resulting string is easier to process and ensures a consistent format for further manipulation and analysis.
- occurrence(word: str) int
Calculate and update the occurrence count of a given word.
This method updates the occurrence count of the word in the couples list and appends a new entry if the word is not already present.
- static ordinal(num: int) str
Convert a number to its ordinal representation.
This method converts a given integer into its corresponding ordinal form (e.g., 1 → “1st”, 2 → “2nd”, 3 → “3rd”, etc.). It handles exceptions for numbers 11, 12, and 13, which always have the “th” suffix.
- other_instance_elaboration(root: Node) Node
Elaborate on instances other than verbs related to the given root node.
This method retrieves the instance associated with the root node and, based on its properties, generates a new variable for the instance, updates its relation, and assigns specific values to the instance based on predefined glossaries. It also modifies nodes that are equal to the root node.
- other_instance_elaboration_prefix(root: Node, prefix: str) Node
Elaborate on an instance by adding a prefix and handling special instances.
This method updates the variable and relation of a node’s instance based on a given prefix. It also handles special instances and modifies the relation to RDF_TYPE where applicable. The method ensures that the instance is updated with a unique variable and correct status.
- parse(amr: str) Node
Parses the given AMR string and returns the root node of the AMR representation.
- This method processes the AMR string in multiple stages:
Strips accents from the input string.
Retrieves the root node.
Applies corrections for missing instances.
Handles multi-sentence AMR representations.
Translates relations and values into FRED format.
Elaborates on verbs and resolves ambiguities.
Adds the special TOPIC node if necessary.
Corrects residual parsing errors.
Integrates logical triples into the representation.
- prep_control(root: Node) Node
Process a root node representing a preposition, adding a quality node and adjusting relations.
This method checks if the root node represents a valid preposition and, if so, processes the node by creating a quality node, adjusting the relations of its child nodes, and handling any manner-related transformations. It updates the root node’s relation and its node list accordingly.
- reinitialise()
Resets the internal structures before a new parsing run.
- remove_instance(root: Node)
Remove the instance associated with the given root node.
This method updates the var of all nodes equal to the root node and removes the instance from the node list of the root node.
- Parameters:
root (Node) – The root node from which the instance should be removed.
- root_elaboration(root: Node) Node
Elaborate on the given root node by processing its instance and its children.
This method performs various transformations on the root node and its associated instance. It checks for specific conditions in the node and its children, modifying relationships, variables, and adding new nodes based on predefined glossaries and logical rules. The method also handles special cases like conjunctions, disjunctions, concessions, and conditions.
- set_equals(root: Node)
Sets the var attribute of nodes that are equal to the given root node.
This method iterates over nodes that are considered equal to the given root node, and sets their var attribute to match the var attribute of the root node.
- Parameters:
root (Node) – The root node whose var value will be propagated to equal nodes.
- string2array(amr: str) list[str] | None
Converts an AMR string into a list of words, normalizing and processing special characters as necessary.
This method processes the input AMR string and splits it into a list of words. It handles normalizations such as converting words to lowercase and replacing special characters like spaces, quotes, and underscores. Words that are quoted are processed to preserve their meaning in the AMR structure.
If an error occurs during processing (such as a malformed string), the method logs a warning and returns None.
- treat_instance(root: Node)
Process and modify the given root node’s instance and update all equal nodes.
This method updates the var of all nodes that are equal to the root node and changes the status of the instance associated with the root node to REMOVE.
- Parameters:
root (Node) – The root node to be treated.