ó
ù`]c           @  sh   d  d l  m Z m Z m Z d  d l m Z m Z d  d l m Z e e ƒ d e	 f d „  ƒ  Yƒ Z
 d S(   iÿÿÿÿ(   t   divisiont   print_functiont   unicode_literals(   t   ABCMetat   abstractmethod(   t   add_metaclasst   Featurec           B  s¶   e  Z d  Z d Z d Z d d „ Z d „  Z e d „  ƒ Z	 d „  Z
 e e d „ ƒ Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z e e d „  ƒ ƒ Z RS(   u  
    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.

    u   nltk.tbl.Featurec         C  s½   d |  _ | d k r= t t t d „  | Dƒ ƒ ƒ ƒ |  _ na y5 | | k rU t ‚ n  t t | | d ƒ ƒ |  _ Wn) t k
 r t d j | | ƒ ƒ ‚ n X|  j	 j
 p³ |  j	 j |  _
 d S(   uO  
        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

        c         s  s   |  ] } t  | ƒ Vq d  S(   N(   t   int(   t   .0t   i(    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pys	   <genexpr>R   s    i   u4   illegal interval specification: (start={0}, end={1})N(   t   Nonet	   positionst   tuplet   sortedt   sett	   TypeErrort   ranget
   ValueErrort   formatt	   __class__t   PROPERTY_NAMEt   __name__(   t   selfR   t   end(    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __init__'   s    )	(	 c         C  s   |  j  S(   N(   R   (   R   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   encode_json_objc   s    c         C  s   | } |  | ƒ S(   N(    (   t   clst   objR   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   decode_json_objf   s    c         C  s   d |  j  j t |  j ƒ f S(   Nu   %s(%r)(   R   R   t   listR   (   R   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __repr__k   s    c           ss   t  d „  | Dƒ ƒ s. t d j | ƒ ƒ ‚ n  ‡  f d †  | Dƒ } g  | D]$ } | o` d | k sK |  | ƒ ^ qK S(   u¸  
        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(   i    N(    (   R   t   x(    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pys	   <genexpr>    s    u!   non-positive window length in {0}c         3  sA   |  ]7 } t  t ˆ  ƒ | d  ƒ D] } ˆ  | | | !Vq! q d S(   i   N(   R   t   len(   R   t   wR	   (   t   starts(    s/   lib/python2.7/site-packages/nltk/tbl/feature.pys	   <genexpr>¢   s    i    (   t   allR   R   (   R   R"   t   winlenst   excludezerot   xsR   (    (   R"   s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   expandn   s    2c         C  s.   |  j  | j  k o- t |  j ƒ t | j ƒ k S(   uQ  
        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   R   R   (   R   t   other(    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt
   issuperset¥   s    !c         C  s2   t  |  j | j k o. t |  j ƒ t | j ƒ @ƒ S(   u  
        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
        (   t   boolR   R   R   (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt
   intersectsÅ   s    c         C  s"   |  j  | j  k o! |  j | j k S(   N(   R   R   (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __eq__ë   s    c         C  s(   |  j  j | j  j k  p' |  j | j k  S(   N(   R   R   R   (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __lt__î   s    c         C  s   |  | k S(   N(    (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __ne__ö   s    c         C  s
   | |  k  S(   N(    (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __gt__ù   s    c         C  s   |  | k  S(   N(    (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __ge__ü   s    c         C  s   |  | k  p |  | k S(   N(    (   R   R(   (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   __le__ÿ   s    c         C  s   d S(   u@  
        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)
        N(    (   t   tokenst   index(    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   extract_property  t    N(   R   t
   __module__t   __doc__t   json_tagR
   R   R   R   t   classmethodR   R   t   FalseR'   R)   R+   R,   R-   R.   R/   R0   R1   t   staticmethodR   R4   (    (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyR      s&   <		6	 	&						N(   t
   __future__R    R   R   t   abcR   R   t   sixR   t   objectR   (    (    (    s/   lib/python2.7/site-packages/nltk/tbl/feature.pyt   <module>   s   