MCPcopy
hub / github.com/nltk/nltk / __new__

Method __new__

nltk/featstruct.py:154–197  ·  view source on GitHub ↗

Construct and return a new feature structure. If this constructor is called directly, then the returned feature structure will be an instance of either the ``FeatDict`` class or the ``FeatList`` class. :param features: The initial feature values for this fe

(cls, features=None, **morefeatures)

Source from the content-addressed store, hash-verified

152 ##////////////////////////////////////////////////////////////
153
154 def __new__(cls, features=None, **morefeatures):
155 """
156 Construct and return a new feature structure. If this
157 constructor is called directly, then the returned feature
158 structure will be an instance of either the ``FeatDict`` class
159 or the ``FeatList`` class.
160
161 :param features: The initial feature values for this feature
162 structure:
163
164 - FeatStruct(string) -> FeatStructReader().read(string)
165 - FeatStruct(mapping) -> FeatDict(mapping)
166 - FeatStruct(sequence) -> FeatList(sequence)
167 - FeatStruct() -> FeatDict()
168 :param morefeatures: If ``features`` is a mapping or None,
169 then ``morefeatures`` provides additional features for the
170 ``FeatDict`` constructor.
171 """
172 # If the FeatStruct constructor is called directly, then decide
173 # whether to create a FeatDict or a FeatList, based on the
174 # contents of the `features` argument.
175 if cls is FeatStruct:
176 if features is None:
177 return FeatDict.__new__(FeatDict, **morefeatures)
178 elif _is_mapping(features):
179 return FeatDict.__new__(FeatDict, features, **morefeatures)
180 elif morefeatures:
181 raise TypeError(
182 "Keyword arguments may only be specified "
183 "if features is None or is a mapping."
184 )
185 if isinstance(features, str):
186 if FeatStructReader._START_FDICT_RE.match(features):
187 return FeatDict.__new__(FeatDict, features, **morefeatures)
188 else:
189 return FeatList.__new__(FeatList, features, **morefeatures)
190 elif _is_sequence(features):
191 return FeatList.__new__(FeatList, features)
192 else:
193 raise TypeError("Expected string or mapping or sequence")
194
195 # Otherwise, construct the object as normal.
196 else:
197 return super().__new__(cls, features, **morefeatures)
198
199 ##////////////////////////////////////////////////////////////
200 # { Uniform Accessor Methods

Callers 2

__new__Method · 0.45
__new__Method · 0.45

Calls 3

_is_mappingFunction · 0.85
_is_sequenceFunction · 0.85
matchMethod · 0.45

Tested by

no test coverage detected