11"""Feature Structure"""
22
3- from typing import Dict , List , Iterable , Tuple , Optional , Any
3+ from typing import Dict , List , Iterable , Tuple , Optional , Hashable
44
55
66class ContentAlreadyExistsException (Exception ):
@@ -25,40 +25,11 @@ class FeatureStructure:
2525
2626 """
2727
28- def __init__ (self , value : Any = None ) -> None :
28+ def __init__ (self , value : Hashable = None ) -> None :
2929 self ._content : Dict [str , FeatureStructure ] = {}
3030 self ._value = value
3131 self ._pointer : Optional [FeatureStructure ] = None
3232
33- def copy (self , already_copied : Dict ["FeatureStructure" ,
34- "FeatureStructure" ] = None ) \
35- -> "FeatureStructure" :
36- """Copies the current feature structure
37-
38- Parameters
39- ----------
40- already_copied : dict
41- A dictionary containing the parts already copied.
42- For internal usage.
43-
44- Returns
45- ----------
46- fs : :class:`~pyformlang.fcfg.FeatureStructure`
47- The copied feature structure
48- """
49- if already_copied is None :
50- already_copied = {}
51- if self in already_copied :
52- return already_copied [self ]
53- new_fs = FeatureStructure (self .value )
54- if self ._pointer is not None :
55- pointer_copy = self ._pointer .copy (already_copied )
56- new_fs .pointer = pointer_copy
57- for feature , content in self ._content .items ():
58- new_fs .content [feature ] = content .copy (already_copied )
59- already_copied [self ] = new_fs
60- return new_fs
61-
6233 @property
6334 def content (self ) -> Dict [str , "FeatureStructure" ]:
6435 """Gets the content of the current node"""
@@ -75,12 +46,12 @@ def pointer(self, new_pointer: "FeatureStructure") -> None:
7546 self ._pointer = new_pointer
7647
7748 @property
78- def value (self ) -> Any :
49+ def value (self ) -> Hashable :
7950 """Gets the value associated to the current node"""
8051 return self ._value if self .pointer is None else self .pointer .value
8152
8253 @value .setter
83- def value (self , new_value : Any ) -> None :
54+ def value (self , new_value : Hashable ) -> None :
8455 """Gets the value associated to the current node"""
8556 self ._value = new_value
8657
@@ -256,6 +227,35 @@ def __repr__(self) -> str:
256227 res .append ("." .join (path ) + "=" + str (value ))
257228 return " | " .join (res )
258229
230+ def copy (self , already_copied : Dict ["FeatureStructure" ,
231+ "FeatureStructure" ] = None ) \
232+ -> "FeatureStructure" :
233+ """Copies the current feature structure
234+
235+ Parameters
236+ ----------
237+ already_copied : dict
238+ A dictionary containing the parts already copied.
239+ For internal usage.
240+
241+ Returns
242+ ----------
243+ fs : :class:`~pyformlang.fcfg.FeatureStructure`
244+ The copied feature structure
245+ """
246+ if already_copied is None :
247+ already_copied = {}
248+ if self in already_copied :
249+ return already_copied [self ]
250+ new_fs = FeatureStructure (self .value )
251+ if self ._pointer is not None :
252+ pointer_copy = self ._pointer .copy (already_copied )
253+ new_fs .pointer = pointer_copy
254+ for feature , content in self ._content .items ():
255+ new_fs .content [feature ] = content .copy (already_copied )
256+ already_copied [self ] = new_fs
257+ return new_fs
258+
259259 @classmethod
260260 def from_text (cls ,
261261 text : str ,
0 commit comments