JL i$,ddlmZmZGddeZy))ABCMetaabstractmethodceZdZdZdZdZddZdZedZ dZ eddZ d Z d Z d Zd Zd ZdZdZdZeedZy)Featurea An abstract base class for Features. A Feature is a combination of a specific property-computing method and a list of relative positions to apply that method to. The property-computing method, M{extract_property(tokens, index)}, must be implemented by every subclass. It extracts or computes a specific property for the token at the current index. Typical extract_property() methods return features such as the token text or tag; but more involved methods may consider the entire sequence M{tokens} and for instance compute the length of the sentence the token belongs to. In addition, the subclass may have a PROPERTY_NAME, which is how it will be printed (in Rules and Templates, etc). If not given, defaults to the classname. znltk.tbl.FeatureNc d|_|1tt|Dchc] }t|c}|_n) ||kDrttt ||dz|_|jjxs|jj|_ ycc}w#t$r!}t dj|||d}~wwxYw)al Construct a Feature which may apply at C{positions}. >>> # For instance, importing some concrete subclasses (Feature is abstract) >>> from nltk.tag.brill import Word, Pos >>> # Feature Word, applying at one of [-2, -1] >>> Word([-2,-1]) Word([-2, -1]) >>> # Positions need not be contiguous >>> Word([-2,-1, 1]) Word([-2, -1, 1]) >>> # Contiguous ranges can alternatively be specified giving the >>> # two endpoints (inclusive) >>> Pos(-3, -1) Pos([-3, -2, -1]) >>> # In two-arg form, start <= end is enforced >>> Pos(2, 1) Traceback (most recent call last): File "", line 1, in File "nltk/tbl/template.py", line 306, in __init__ raise TypeError ValueError: illegal interval specification: (start=2, end=1) :type positions: list of int :param positions: the positions at which this features should apply :raises ValueError: illegal position specifications An alternative calling convention, for contiguous positions only, is Feature(start, end): :type start: int :param start: start of range where this feature should apply :type end: int :param end: end of range (NOTE: inclusive!) where this feature should apply Nz2illegal interval specification: (start={}, end={})) positionstuplesortedint TypeErrorrange ValueErrorformat __class__ PROPERTY_NAME__name__)selfr endies V/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/tbl/feature.py__init__zFeature.__init__#sP ;"69*Ea3q6*E#FGDN s?#O!&uYa'@!A"^^99TT^^=T=T+F   HOO!3  sB (B C&CCc|jSN)r rs rencode_json_objzFeature.encode_json_obj^s ~~c|}||Sr)clsobjr s rdecode_json_objzFeature.decode_json_objas 9~rc`|jjdt|jdS)N())rrlistr rs r__repr__zFeature.__repr__fs*..))*!D,@+C1EErctd|Dstd|fd|D}|Dcgc]}|rd|vr ||c}Scc}w)a Return a list of features, one for each start point in starts and for each window length in winlen. If excludezero is True, no Features containing 0 in its positions will be generated (many tbl trainers have a special representation for the target feature at [0]) For instance, importing a concrete subclass (Feature is abstract) >>> from nltk.tag.brill import Word First argument gives the possible start positions, second the possible window lengths >>> Word.expand([-3,-2,-1], [1]) [Word([-3]), Word([-2]), Word([-1])] >>> Word.expand([-2,-1], [1]) [Word([-2]), Word([-1])] >>> Word.expand([-3,-2,-1], [1,2]) [Word([-3]), Word([-2]), Word([-1]), Word([-3, -2]), Word([-2, -1])] >>> Word.expand([-2,-1], [1]) [Word([-2]), Word([-1])] A third optional argument excludes all Features whose positions contain zero >>> Word.expand([-2,-1,0], [1,2], excludezero=False) [Word([-2]), Word([-1]), Word([0]), Word([-2, -1]), Word([-1, 0])] >>> Word.expand([-2,-1,0], [1,2], excludezero=True) [Word([-2]), Word([-1]), Word([-2, -1])] All window lengths must be positive >>> Word.expand([-2,-1], [0]) Traceback (most recent call last): File "", line 1, in File "nltk/tag/tbl/template.py", line 371, in expand :param starts: where to start looking for Feature ValueError: non-positive window length in [0] :param starts: where to start looking for Feature :type starts: list of ints :param winlens: window lengths where to look for Feature :type starts: list of ints :param excludezero: do not output any Feature with 0 in any of its positions. :type excludezero: bool :returns: list of Features :raises ValueError: for non-positive window lengths c3&K|] }|dkD yw)rNr ).0xs r z!Feature.expand..s*Q1q5*sznon-positive window length in c3lK|]+}tt|z dzD] }|||z-yw)rN)rlen)r+wrstartss rr-z!Feature.expand..s< UA%F aRS@S:T UQfQQ U Us14r)allr)r!r1winlens excludezeroxsr,s ` rexpandzFeature.expandisQl*'**=gYGH H U U "C1;16ACCCs A  A c|j|juxr+t|jt|jk\S)aQ Return True if this Feature always returns True when other does More precisely, return True if this feature refers to the same property as other; and this Feature looks at all positions that other does (and possibly other positions in addition). #For instance, importing a concrete subclass (Feature is abstract) >>> from nltk.tag.brill import Word, Pos >>> Word([-3,-2,-1]).issuperset(Word([-3,-2])) True >>> Word([-3,-2,-1]).issuperset(Word([-3,-2, 0])) False #Feature subclasses must agree >>> Word([-3,-2,-1]).issuperset(Pos([-3,-2])) False :param other: feature with which to compare :type other: (subclass of) Feature :return: True if this feature is superset, otherwise False :rtype: bool )rsetr rothers r issupersetzFeature.issupersets=8~~0 S5HC OOM 6  rct|j|juxr+t|jt|jzS)a Return True if the positions of this Feature intersects with those of other More precisely, return True if this feature refers to the same property as other; and there is some overlap in the positions they look at. #For instance, importing a concrete subclass (Feature is abstract) >>> from nltk.tag.brill import Word, Pos >>> Word([-3,-2,-1]).intersects(Word([-3,-2])) True >>> Word([-3,-2,-1]).intersects(Word([-3,-2, 0])) True >>> Word([-3,-2,-1]).intersects(Word([0])) False #Feature subclasses must agree >>> Word([-3,-2,-1]).intersects(Pos([-3,-2])) False :param other: feature with which to compare :type other: (subclass of) Feature :return: True if feature classes agree and there is some overlap in the positions they look at :rtype: bool )boolrr8r r9s r intersectszFeature.intersectss@: NNeoo - ;DNN#c%//&::  rch|j|juxr|j|jk(Sr)rr r9s r__eq__zFeature.__eq__s'~~0VT^^u5VVrc|jj|jjkxs|j|jkSr)rrr r9s r__lt__zFeature.__lt__s: NN # #eoo&>&> > - NNU__ ,  rc||k( Srr r9s r__ne__zFeature.__ne__sEM""rc ||kSrr r9s r__gt__zFeature.__gt__s t|rc||k Srr r9s r__ge__zFeature.__ge__s%<rc||kxs||k(Srr r9s r__le__zFeature.__le__se|,tu},rcy)a@ Any subclass of Feature must define static method extract_property(tokens, index) :param tokens: the sequence of tokens :type tokens: list of tokens :param index: the current index :type index: int :return: feature value :rtype: any (but usually scalar) Nr )tokensindexs rextract_propertyzFeature.extract_propertysrr)F)r __module__ __qualname____doc__json_tagrrr classmethodr#r(r6r;r>r@rBrDrFrHrJ staticmethodrrNr rrrr s$"HM9UvF8D8Dt @  HW # -    rr) metaclassN)abcrrrr rrrWs(~ ~ r