parser module

class py_amr2fred.parser.Parser

Bases: object

A 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.

Parameters:

root (Node) – The root node of the AMR tree to be processed.

Returns:

The updated AMR tree with parent nodes properly integrated.

Return type:

Node

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.

Parameters:

root (Node) – The root node to check and process.

Returns:

The updated root node, or None if the root node’s status is not “OK”.

Return type:

Node | None

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.

Parameters:

root (Node) – The root node to be checked.

Returns:

True if the node has a valid AMR instance with a prefix, False otherwise.

Return type:

bool

check_missing_instances(root: Node) Node

Checks for missing instances in the AMR and attempts to correct them.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node after corrections, if necessary.

Return type:

Node

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.

Parameters:

root (Node) – The root node of the AMR structure to be processed.

Returns:

The root node after verifying and adjusting :opX relations.

Return type:

Node

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.

Parameters:

root (Node) – The root node to be processed and elaborated.

Returns:

The modified root node after date entity elaboration.

Return type:

Node

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.

Parameters:

var (str) – The variable to be disambiguated.

Returns:

The disambiguated value or the original variable prefixed with Glossary.FRED.

Return type:

str

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.

Parameters:

root (Node) – The root node of the AMR structure to be verified.

Returns:

The root node after domain verification.

Return type:

Node

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.

Parameters:

root (Node) – The root node of the AMR to be translated.

Returns:

The root node after being processed and translated into FRED format.

Return type:

Node

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.

Parameters:

root (Node) – The root node to compare against other nodes.

Returns:

A list of nodes that are equal to the given root node.

Return type:

list[Node]

get_instance_alt(node_id) Node | None

Get the instance associated with the given node ID, if it exists.

Parameters:

node_id (int) – The ID of the node whose instance is to be retrieved.

Returns:

The instance associated with the node, or None if no instance is found.

Return type:

Node or None

get_nodes(relation: str, amr_list: list[str]) Node | None

Retrieves nodes from the AMR string based on the provided top node relation.

Parameters:
  • relation (str) – The top node relation.

  • amr_list (list[str]) – The array of words representing the AMR structure.

Returns:

The root node or None if an error occurs.

Return type:

Node | None

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.

Parameters:

root (Node) – The root node to be checked for equality.

Returns:

The original node matching the root node, or None if no match is found.

Return type:

Node | None

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:

Parser

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.

Parameters:

root (Node) – The node from which to start the search.

Returns:

The nearest ancestor node that represents a verb, or None if none is found.

Return type:

Node | None

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.

Parameters:

root (Node) – The root node of the AMR tree to be processed.

Returns:

The updated AMR tree with refined instance handling.

Return type:

Node

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.

Parameters:

root (Node) – The root node of the AMR structure.

Returns:

The root node after processing inverse relations.

Return type:

Node

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.

Parameters:
  • var (str) – The variable (usually a string) to be checked.

  • node_list (list of Node, optional) – Optional list of nodes to search through (only used if var is a string).

Returns:

True if the variable corresponds to a verb, False otherwise.

Return type:

bool

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.

Parameters:

root (Node) – The root node of the AMR structure to be processed.

Returns:

The root node after verifying and adjusting :li relations.

Return type:

Node

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.

Parameters:

root (Node) – The root node of the AMR tree to be processed.

Returns:

The transformed AMR tree with updated nodes and relations.

Return type:

Node

logic_triples_integration(root: Node) Node

Integrates logical triples into the AMR representation.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node with integrated logical triples.

Return type:

Node

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.

Parameters:

var (str) – The variable to be checked for a matching manner adverb.

Returns:

The first matching manner adverb or an empty string if no match is found.

Return type:

str

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.

Parameters:

root (Node) – The root node from which to start modifier verification.

Returns:

The modified root node with verified and transformed modifiers.

Return type:

Node

multi_sentence(root: Node) Node

Handles multi-sentence cases in the AMR string.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node after multi-sentence processing.

Return type:

Node

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.

Parameters:

amr (str) – The AMR string to normalize.

Returns:

The normalized AMR string.

Return type:

str

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.

Parameters:

word (str) – The word whose occurrence is to be calculated.

Returns:

The updated occurrence count for the word.

Return type:

int

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.

Parameters:

num (int) – The integer to be converted into an ordinal.

Returns:

The ordinal representation of the given number.

Return type:

str

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.

Parameters:

root (Node) – The root node to be processed.

Returns:

The modified root node.

Return type:

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.

Parameters:
  • root (Node) – The root node to be processed.

  • prefix (str) – The prefix to be added to the instance’s variable.

Returns:

The modified root node after elaboration.

Return type:

Node

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:
  1. Strips accents from the input string.

  2. Retrieves the root node.

  3. Applies corrections for missing instances.

  4. Handles multi-sentence AMR representations.

  5. Translates relations and values into FRED format.

  6. Elaborates on verbs and resolves ambiguities.

  7. Adds the special TOPIC node if necessary.

  8. Corrects residual parsing errors.

  9. Integrates logical triples into the representation.

Parameters:

amr (str) – The AMR string to parse.

Returns:

The root node of the parsed AMR.

Return type:

Node

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.

Parameters:

root (Node) – The root node to be processed.

Returns:

The modified root node after preposition control elaboration.

Return type:

Node

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.

residual(root: Node) Node

Attempts to correct residual errors in the parsed AMR.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node after error correction.

Return type:

Node

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.

Parameters:

root (Node) – The root node to be processed and elaborated.

Returns:

The modified root node after elaboration.

Return type:

Node

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.

Parameters:

amr (str) – The AMR string to convert.

Returns:

A list of words extracted from the AMR string, or None if an error occurs.

Return type:

list[str] | None

static strip_accents(amr: str) str

Strips any accent marks from the given AMR string.

Parameters:

amr (str) – The AMR string to process.

Returns:

The AMR string with accents removed.

Return type:

str

topic(root: Node) Node

Determines if a special topic node needs to be added to the AMR.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node after topic handling.

Return type:

Node

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.

verbs_elaboration(root: Node) Node

Elaborates on verbs and resolves ambiguities for predicate arguments.

Parameters:

root (Node) – The root node of the AMR.

Returns:

The root node after verb elaboration.

Return type:

Node