forked from Aunsiels/pyformlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_production.py
58 lines (49 loc) · 1.93 KB
/
feature_production.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Production rules with features."""
from typing import List, Iterable
from pyformlang.cfg import CFGObject, Variable, Production
from .feature_structure import FeatureStructure
class FeatureProduction(Production):
"""A feature production or rule of a FCFG.
Parameters
----------
head:
The head of the production.
body:
The body of the production.
head_feature:
The feature structure of the head.
body_features:
The feature structures of the elements of the body.
Must be the same size as the body.
filtering:
Whether to ignore the epsilon terminals in body.
"""
def __init__(self,
head: Variable,
body: List[CFGObject],
head_feature: FeatureStructure,
body_features: Iterable[FeatureStructure],
filtering: bool = True) -> None:
"""Initializes the feature production."""
super().__init__(head, body, filtering)
self._features = FeatureStructure()
self._features.add_content("head", head_feature)
for i, feature_structure in enumerate(body_features):
self._features.add_content(str(i), feature_structure)
@property
def features(self) -> FeatureStructure:
"""Gets the merged features of the production rules."""
return self._features
def __repr__(self) -> str:
"""Gets the string representation of the feature grammar."""
res = [self.head.to_text()]
cond_head = str(self._features.get_feature_by_path(["head"]))
if cond_head:
res.append("[" + cond_head + "]")
res.append("->")
for i, body_part in enumerate(self.body):
res.append(body_part.to_text())
body_part_cond = str(self._features.get_feature_by_path([str(i)]))
if body_part_cond:
res.append("[" + body_part_cond + "]")
return " ".join(res)