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_symbol
Variable, optional The start symbol
- productionsset of
FeatureProduction, optional The feature productions or rules of the FCFG
- variablesset of
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
- 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:
- head
Variable The head of the production
- bodyiterable of
CFGObject The body of the production
- head_feature
FeatureStructure 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.
- head
- 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_structure
FeatureStructure 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_structure
FeatureStructure 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
- copy(already_copied=None)[source]
Copies the current feature structure
- Parameters:
- already_copieddict
A dictionary containing the parts already copied. For internal usage.
- Returns:
- fs
FeatureStructure The copied feature structure
- fs
- 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_structure
FeatureStructure The parsed feature structure
- feature_structure
- get_all_paths()[source]
Get the list of all path in the feature structure
- Returns:
- pathsIterable of
FeatureStructure The paths
- pathsIterable of
- 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_structure
FeatureStructure The feature structure at the end of the path.
- feature_structure
- Raises:
- PathDoesNotExistsException
When the path does not exist
- subsumes(other: FeatureStructure)[source]
Check whether the current feature structure subsumes another one.
- Parameters:
- other
FeatureStructure The other feature structure to unify.
- other
- 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:
- other
FeatureStructure The other feature structure to unify.
- other
- Raises:
- FeatureStructuresNotCompatibleException
When the feature structure cannot be unified.