API Documentation for PyLFG#

API Reference for the classes in parse_tree.py#

This module provides classes to represent and visualize LFG parse trees. The LFGParseTree class provides a visualization of the tree structure of the sentence, as well as functional annotations in the lexical items.

class pylfg.parse_tree.LFGParseTreeNode(label: str, token: str, functional_labels: dict = None, children=None)[source]#

Bases: object

A class representing a node in a LFG (Lexical Functional Grammar) parse tree. Each node has a label, which can be either a lexical or functional category, and a list of child nodes. It also has a token attribute, which is the string representation of the token the node represents. Parameters: - label (str): The label of the node, typically a non-terminal symbol or a terminal symbol. - token (str): The token the node represents, if any. - functional_labels (Dict[str, str]): functional labels of the lexical item in the XLFG standard format, where keys are the functional labels and values are the corresponding label values - children (List[LFGParseTreeNode]): The children of the node.

add_functional_label(label: str, value: str)[source]#

Add a new functional label :param label: (str) functional label :param value: (str) corresponding label value

get_functional_label(label: str)[source]#

get the value of the functional label specified :param label: (str) functional label :return: (str) corresponding label value

remove_functional_label(label: str)[source]#

remove the specified functional label from the functional_labels :param label: (str) functional label

get_all_functional_labels()[source]#

get all functional labels :return: dict of functional labels in the XLFG standard format

calculate_e_structure()[source]#

Calculate the e-structure representation of the parse tree node

is_leaf()[source]#

Determine if the node is a leaf node (i.e., has no children). Returns: - bool: True if the node is a leaf node, False otherwise.

class pylfg.parse_tree.LFGParseTreeNodeF(label: str, token: str, functional_labels: dict = None, children=None)[source]#

Bases: LFGParseTreeNode

Class representing a node in a LFG parse tree. This class is a child class of LFGParseTreeNode and adds functionality for storing and manipulating an F-structure.

add_to_f_structure(attribute: str, value: str)[source]#

Add a new attribute and value to the f_structure of the node.

set_in_f_structure(attribute: str, value: str)[source]#

Set the value of an attribute in the f_structure of the node.

get_from_f_structure(attribute: str)[source]#

Get the value of an attribute in the f_structure of the node.

remove_from_f_structure(attribute: str)[source]#

Remove an attribute from the f_structure of the node.

get_all_f_structure()[source]#

Returns all attributes and values in the f_structure of the node.

has_in_f_structure(attribute: str)[source]#

Check if the attribute is present in the f_structure of the node.

display_f_structure()[source]#

Display the f_structure of the node.

set_path_stm(path_stm)[source]#

Set the path_stm of the node.

get_path_stm()[source]#

Get the path_stm of the node.

class pylfg.parse_tree.LFGParseTreeNodeE(label: str, token: str, functional_labels: dict = None, children=None)[source]#

Bases: LFGParseTreeNode

Class representing a node in a LFG parse tree. This class is a child class of LFGParseTreeNode and adds functionality for storing and manipulating an E-structure.

init(label: str, token: str, children=None)[source]#
Parameters:
  • label (str) – label of the parse tree node

  • token (str) – token of the parse tree node

  • children (List[LFGParseTreeNodeE]) – children of the parse tree node, None by default

set_syntactic_category(category: str)[source]#

Set the syntactic category for this parse tree node

Parameters:

category (str) – the syntactic category

get_syntactic_category()[source]#

Get the syntactic category for this parse tree node

Returns:

the syntactic category

Return type:

str

set_tense(tense: str)[source]#

Set the tense for this parse tree node

Parameters:

tense (str) – the tense

get_tense()[source]#

Get the tense for this parse tree node

Returns:

the tense

Return type:

str

set_aspect(aspect: str)[source]#

Set the aspect for this parse tree node

Parameters:

aspect (str) – the aspect

get_aspect()[source]#

Get the aspect for this parse tree node

Returns:

the aspect

Return type:

str

set_mood(mood: str)[source]#

Set the mood for this parse tree node

Parameters:

mood (str) – the mood

get_mood()[source]#

Get the mood for this parse tree node

Returns:

the mood

Return type:

str

set_semantic_role(role: str)[source]#

Set the semantic role for this parse tree node

Parameters:

role (str) – the semantic role

get_semantic_role()[source]#

Get the semantic role for this parse tree node

set_lexical_head(head: str)[source]#

Set the lexical head for this parse tree node

Parameters:

role (str) – the lexical head

get_lexical_head()[source]#

Get the lexical head for this parse tree node

set_logical_form(form: str)[source]#

Set the logical form for this parse tree node

Parameters:

role (str) – the logical form

get_logical_form()[source]#

Get the logical form for this parse tree node

class pylfg.parse_tree.LFGParseTree(root: LFGParseTreeNode)[source]#

Bases: object

The LFGParseTree class represents a parse tree generated using Lexical Functional Grammar (LFG) parsing. It has a root node, which is an instance of the LFGParseTreeNode class, and a sentence string. The class has several methods such as set_sentence, which allows setting the sentence string for the parse tree, is_leaf, which checks if the current node is a leaf node, to_string, which converts the parse tree to a string representation, f_structure_matrix, which returns the f-structure represented as an attribute-value matrix, to_latex, which writes the parse tree to a LaTeX file, to_networkx, which returns a directed graph (networkx DiGraph) representation of the parse tree and draw, which draws the parse tree using a specified layout and color map. The class also has helper methods that are used internally by the other methods to perform specific tasks such as _to_c_structure_latex_helper which is used by to_latex method to recursively convert the c-structure parse tree to LaTeX format.

set_sentence(sentence: str)[source]#

Set the sentence string for the parse tree.

Parameters:

sentence (str): The sentence string.

is_leaf()[source]#

Check if the current node is a leaf node.

Returns:

bool: True if the current node is a leaf, False otherwise.

to_string() str[source]#

Convert the parse tree to a string representation.

Returns:

str: The string representation of the parse tree.

f_structure_matrix()[source]#

Returns the f-structure represented as an attribute-value matrix.

to_latex(filepath: str)[source]#

Write the parse tree to a LaTeX file.

Parameters:

filepath (str): The path to the LaTeX file.

_to_c_structure_latex_helper(node)[source]#

Helper method that recursively converts the c-structure parse tree to latex format.

to_networkx()[source]#

Returns a directed graph (networkx DiGraph) representation of the parse tree.

draw(layout: str = 'spring', color_map: Dict[str, str] = None)[source]#

Draws the parse tree using a specified layout and color map. If a color map is not provided, the default is white for all nodes.

Example: tree = LFGParseTree(root_node) color_map = {‘NP’: ‘red’, ‘VP’: ‘green’, ‘PP’: ‘blue’} tree.draw(layout=’spring’, color_map=color_map)

get_annotations()[source]#

Returns a dictionary of annotations for the parse tree. The keys are the node labels and the values are the annotations.

class pylfg.parse_tree.FStructure[source]#

Bases: object

This class is used to create and manipulate f-structures, which are data structures used in formal semantics to represent the meaning of a sentence. The class contains several methods for adding, setting, getting, and removing attributes and their values, as well as functional labels. It also has methods for adding, removing, and checking constraints on the attributes and for adding, getting, and generating sentences from f-structures. The class also has a method for transferring the language by applying transfer rules to the f-structure. The class also contains a list for storing other f-structures. The class is a dict-like structure where attributes are the keys and values are the corresponding values.

add(attribute: str, value: str)[source]#

Add a new attribute-value pair to the f-structure :param attribute: (str) the attribute name :param value: (str) the value of the attribute

set(attribute: str, value: str)[source]#

Set the value of the specified attribute :param attribute: (str) the attribute name :param value: (str) the value of the attribute

get(attribute: str)[source]#

Get the value of the specified attribute :param attribute: (str) the attribute name :return: (str) the value of the attribute

remove(attribute: str)[source]#

Remove the specified attribute from the f-structure :param attribute: (str) the attribute name

get_all_attributes()[source]#

get all attributes and their values :return: dict of attributes and their values

has_attribute(attribute: str)[source]#

check if the f-structure has the specified attribute :param attribute: (str) attribute name :return: (bool) True if has the attribute, False otherwise

add_functional_label(label: str, value: str)[source]#

Add a new functional label :param label: (str) functional label :param value: (str) corresponding label value

get_functional_label(label: str)[source]#

get the value of the functional label specified :param label: (str) functional label :return: (str) corresponding label value

remove_functional_label(label: str)[source]#

remove the specified functional label from the f-structure :param label: (str) functional label

get_all_functional_labels()[source]#

get all functional labels :return: dict of functional labels in the XLFG standard format

add_constraint(constraint: str)[source]#

Add a new constraint to the f-structure :param constraint: (str) the constraint to be added

remove_constraint(constraint: str)[source]#

Remove the specified constraint from the f-structure :param constraint: (str) the constraint to be removed

check_constraints()[source]#

Check if all the constraints are satisfied :return: (bool) True if all constraints are satisfied, False otherwise

add_f_structure(f_structure: dict)[source]#

Add a new f-structure to the current f-structure :param f_structure: (dict) the f-structure to be added

get_f_structures()[source]#

Get all f-structures :return: (list) all f-structures

generate_sentence() str[source]#

Generate a sentence from the f-structure :return: (str) the sentence

transfer_language(transfer_rules: dict)[source]#

Transfer the f-structure to a different language using the provided transfer rules. :param transfer_rules: (Dict[str, str]) A dictionary where keys are the attributes in the original language,

and values are the corresponding attributes in the target language. :return: (dict) the transfered attributes

API Reference for the classes in parser.py#

PyLFG is a package for parsing sentences using Lexical Functional Grammar (LFG). This module provides an implementation of the Earley parsing algorithm for building parse trees from sentences and grammar rules specified in LFG.

The primary entry point for the module is the LfgParser class and it’s main methode parse(sentence), which takes a sentence string and a set of grammar rules and lexicon and returns a list of parse trees for the sentence.

The package also provides helper functions for loading grammar rules and lexicon from files, and a LFGParseTree and LFGParseTreeNode class for representing and visualizing parse trees, as well as a FStructure class to represent the f-structure of the analyzed sentence.

class pylfg.parser.LfgParser(grammar_format)[source]#

Bases: object

The main class of PyLFG (Lexical Functional Grammar) module, which provides functionality for parsing sentences using LFG grammars. It is the parent class for XlfgParser and XleParser, which inherit its functionality and extend it for specific uses. The main method of this class is build_parse_trees which takes a sentence as input and returns a list of parse trees for the sentence. The class also has an __init__ method which takes in a grammar_format parameter, which determines which method to call for building parse trees.

Parameters:

grammar_format – str. The format of the grammar to be used for parsing.

Variables:

grammar_format – str. The format of the grammar being used by the parser.

parse() list[source]#

Parses a given sentence using the specified grammar format and returns a list of parse trees. :param sentence: str. The sentence to be parsed. :return: list of LFGParseTree objects representing the possible parse trees for the sentence.

class pylfg.parser.XlfgParser(grammar: dict, lexicon: dict)[source]#

Bases: LfgParser

XlfgParser is a class that inherits from LfgParser and implements a parser for the XLFG format. XLFG is a fast, accurate deep parser for LFG grammar. These outputs are phrase structures, predicate-argument structures and predicate-thematic relationships. It takes in a grammar in the form of a dictionary and a lexicon object as input and uses them to build parse trees for a given sentence. :param grammar: dict. A dictionary containing the X-bar LFG grammar rules in the form of a string. :param lexicon: Lexicon. A lexicon object containing the lexical entries for the words in the language. :ivar grammar: dict. A dictionary containing the X-bar LFG grammar rules in the form of a string. :ivar lexicon: Lexicon. A lexicon object containing the lexical entries for the words in the language. :method build_parse_trees(sentence: str) -> list: Builds a parse tree for a given sentence using the grammar and lexicon provided in the class.

param sentence:

str. Sentence to be parsed.

return:

list. A list of LFGParseTree objects representing the possible parse trees for the given sentence.”

build_parse_trees() list[source]#

Builds parse trees for a given sentence using the grammar and lexicon provided to the XlfgParser object. :param sentence: string. The sentence to be parsed. :return: list. A list of LFGParseTree objects representing the possible parse trees for the sentence.

class pylfg.parser.XleParser(template_file, features_file, grammar_file, lexicon_file, fst_dir)[source]#

Bases: LfgParser

Initialize the XleParser with the necessary resources.

Parameters:
  • template_file – str file path of the template file.

  • features_file – str file path of the features file.

  • grammar_file – str file path of the grammar file.

  • lexicon_file – str file path of the lexicon file.

  • fst_dir – str directory containing the finite state transducers (FSTs) for disambiguation.

build_parse_trees(sentence)[source]#

Use the resources from the lexicon, grammar, and FSTs to build parse trees for the given sentence. :param sentence: str

Sentence to parse.

Returns:

List[str] List of parse trees for the given sentence.

class pylfg.parser.Grammar(grammar_format: str, grammar: dict)[source]#

Bases: object

A class representing a lexical functional grammar.

parse()[source]#

Parses the grammar based on the format specified in the constructor.

Returns:

A parsed representation of the grammar.

Raises:

ValueError – If the grammar format is not supported.

class pylfg.parser.Lexicon(lexicon_format: str, lexicon: dict)[source]#

Bases: object

A class representing a lexical functional grammar lexicon.

parse()[source]#

Parses the lexicon based on the format specified in the constructor.

Returns:

A parsed representation of the lexicon.

Raises:

ValueError – If the lexicon format is not supported.

class pylfg.parser.XlfgGrammar(grammar_file: str)[source]#

Bases: object

A class for parsing and storing XLFG grammar rules.

Parameters:

grammar_file – str. the file containing XLFG grammar rules

Variables:

grammar – dict. A dictionary with the nonterminals as keys and lists of rules as values.

static parse_grammar(grammar_file: str) dict[source]#

Given a file containing XLFG grammar rules, returns a dictionary with the nonterminals as keys and lists of rules as values.

Parameters:

grammar_file – the file containing XLFG grammar rules

Returns:

a dictionary with the nonterminals as keys and lists of rules as values

class pylfg.parser.XlfgLexicon(lexicon_file: str)[source]#

Bases: object

A class for parsing and storing XLFG lexicon entries.

Parameters:

lexicon_file – str. the file containing XLFG lexicon entries

Variables:

lexicon – dict. A dictionary with the words as keys and lexicon entries as values.

static parse_lexicon(lexicon_file: str) dict[source]#

Given a file containing XLFG lexicon entries, returns a dictionary with the words as keys and lexicon entries as values.

Parameters:

lexicon_file – the file containing XLFG lexicon entries

Returns:

a dictionary with the words as keys and lexicon entries as values

class pylfg.parser.XleGrammar(grammar_file: str)[source]#

Bases: Grammar

A class for parsing and storing XLE grammar rules.

Parameters:

grammar_file – str. the file containing XLE grammar rules

Variables:

grammar – dict. A dictionary with the nonterminals as keys and lists of rules as values.

parse()[source]#

Parse the grammar in XLE format and return a dictionary in the format {non_terminal: List[Tuple[str, List[str], Dict[str, str], Dict[str, str]]]}

class pylfg.parser.XleLexicon(lexicon_file: str)[source]#

Bases: Lexicon

A class for parsing and storing XLE lexicon entries.

Parameters:

lexicon_file – str. the file containing XLE lexicon entries

Variables:

lexicon – dict. A dictionary with the words as keys and lexicon entries as values.

parse()[source]#

Parse the lexicon in XLE format and return a dictionary in the format {token: Tuple[List[Tuple[str, str]], Dict[str, str]]}

pylfg.parser.tokenize(sentence, fst_tokenizer_path)[source]#

Tokenize the given sentence using an FST tokenizer if present in the given directory, otherwise use a default string split method.

pylfg.parser.fst_tokenize(sentence, fst_path)[source]#

Tokenize the given sentence using the FST at the given path.

pylfg.parser.build_trees(disambiguated_entries, grammar, templates)[source]#

Build parse trees from the disambiguated entries using the provided grammar and templates.

API Reference for the classes in parser_gen.py#

The module parser_gen.py is used to convert the XLFG grammar and lexicon format to the format required by the parser generator and also generates a parser using the converted grammar and lexicon. The module contains two classes: ParserGenerator and CompiledLfgParser.

class pylfg.parser_gen.ParserGenerator(grammar_format)[source]#

Bases: object

The ParserGenerator class is used to convert the LFG grammar and lexicon format to the format required by the parser generator. It also generates a parser using the converted grammar and lexicon.

convert_grammar(grammar: dict) str[source]#

Convert the LFG grammar format to the format required by the parser generator

convert_lexicon(lexicon: dict) str[source]#

Convert the current lexicon format to the format required by the parser generator

generate_parser(grammar: str, lexicon: str)[source]#

Use the parser generator to generate a parser

class pylfg.parser_gen.CompiledLfgParser(grammar_format, grammar: dict, lexicon: dict)[source]#

Bases: object

The CompiledLfgParser class is used to generate a parser from a given grammar and lexicon, and then use that parser to parse sentences and build parse trees.

Attributes: grammar_format (str): The format of the input grammar, should be “xlfg” or “xle”. grammar (dict): The grammar in the specified format. lexicon (dict): The lexicon in the specified format. parser_generator (ParserGenerator): An instance of the ParserGenerator class, used to generate the parser.

Methods: parse(sentence: str) -> list: Uses the generated parser to parse the given sentence and build parse trees. Returns a list of LFGParseTree objects.

parse(sentence: str) list[source]#

Takes in a string sentence and returns a list of LFGParseTree objects, generated from the parsed sentence using the pre-compiled grammar and lexicon.

API Reference for the classes in xle.py#

The above docstrings describe the functions of the xle.py module, which includes loading templates, features, grammar, and lexicon from files. The load_templates function takes a template file as an input and returns a dictionary of templates, where the keys are the template names and the values are the template strings. The load_features function takes a features file as an input and returns a dictionary of features, where the keys are the feature names and the values are lists of feature values. The parse_grammar function takes a grammar file as an input, parse an XLE grammar file and return a dictionary of grammar rules, where the keys are non-terminal symbols and the values are lists of tuples representing the corresponding grammar rules in the form (lhs, rhs, c_constraints, f_constraints). The parse_lexicon function takes a lexicon file as an input, parse a lexicon file and return a dictionary of lexical entries, where the keys are words and the values are tuples of the form (morph, features).

pylfg.xle.load_templates(template_file)[source]#

Loads templates from a given file and returns a dictionary of templates, where the keys are the template names and the values are the template strings.

pylfg.xle.load_features(features_file)[source]#

Loads features from a given file and returns a dictionary of features, where the keys are the feature names and the values are lists of feature values.

pylfg.xle.parse_grammar(grammar_file: str) Dict[str, List[Tuple[str, List[str], Dict[str, str], Dict[str, str]]]][source]#

Parse an XLE grammar file and return a dictionary of grammar rules, where the keys are non-terminal symbols and the values are lists of tuples representing the corresponding grammar rules in the form (lhs, rhs, c_constraints, f_constraints).

pylfg.xle.integrate_lfg(lexicon, grammar)[source]#

Integrates a lexicon and grammar into a LFG (Lexical Functional Grammar) format. It loads templates, feature declaration, morphconfig and rules from specific files. Replaces placeholders in the templates with the parsed lexicon and grammar. Returns the LFG in string format.

API Reference for the classes in xlfg.py#

The file xlfg.py contains several functions that pertain to processing and manipulating phrase structure rules and lexicon entries in the eXtended Lexical Functional Grammar (XLFG) formalism.

pylfg.xlfg.parse_rule(rule: str) Tuple[str, List[str]][source]#

Given a string representation of a XLFG phrase structure rule, returns a tuple of the rule in the format “LHS → RHS” and a list of c-structure constraints.

Parameters:

rule – the string representation of a XLFG phrase structure rule

Returns:

a tuple of the rule in the format “LHS → RHS” and a list of c-structure constraints

pylfg.xlfg.parse_lexicon_entry(lexicon_entry: str) dict[source]#

Given a string representation of a XLFG lexicon entry, returns a dictionary of functional labels and their values.

Parameters:

lexicon_entry – the string representation of a XLFG lexicon entry

Returns:

a dictionary of functional labels and their values

pylfg.xlfg.match_constraints(rule: str, lexicon_entry: dict) bool[source]#

Given a rule and lexicon entry, check if the constraints in the rule are satisfied by the lexicon entry. :param rule: the rule to check constraints against :param lexicon_entry: the lexicon entry to check against the rule’s constraints :return: True if the constraints are satisfied, False otherwise

pylfg.xlfg.match_c_constraints(rule, tokens, i)[source]#

Given a rule, a list of tokens, and an index, check if the c-structure constraints in the rule are satisfied by the token at the given index. :param rule: the rule to check constraints against :param tokens: a list of tokens :param i: the index of the token to check against the rule’s c-structure constraints :return: True if the constraints are satisfied, False otherwise

pylfg.xlfg.match_f_constraints(rule, lexicon_entry)[source]#

Given a rule and a lexicon entry, check if the f-structure constraints in the rule are satisfied by the lexicon entry. :param rule: the rule to check constraints against :param lexicon_entry: the lexicon entry to check against the rule’s f-structure constraints :return: True if the constraints are satisfied, False otherwise

pylfg.xlfg.impose_constraints_in_tree(tree: LFGParseTreeNodeF, constraints: dict)[source]#

Impose the constraints on the parse tree. :param tree: the root node of the parse tree :param constraints: the constraints, in the form of a dictionary where the keys are the functional labels and the values are the corresponding label values

pylfg.xlfg.remove_unused_constraints(node: LFGParseTreeNodeF)[source]#

remove unused constraint labels from a parse tree node and its children

pylfg.xlfg.parse_lexicon(file)[source]#

Given a file path, read the lexicon from the file and return the lexicon entries as a dictionary. :param file: the path of the file containing the lexicon :return: a dictionary of lexicon entries

pylfg.xlfg.parse_xlfg_rule(rule: str) Tuple[str, List[str], Dict[str, str], Dict[str, Dict[str, str]]][source]#

Given a string representation of a XLFG phrase structure rule, returns a tuple of the rule in the format “LHS → RHS”, c-structure constraints, and f-structure constraints.

API Reference for the classes in fst.py#

API Reference for the classes in transfer.translator.py#

Module for performing LFG (Lexical Functional Grammar) based translations.

This module contains the LfgTranslator class, which uses LFG grammars and lexicons to translate sentences from one language to another. The class takes in source and target language information, as well as source and target grammars and lexicons, and uses these to parse and translate sentences.

class pylfg.transfer.translator.LfgTranslator(source_language: str, target_language: str, source_grammar: dict, target_grammar: dict, source_lexicon: dict, target_lexicon: dict)[source]#

Bases: object

The LfgTranslator class is used to translate sentences from one language to another using LFG (Lexical-Functional Grammar) theory.

transfer_f_structure(f_structure: dict) dict[source]#

Translates the f-structure from the source language to the target language :param f_structure: the f-structure of the source language sentence :return: the f-structure of the target language sentence

translate(sentence: str) str[source]#

Translates the sentence from the source language to the target language :param sentence: the source language sentence :return: the target language sentence