a
     ˜ëjÏ$  ã                   @   s&   d dl mZmZ G dd„ ded�ZdS )é    )ÚABCMetaÚabstractmethodc                   @   sœ   e Zd ZdZdZdZd!dd„Zdd„ Zedd	„ ƒZ	d
d„ Z
ed"dd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeedd „ ƒƒZdS )#Ú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.

    znltk.tbl.FeatureNc              
   C   sš   d| _ |du r(ttdd„ |D ƒƒƒ| _ n\z$||kr6t‚tt||d ƒƒ| _ W n6 ty‚ } ztd ||¡ƒ|‚W Y d}~n
d}~0 0 | jjp’| jj	| _dS )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 "<stdin>", line 1, in <module>
          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
        Nc                 S   s   h | ]}t |ƒ’qS © )Úint)Ú.0Úir   r   úO/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/tbl/feature.pyÚ	<setcomp>M   ó    z#Feature.__init__.<locals>.<setcomp>é   z2illegal interval specification: (start={}, end={}))
Ú	positionsÚtupleÚsortedÚ	TypeErrorÚrangeÚ
ValueErrorÚformatÚ	__class__ÚPROPERTY_NAMEÚ__name__)Úselfr   ÚendÚer   r   r	   Ú__init__#   s     (ÿÿüzFeature.__init__c                 C   s   | j S ©N)r   ©r   r   r   r	   Úencode_json_obj^   s    zFeature.encode_json_objc                 C   s   |}| |ƒS r   r   )ÚclsÚobjr   r   r   r	   Údecode_json_obja   s    zFeature.decode_json_objc                 C   s   | j j› dt| jƒ›d�S )Nú(ú))r   r   Úlistr   r   r   r   r	   Ú__repr__f   s    zFeature.__repr__Fc                    sF   t dd„ |D ƒƒs td|› �ƒ‚‡fdd„|D ƒ}‡ ‡fdd„|D ƒS )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 "<stdin>", line 1, in <module>
          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
        c                 s   s   | ]}|d kV  qdS )r   Nr   ©r   Úxr   r   r	   Ú	<genexpr>Ÿ   r   z!Feature.expand.<locals>.<genexpr>znon-positive window length in c                 3   s8   | ]0}t tˆ ƒ| d  ƒD ]}ˆ ||| … V  qqdS )r   N)r   Úlen)r   Úwr   )Ústartsr   r	   r'   ¡   r   c                    s    g | ]}ˆrd |v sˆ |ƒ‘qS )r   r   r%   )r   Úexcludezeror   r	   Ú
<listcomp>¢   r   z"Feature.expand.<locals>.<listcomp>)Úallr   )r   r*   Zwinlensr+   Úxsr   )r   r+   r*   r	   Úexpandi   s    6zFeature.expandc                 C   s    | j |j u ot| jƒt|jƒk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


        )r   Úsetr   ©r   Úotherr   r   r	   Ú
issuperset¤   s    ÿzFeature.issupersetc                 C   s$   t | j|ju o t| jƒt|jƒ@ ƒS )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
        )Úboolr   r0   r   r1   r   r   r	   Ú
intersectsÄ   s    þzFeature.intersectsc                 C   s   | j |j u o| j|jkS r   )r   r   r1   r   r   r	   Ú__eq__è   s    zFeature.__eq__c                 C   s   | j j|j jk p| j|jk S r   )r   r   r   r1   r   r   r	   Ú__lt__ë   s    
üzFeature.__lt__c                 C   s
   | |k S r   r   r1   r   r   r	   Ú__ne__ó   s    zFeature.__ne__c                 C   s   || k S r   r   r1   r   r   r	   Ú__gt__ö   s    zFeature.__gt__c                 C   s
   | |k  S r   r   r1   r   r   r	   Ú__ge__ù   s    zFeature.__ge__c                 C   s   | |k p| |kS r   r   r1   r   r   r	   Ú__le__ü   s    zFeature.__le__c                 C   s   dS )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   )ÚtokensÚindexr   r   r	   Úextract_propertyÿ   s    zFeature.extract_property)N)F)r   Ú
__module__Ú__qualname__Ú__doc__Zjson_tagr   r   r   Úclassmethodr    r$   r/   r3   r5   r6   r7   r8   r9   r:   r;   Ústaticmethodr   r>   r   r   r   r	   r      s*   
;
: $r   )Ú	metaclassN)Úabcr   r   r   r   r   r   r	   Ú<module>
   s   