forked from Aunsiels/pyformlang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_production.py
More file actions
54 lines (45 loc) · 1.91 KB
/
feature_production.py
File metadata and controls
54 lines (45 loc) · 1.91 KB
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
"""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 : :class:`~pyformlang.cfg.Variable`
The head of the production
body : iterable of :class:`~pyformlang.cfg.CFGObject`
The body of the production
head_feature : :class:`~pyformlang.fcfg.FeatureStructure`
The feature structure of the head
body_features : Iterable of :class:`~pyformlang.fcfg.FeatureStructure`
The feature structures of the elements of the body.
Must be the same size as the body.
"""
def __init__(self,
head: Variable,
body: List[CFGObject],
head_feature: FeatureStructure,
body_features: Iterable[FeatureStructure],
filtering: bool = True) -> None:
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:
"""The merged features of the production rules"""
return self._features
def __repr__(self) -> str:
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)