
    gY                        d dl Z d dlmZ d dlmZmZ d dlmZmZ  G d de	      Z
e G d d             Z G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de	      Zy)    N)total_ordering)chainislice)raise_unorderable_typesslice_boundsc                   f    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 ZddZd ZddZd Zd Zy)OrderedDictNc                     | j                  ||j                  d            | _        |j                  d      | _        |t        j                  |        y t        j                  | |       y )Nkeysdefault_factory)r   get_keys_default_factorydict__init__)selfdatakwargss      E/var/www/openai/venv/lib/python3.12/site-packages/nltk/collections.pyr   zOrderedDict.__init__   sN    YYtVZZ%78
 &

+< =<MM$MM$%    c                 f    t         j                  | |       | j                  j                  |       y N)r   __delitem__r   remover   keys     r   r   zOrderedDict.__delitem__   s$    s#

#r   c                 p    	 t         j                  | |      S # t        $ r | j                  |      cY S w xY wr   )r   __getitem__KeyError__missing__r   s     r   r   zOrderedDict.__getitem__    s:    	)##D#.. 	)##C((	)s    55c                 0    d | j                         D        S )Nc              3       K   | ]  }|  y wr    ).0r   s     r   	<genexpr>z'OrderedDict.__iter__.<locals>.<genexpr>'   s     +{{s   )r   r   s    r   __iter__zOrderedDict.__iter__&   s    +tyy{++r   c                 j    | j                   s|| j                  vr
t               | j                         S r   )r   r   r   r   s     r   r    zOrderedDict.__missing__)   s-    $$DJJ)>*$$&&r   c                     t         j                  | ||       || j                  vr| j                  j                  |       y y r   )r   __setitem__r   append)r   r   items      r   r*   zOrderedDict.__setitem__.   s7    sD)djj JJc" !r   c                 b    t         j                  |        | j                  j                          y r   )r   clearr   r&   s    r   r.   zOrderedDict.clear3   s    

4

r   c                 R    t         j                  |       }| j                  |_        |S r   )r   copyr   )r   ds     r   r0   zOrderedDict.copy7   s    IIdO**r   c                 R    t        | j                         | j                               S r   )zipr   valuesr&   s    r   itemszOrderedDict.items<   s    499;..r   c                    |r|r-t        |t              sJ t        |      t        |      k(  sJ |S t        |t              s"t        |t              st        |t              sJ t        |t              st        |t              r|j                         S t        |t              r|D cg c]  \  }}|	 c}}S y d| j                  v r| j                  S g S c c}}w )Nr   )
isinstancelistlenr   r	   r   __dict__r   )r   r   r   r   values        r   r   zOrderedDict.keys?   s    !$---4yCI--- tT*!$4!$-. dD)Zk-J99;&d+489DLS%CD99 ,%::I	 :s   )Cc                 z    | j                   s
t               | j                   j                         }| |   }| |= ||fS r   )r   r   pop)r   r   r;   s      r   popitemzOrderedDict.popitemT   s;    zz*jjnnS	IU|r   c                     t         j                  | ||       || j                  vr| j                  j                  |       y y r   )r   
setdefaultr   r+   )r   r   failobjs      r   r@   zOrderedDict.setdefault]   s5    c7+djj JJc" !r   c                     t         j                  | |       | j                  |      D ],  }|| j                  vs| j                  j	                  |       . y r   )r   updater   r   r+   )r   r   r   s      r   rC   zOrderedDict.updateb   s@    D$99T?C$**$

!!#& #r   c                 B    t        | j                  | j                        S r   )mapr   r   r&   s    r   r4   zOrderedDict.valuesh   s    488TZZ((r   r   NN)__name__
__module____qualname__r   r   r   r'   r    r*   r.   r0   r5   r   r>   r@   rC   r4   r#   r   r   r	   r	      sH    &),'
#

/*#
')r   r	   c                   v    e Zd ZdZd Zd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd ZdZd Zd Zd Zd Zd Zy)AbstractLazySequenceaG  
    An abstract base class for read-only sequences whose values are
    computed as needed.  Lazy sequences act like tuples -- they can be
    indexed, sliced, and iterated over; but they may not be modified.

    The most common application of lazy sequences in NLTK is for
    corpus view objects, which provide access to the contents of a
    corpus without loading the entire corpus into memory, by loading
    pieces of the corpus from disk as needed.

    The result of modifying a mutable element of a lazy sequence is
    undefined.  In particular, the modifications made to the element
    may or may not persist, depending on whether and when the lazy
    sequence caches that element's value or reconstructs it from
    scratch.

    Subclasses are required to define two methods: ``__len__()``
    and ``iterate_from()``.
    c                     t        d      )ze
        Return the number of tokens in the corpus file underlying this
        corpus view.
        !should be implemented by subclassNotImplementedErrorr&   s    r   __len__zAbstractLazySequence.__len__   s    
 ""EFFr   c                     t        d      )z
        Return an iterator that generates the tokens in the corpus
        file underlying this corpus view, starting at the token number
        ``start``.  If ``start>=len(self)``, then this iterator will
        generate no tokens.
        rM   rN   r   starts     r   iterate_fromz!AbstractLazySequence.iterate_from   s     ""EFFr   c                    t        |t              rt        | |      \  }}t        | ||      S |dk  r|t	        |       z  }|dk  rt        d      	 t        | j                  |            S # t        $ r}t        d      |d}~ww xY w)z
        Return the *i* th token in the corpus file underlying this
        corpus view.  Negative indices and spans are both supported.
        r   index out of rangeN)	r7   slicer   LazySubsequencer9   
IndexErrornextrT   StopIteration)r   irS   stopes        r   r   z AbstractLazySequence.__getitem__   s    
 a&tQ/KE4"455 1uSY1u !566>D--a011  > !56A=>s   A+ +	B4B  Bc                 $    | j                  d      S )zdReturn an iterator that generates the tokens in the corpus
        file underlying this corpus view.r   )rT   r&   s    r   r'   zAbstractLazySequence.__iter__   s       ##r   c                 ,    t        fd| D              S )z8Return the number of times this list contains ``value``.c              3   .   K   | ]  }|k(  s	d   yw)   Nr#   )r$   eltr;   s     r   r%   z-AbstractLazySequence.count.<locals>.<genexpr>   s     5u1s   
)sumr   r;   s    `r   countzAbstractLazySequence.count   s    5555r   Nc                     t        | t        ||            \  }}t        t        | ||            D ]  \  }}||k(  s||z   c S  t	        d      )a  Return the index of the first occurrence of ``value`` in this
        list that is greater than or equal to ``start`` and less than
        ``stop``.  Negative start and stop values are treated like negative
        slice bounds -- i.e., they count from the end of the list.zindex(x): x not in list)r   rW   	enumerater   
ValueError)r   r;   rS   r]   r\   rc   s         r   indexzAbstractLazySequence.index   sV    
 #4ud);<ttUD 9:FAse|5y  ; 233r   c                 6    t        | j                  |            S )z,Return true if this list contains ``value``.)boolrf   re   s     r   __contains__z!AbstractLazySequence.__contains__   s    DJJu%&&r   c                     t        | |g      S z,Return a list concatenating self with other.LazyConcatenationr   others     r   __add__zAbstractLazySequence.__add__   s     $//r   c                     t        || g      S z,Return a list concatenating other with self.rp   rr   s     r   __radd__zAbstractLazySequence.__radd__   s     %//r   c                      t        | g|z        S z=Return a list concatenating self with itself ``count`` times.rp   r   rf   s     r   __mul__zAbstractLazySequence.__mul__        $%00r   c                      t        | g|z        S ry   rp   rz   s     r   __rmul__zAbstractLazySequence.__rmul__   r|   r   <   c                 
   g }d}| D ]g  }|j                  t        |             |t        |d         dz   z  }|| j                  kD  sAt        |      dkD  sPddj	                  |dd       z  c S  ddj	                  |      z  S )z
        Return a string representation for this corpus view that is
        similar to a list's representation; but if it would be more
        than 60 characters long, it is truncated.
              z	[%s, ...]z, Nz[%s])r+   reprr9   _MAX_REPR_SIZEjoin)r   pieceslengthrc   s       r   __repr__zAbstractLazySequence.__repr__   s     CMM$s)$c&*o))F+++Fa"TYYvcr{%;;;	 
 		&)))r   c                 b    t        |       t        |      k(  xr t        |       t        |      k(  S r   )typer8   rr   s     r   __eq__zAbstractLazySequence.__eq__   s'    DzT%[(FT$Z4;-FFr   c                     | |k(   S r   r#   rr   s     r   __ne__zAbstractLazySequence.__ne__   s    5=  r   c                 x    t        |      t        |       k7  rt        d| |       t        |       t        |      k  S )N<)r   r   r8   rr   s     r   __lt__zAbstractLazySequence.__lt__   s1    ;$t*$#Cu5DzDK''r   c                 F    t        d| j                  j                  z        )zH
        :raise ValueError: Corpus view objects are unhashable.
        z%s objects are unhashable)ri   	__class__rG   r&   s    r   __hash__zAbstractLazySequence.__hash__   s     4t~~7N7NNOOr   rF   )rG   rH   rI   __doc__rP   rT   r   r'   rf   rj   rm   rt   rw   r{   r~   r   r   r   r   r   r   r#   r   r   rK   rK   q   sb    (GG>($
6	4'0011 N*G!(
Pr   rK   c                   .    e Zd ZdZdZ	 d Zd Zd Zd Zy)rX   z
    A subsequence produced by slicing a lazy sequence.  This slice
    keeps a reference to its source sequence, and generates its values
    by looking them up in the source sequence.
    d   c                     ||z
  | j                   k  r't        t        |j                  |      ||z
              S t        j                  |       S )a  
        Construct a new slice from a given underlying sequence.  The
        ``start`` and ``stop`` indices should be absolute indices --
        i.e., they should not be negative (for indexing from the back
        of a list) or greater than the length of ``source``.
        )MIN_SIZEr8   r   rT   object__new__)clssourcerS   r]   s       r   r   zLazySubsequence.__new__  sD     %<#,,&v22594%<HII>>#&&r   c                 .    || _         || _        || _        y r   )_source_start_stop)r   r   rS   r]   s       r   r   zLazySubsequence.__init__  s    
r   c                 4    | j                   | j                  z
  S r   )r   r   r&   s    r   rP   zLazySubsequence.__len__  s    zzDKK''r   c           	          t        | j                  j                  || j                  z         t	        dt        |       |z
              S Nr   )r   r   rT   r   maxr9   rR   s     r   rT   zLazySubsequence.iterate_from  s<    LL%%edkk&9:C3t9uCT<U
 	
r   N)	rG   rH   rI   r   r   r   r   rP   rT   r#   r   r   rX   rX      s'     H'
(
r   rX   c                   "    e Zd ZdZd Zd Zd Zy)rq   a%  
    A lazy sequence formed by concatenating a list of lists.  This
    underlying list of lists may itself be lazy.  ``LazyConcatenation``
    maintains an index that it uses to keep track of the relationship
    between offsets in the concatenated lists and offsets in the
    sublists.
    c                 "    || _         dg| _        y r   )_list_offsets)r   list_of_listss     r   r   zLazyConcatenation.__init__(  s    "
r   c                     t        | j                        t        | j                        k  r#| j                  | j                  d         D ]  } | j                  d   S )Nr   )r9   r   r   rT   r   _s     r   rP   zLazyConcatenation.__len__,  sK    t}}TZZ0&&t}}R'89 :}}R  r   c              #     K   || j                   d   k  r$t        j                  | j                   |      dz
  }nt        | j                         dz
  }| j                   |   }t	        | j
                  t              r| j
                  j                  |      }nt        | j
                  |d       }|D ]  }|t        | j                         dz
  k(  rM|t        |      z   | j                   d   k\  sJ d       | j                   j                  |t        |      z          n(| j                   |dz      |t        |      z   k(  sJ d       |t        d||z
        d  E d {    |t        |      z  }|dz  } y 7 w)Nr   rb   z!offsets not monotonic increasing!z"inconsistent list value (num elts)r   )r   bisectbisect_rightr9   r7   r   rK   rT   r   r+   r   )r   start_indexsublist_indexrj   sublist_itersublists         r   rT   zLazyConcatenation.iterate_from2  se    r**"//{KaOM.2Mm, djj"67::22=AL!$**mTBL#GT]]!3a!78CL(DMM",==767=$$US\%9:}}]Q%6753D <  878  s1kE&9:<===S\!EQM $ >s   EE4E2E4N)rG   rH   rI   r   r   rP   rT   r#   r   r   rq   rq     s    !r   rq   c                   (    e Zd ZdZd Zd Zd Zd Zy)LazyMapa  
    A lazy sequence whose elements are formed by applying a given
    function to each element in one or more underlying lists.  The
    function is applied lazily -- i.e., when you read a value from the
    list, ``LazyMap`` will calculate that value by applying its
    function to the underlying lists' value(s).  ``LazyMap`` is
    essentially a lazy version of the Python primitive function
    ``map``.  In particular, the following two expressions are
    equivalent:

        >>> from nltk.collections import LazyMap
        >>> function = str
        >>> sequence = [1,2,3]
        >>> map(function, sequence) # doctest: +SKIP
        ['1', '2', '3']
        >>> list(LazyMap(function, sequence))
        ['1', '2', '3']

    Like the Python ``map`` primitive, if the source lists do not have
    equal size, then the value None will be supplied for the
    'missing' elements.

    Lazy maps can be useful for conserving memory, in cases where
    individual values take up a lot of space.  This is especially true
    if the underlying list's values are constructed lazily, as is the
    case with many corpus readers.

    A typical example of a use case for this class is performing
    feature detection on the tokens in a corpus.  Since featuresets
    are encoded as dictionaries, which can take up a lot of memory,
    using a ``LazyMap`` can significantly reduce memory usage when
    training and running classifiers.
    c                     |st        d      || _        || _        |j                  dd      | _        | j                  dkD  ri nd| _        t        d |D              t        |      k(  | _        y)aJ  
        :param function: The function that should be applied to
            elements of ``lists``.  It should take as many arguments
            as there are ``lists``.
        :param lists: The underlying lists.
        :param cache_size: Determines the size of the cache used
            by this lazy map.  (default=5)
        z"LazyMap requires at least two args
cache_sizer   r   Nc              3   <   K   | ]  }t        |t                y wr   )r7   rK   r$   lsts     r   r%   z#LazyMap.__init__.<locals>.<genexpr>  s      
=BcJs01Us   )		TypeError_lists_funcr   _cache_size_cacherd   r9   	_all_lazy)r   functionlistsconfigs       r   r   zLazyMap.__init__t  sq     @AA
!::lA6 ,,q0bd
  
=B
 
Zr   c              #   "  K   t        | j                        dk(  rC| j                  r7| j                  d   j                  |      D ]  }| j	                  |        y t        | j                        dk(  r+	 	 | j	                  | j                  d   |          |dz  }*| j                  r| j                  D cg c]  }|j                  |       }}	 g }|D ]  }	 |j                  t        |              |d gt        | j                        z  k(  ry  | j                  |  |dz  }X	 	 | j                  D cg c]  }||   	 }} | j                  |  |dz  }4# t
        $ r Y y w xY wc c}w #  |j                  d        Y xY wc c}w # t
        $ ro d gt        | j                        z  }t        | j                        D ]  \  }}	 ||   ||<   # t
        $ r Y w xY w |d gt        | j                        z  k(  rY y Y w xY ww)Nrb   r   )	r9   r   r   rT   r   rY   r+   rZ   rh   )r   rj   r;   r   	iteratorselementsiteratorr\   s           r   rT   zLazyMap.iterate_from  s    t{{q T^^Q44U;jj'' < "**T[[^E%:;; 
  ^^<@KKHKS))%0KIH )H. X7 !*
 vDKK(888 djj(++
  
6:kkBksE
kHB !djj(++
 + "  I. -  C!  $vDKK(88H"+DKK"83!*-e*HQK) ! ! #9
  D6C,<#<< =s   A5H8#E#  H;E2
HE787H0F >F
F H#	E/,H.E//H7F
HF =HGH	G'$H&G'' HHHHc                 J   t        |t              r2| j                  D cg c]  }||   	 }}t        | j                  g| S |dk  r|t        |       z  }|dk  rt        d      | j                  || j                  v r| j                  |   S 	 t        | j                  |            }| j                  Kt        | j                        | j                  kD  r| j                  j                          || j                  |<   |S c c}w # t        $ r}t        d      |d }~ww xY w)Nr   rV   )r7   rW   r   r   r   r9   rY   r   rZ   rT   r[   r   r>   )r   rj   r   sliced_listsvalr^   s         r   r   zLazyMap.__getitem__  s   eU#26++>+3CJ+L>4::555 qyT"qy !566{{&5DKK+?{{5))>4,,U34 {{&t{{#d&6&66KK'')%(E"J- ? ! > !56A=>s   DD 	D"DD"c                 :    t        d | j                  D              S )Nc              3   2   K   | ]  }t        |        y wr   r9   r   s     r   r%   z"LazyMap.__len__.<locals>.<genexpr>       3{3s8{   )r   r   r&   s    r   rP   zLazyMap.__len__      3t{{333r   N)rG   rH   rI   r   r   rT   r   rP   r#   r   r   r   r   Q  s     D0.`44r   r   c                   "    e Zd ZdZd Zd Zd Zy)LazyZipa  
    A lazy sequence whose elements are tuples, each containing the i-th
    element from each of the argument sequences.  The returned list is
    truncated in length to the length of the shortest argument sequence. The
    tuples are constructed lazily -- i.e., when you read a value from the
    list, ``LazyZip`` will calculate that value by forming a tuple from
    the i-th element of each of the argument sequences.

    ``LazyZip`` is essentially a lazy version of the Python primitive function
    ``zip``.  In particular, an evaluated LazyZip is equivalent to a zip:

        >>> from nltk.collections import LazyZip
        >>> sequence1, sequence2 = [1, 2, 3], ['a', 'b', 'c']
        >>> zip(sequence1, sequence2) # doctest: +SKIP
        [(1, 'a'), (2, 'b'), (3, 'c')]
        >>> list(LazyZip(sequence1, sequence2))
        [(1, 'a'), (2, 'b'), (3, 'c')]
        >>> sequences = [sequence1, sequence2, [6,7,8,9]]
        >>> list(zip(*sequences)) == list(LazyZip(*sequences))
        True

    Lazy zips can be useful for conserving memory in cases where the argument
    sequences are particularly long.

    A typical example of a use case for this class is combining long sequences
    of gold standard and predicted values in a classification or tagging task
    in order to calculate accuracy.  By constructing tuples lazily and
    avoiding the creation of an additional long sequence, memory usage can be
    significantly reduced.
    c                 4    t        j                  | d g|  y)zT
        :param lists: the underlying lists
        :type lists: list(list)
        c                      | S r   r#   )eltss    r   <lambda>z"LazyZip.__init__.<locals>.<lambda>  s    Tr   N)r   r   )r   r   s     r   r   zLazyZip.__init__  s    
 	1:E:r   c              #      K   t         j                  | |      }|t        |       k  r!t        |       |dz  }|t        |       k  r!y w)Nrb   )r   rT   r9   rZ   )r   rj   r   s      r   rT   zLazyZip.iterate_from  sJ     ''e4c$ix. QJE c$i 	s   AA	A	c                 :    t        d | j                  D              S )Nc              3   2   K   | ]  }t        |        y wr   r   r   s     r   r%   z"LazyZip.__len__.<locals>.<genexpr>	  r   r   )minr   r&   s    r   rP   zLazyZip.__len__  r   r   N)rG   rH   rI   r   r   rT   rP   r#   r   r   r   r     s    >;4r   r   c                       e Zd ZdZd Zy)LazyEnumeratea  
    A lazy sequence whose elements are tuples, each containing a count (from
    zero) and a value yielded by underlying sequence.  ``LazyEnumerate`` is
    useful for obtaining an indexed list. The tuples are constructed lazily
    -- i.e., when you read a value from the list, ``LazyEnumerate`` will
    calculate that value by forming a tuple from the count of the i-th
    element and the i-th element of the underlying sequence.

    ``LazyEnumerate`` is essentially a lazy version of the Python primitive
    function ``enumerate``.  In particular, the following two expressions are
    equivalent:

        >>> from nltk.collections import LazyEnumerate
        >>> sequence = ['first', 'second', 'third']
        >>> list(enumerate(sequence))
        [(0, 'first'), (1, 'second'), (2, 'third')]
        >>> list(LazyEnumerate(sequence))
        [(0, 'first'), (1, 'second'), (2, 'third')]

    Lazy enumerations can be useful for conserving memory in cases where the
    argument sequences are particularly long.

    A typical example of a use case for this class is obtaining an indexed
    list for a long sequence of values.  By constructing tuples lazily and
    avoiding the creation of an additional long sequence, memory usage can be
    significantly reduced.
    c                 V    t         j                  | t        t        |            |       y)zI
        :param lst: the underlying list
        :type lst: list
        N)r   r   ranger9   )r   r   s     r   r   zLazyEnumerate.__init__)  s    
 	uSX4r   N)rG   rH   rI   r   r   r#   r   r   r   r     s    85r   r   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	LazyIteratorListz
    Wraps an iterator, loading its elements on demand
    and making them subscriptable.
    __repr__ displays only the first few elements.
    Nc                 .    || _         || _        g | _        y r   )_it_lenr   )r   it	known_lens      r   r   zLazyIteratorList.__init__8  s    	r   c                     | j                   r| j                   S | j                  t        | j                              D ]  } t        | j                        | _         | j                   S r   )r   rT   r9   r   r   s     r   rP   zLazyIteratorList.__len__=  sN    9999""3t{{#34A 5$	yyr   c              #     K   t        | j                        |k  rIt        | j                        }| j                  j	                  |       t        | j                        |k  rI|}|t        | j                        k  r/| j                  |    |dz  }|t        | j                        k  r/	 	 t        | j                        }| j                  j	                  |       | 5# t
        $ r Y yw xY ww)zBCreate a new iterator over this list starting at the given offset.rb   N)r9   r   rZ   r   r+   r[   )r   rS   vr\   s       r   rT   zLazyIteratorList.iterate_fromE  s     $++&TXXAKKq! $++& #dkk""++a. FA #dkk""	N""1%   		s+   A!C3$AC3.6C$ $	C0-C3/C00C3c                 8     t        |       t        | |            S ro   r   r   rr   s     r   rt   zLazyIteratorList.__add__V  s    tDz%e,--r   c                 8     t        |       t        ||             S rv   r   rr   s     r   rw   zLazyIteratorList.__radd__Z  s    tDz%t,--r   r   )	rG   rH   rI   r   r   rP   rT   rt   rw   r#   r   r   r   r   1  s     
"..r   r   c                   4     e Zd ZdZdZd fd	Zd Zd Z xZS )Triez!A Trie implementation for stringsTc                 X    t         |           |r|D ]  }| j                  |        yy)ay  Builds a Trie object, which is built around a ``dict``

        If ``strings`` is provided, it will add the ``strings``, which
        consist of a ``list`` of ``strings``, to the Trie.
        Otherwise, it'll construct an empty Trie.

        :param strings: List of strings to insert into the trie
            (Default is ``None``)
        :type strings: list(str)

        N)superr   insert)r   stringsstringr   s      r   r   zTrie.__init__g  s.     	!F# " r   c                 v    t        |      r| |d      j                  |dd        yd| t        j                  <   y)a  Inserts ``string`` into the Trie

        :param string: String to insert into the trie
        :type string: str

        :Example:

        >>> from nltk.collections import Trie
        >>> trie = Trie(["abc", "def"])
        >>> expected = {'a': {'b': {'c': {True: None}}},                         'd': {'e': {'f': {True: None}}}}
        >>> trie == expected
        True

        r   rb   N)r9   r   r   LEAF)r   r   s     r   r   zTrie.insertx  s6      v;O""6!":. #DOr   c                 &    t               | |<   | |   S r   )r   r   s     r   r    zTrie.__missing__  s    FS	Cyr   r   )	rG   rH   rI   r   r   r   r   r    __classcell__)r   s   @r   r   r   b  s    +D$"#,r   r   )r   	functoolsr   	itertoolsr   r   nltk.internalsr   r   r   r	   rK   rX   rq   r   r   r   r   r   r#   r   r   <module>r      s     $ # @V)$ V)| AP AP APH&
* &
R/, /dF4" F4R/4g /4d"5G "5J+.+ +.b.4 .r   