Feature Context Free Grammar

pyformlang.fcfg

This submodule implements functions related to feature-based grammars.

Available Classes

Sources

Daniel Jurafsky and James H. Martin, Speech and Language Processing

exception pyformlang.fcfg.ContentAlreadyExistsException[source]

Exception raised when we want to add a content that already exists

class pyformlang.fcfg.FCFG(variables: AbstractSet[Variable] = None, terminals: AbstractSet[Terminal] = None, start_symbol: Variable = None, productions: Iterable[FeatureProduction] = None)[source]

A class representing a feature context-free grammar

Parameters:
variablesset of Variable, optional

The variables of the FCFG

terminalsset of Terminal, optional

The terminals of the FCFG

start_symbolVariable, optional

The start symbol

productionsset of FeatureProduction, optional

The feature productions or rules of the FCFG

Examples

Creation of a FCFG from a textual description.

>>> fcfg = FCFG.from_text("""
>>>          S -> NP[AGREEMENT=?a] VP[AGREEMENT=?a]
>>>          S -> Aux[AGREEMENT=?a] NP[AGREEMENT=?a] VP
>>>          NP[AGREEMENT=?a] -> Det[AGREEMENT=?a] Nominal[AGREEMENT=?a]
>>>          Aux[AGREEMENT=[NUMBER=pl, PERSON=3rd]] -> do
>>>          Aux[AGREEMENT=[NUMBER=sg, PERSON=3rd]] -> does
>>>          Det[AGREEMENT=[NUMBER=sg]] -> this
>>>          Det[AGREEMENT=[NUMBER=pl]] -> these
>>>          "VAR:VP[AGREEMENT=?a]" -> Verb[AGREEMENT=?a]
>>>          Verb[AGREEMENT=[NUMBER=pl]] -> serve
>>>          Verb[AGREEMENT=[NUMBER=sg, PERSON=3rd]] -> "TER:serves"
>>>          Noun[AGREEMENT=[NUMBER=sg]] -> flight
>>>          Noun[AGREEMENT=[NUMBER=pl]] -> flights
>>>          Nominal[AGREEMENT=?a] -> Noun[AGREEMENT=?a]
>>>     """)

Check if a word is in the FCFG

>>> fcfg.contains(["this", "flight", "serves"])

True

contains(word: Iterable[Terminal | str]) bool[source]

Gives the membership of a word to the grammar

Parameters:
worditerable of Terminal

The word to check

Returns:
containsbool

Whether word if in the FCFG or not

get_parse_tree(word: Iterable[Terminal | str]) ParseTree[source]

Gives the parse tree for a sentence, if possible

Parameters:
worditerable of Terminal

The word to check

Returns:
parse_treeParseTree

The parse tree

Raises:
NotParsableException

When the word is not parsable.

class pyformlang.fcfg.FeatureProduction(head: Variable, body: List[CFGObject], head_feature, body_features, filtering=True)[source]

A feature production or rule of a FCFG

Parameters:
headVariable

The head of the production

bodyiterable of CFGObject

The body of the production

head_featureFeatureStructure

The feature structure of the head

body_featuresIterable of FeatureStructure

The feature structures of the elements of the body. Must be the same size as the body.

property features

The merged features of the production rules

class pyformlang.fcfg.FeatureStructure(value=None)[source]

The feature structure containing constraints

Parameters:
valueAny, optional

The value of the feature, if defined

add_content(content_name: str, feature_structure: FeatureStructure)[source]

Add content to the current feature structure.

Parameters:
content_namestr

The name of the new feature

feature_structureFeatureStructure

The value of this new feature

Raises:
ContentAlreadyExistsException

When the feature already exists

add_content_path(content_name: str, feature_structure: FeatureStructure, path: List[str])[source]

Add content to the current feature structure at a specific path

Parameters:
content_namestr

The name of the new feature

feature_structureFeatureStructure

The value of this new feature

pathIterable of str

The path where to add the new feature.

Raises:
ContentAlreadyExistsException

When the feature already exists

PathDoesNotExistsException

When the path does not exist

property content: Any

Gets the content of the current node

copy(already_copied=None)[source]

Copies the current feature structure

Parameters:
already_copieddict

A dictionary containing the parts already copied. For internal usage.

Returns:
fsFeatureStructure

The copied feature structure

classmethod from_text(text: str, structure_variables: Dict[str, FeatureStructure] = None)[source]

Construct a feature structure from a text.

Parameters:
textstr

The text to parse

structure_variablesdict of (str, FeatureStructure), optional

Existing structure variables.

Returns:
feature_structureFeatureStructure

The parsed feature structure

get_all_paths()[source]

Get the list of all path in the feature structure

Returns:
pathsIterable of FeatureStructure

The paths

get_dereferenced()[source]

Get the dereferences version of the feature structure. For internal usage.

get_feature_by_path(path: List[str] = None)[source]

Get a feature at a given path.

Parameters:
pathIterable of str, optional

The path to the new feature.

Returns:
feature_structureFeatureStructure

The feature structure at the end of the path.

Raises:
PathDoesNotExistsException

When the path does not exist

property pointer: Any

Gets the pointer of the current node

subsumes(other: FeatureStructure)[source]

Check whether the current feature structure subsumes another one.

Parameters:
otherFeatureStructure

The other feature structure to unify.

Returns:
subsumesbool

Whether the current feature structure subsumes the one.

unify(other: FeatureStructure)[source]

Unify the current structure with another one.

Modifies the current structure.

Parameters:
otherFeatureStructure

The other feature structure to unify.

Raises:
FeatureStructuresNotCompatibleException

When the feature structure cannot be unified.

property value: Any

Gets the value associated to the current node

exception pyformlang.fcfg.FeatureStructuresNotCompatibleException[source]

Raised when trying to unify uncompatible structures

exception pyformlang.fcfg.PathDoesNotExistsException[source]

Raised when looking for a path that does not exist