
    =wgI                         d Z ddlZddlmZ ddlmZmZ ddlm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 e       Z G d de      Z G d de      Zy)a  
This module contains "matcher" classes. Matchers deal with posting lists. The
most basic matcher, which reads the list of postings for a term, will be
provided by the backend implementation (for example,
:class:`whoosh.filedb.filepostings.FilePostingReader`). The classes in this
module provide additional functionality, such as combining the results of two
matchers, or modifying the results of a matcher.

You do not need to deal with the classes in this module unless you need to
write your own Matcher implementation to provide some new functionality. These
classes are not instantiated by the user. They are usually created by a
:class:`~whoosh.query.Query` object's :meth:`~whoosh.query.Query.matcher()`
method, which returns the appropriate matcher to implement the query (for
example, the :class:`~whoosh.query.Or` query's
:meth:`~whoosh.query.Or.matcher()` method returns a
:py:class:`~whoosh.matching.UnionMatcher` object).

Certain backends support "quality" optimizations. These backends have the
ability to skip ahead if it knows the current block of postings can't
contribute to the top N documents. If the matcher tree and backend support
these optimizations, the matcher's :meth:`Matcher.supports_block_quality()`
method will return ``True``.
    N)repeat)izipxrange)abstractmethodc                       e Zd ZdZy)
ReadTooFarzRaised when :meth:`~whoosh.matching.Matcher.next()` or
    :meth:`~whoosh.matching.Matcher.skip_to()` are called on an inactive
    matcher.
    N__name__
__module____qualname____doc__     L/var/www/horilla/myenv/lib/python3.12/site-packages/whoosh/matching/mcore.pyr   r   =   s    r   r   c                       e Zd ZdZy)NoQualityAvailablezoRaised when quality methods are called on a matcher that does not
    support block quality optimizations.
    Nr	   r   r   r   r   r   D   s    r   r   c                   .   e Zd ZdZed        Zed        Zd Zd Zd#dZ	d Z
d	 Zd$d
Zed        Zd Zd Zd Zd Zed        Zd Zd Zd Zed        Zed        Zed        Zd Zd Zd Zed        Zd Zed        Zd Zd Z d Z!d  Z"d! Z#d" Z$y)%Matcherz!Base class for all matchers.
    c                     t         )z}Returns True if this matcher is still "active", that is, it has not
        yet reached the end of the posting list.
        NotImplementedErrorselfs    r   	is_activezMatcher.is_activeP   s
     "!r   c                     t         )a  Returns to the start of the posting list.

        Note that reset() may not do what you expect after you call
        :meth:`Matcher.replace()`, since this can mean calling reset() not on
        the original matcher, but on an optimized replacement.
        r   r   s    r   resetzMatcher.resetX   s
     "!r   c                      y)zReturns a ``("fieldname", "termtext")`` tuple for the term this
        matcher matches, or None if this matcher is not a term matcher.
        Nr   r   s    r   termzMatcher.termc   s    
 r   c              #      K   | j                         |  y| j                         D ]  }|j                         D ]  }|   yw)z;Returns an iterator of term matchers in this tree.
        N)r   childrenterm_matchers)r   cmms      r   r!   zMatcher.term_matchersj   sK      99;"Jmmo ))+ AGs   AANc              #     K   | j                         sy|| j                         }n|| j                         k7  ry| j                         }|0| j                         D ]  }|j	                  |      D ]  }|   y| yw)zReturns an iterator of ``("fieldname", "termtext")`` tuples for the
        **currently matching** term matchers in this tree.
        N)r   idr   r    matching_terms)r   r%   tcs       r   r&   zMatcher.matching_termsu   s     
 ~~:B4779_IIK9]]_ ))"- AG Gs   B Bc                 6    t        | j                                S N)boolr    r   s    r   is_leafzMatcher.is_leaf   s    (((r   c                     g S )zUReturns an (possibly empty) list of the submatchers of this
        matcher.
        r   r   s    r   r    zMatcher.children   s	    
 	r   c                     | S )zReturns a possibly-simplified version of this matcher. For example,
        if one of the children of a UnionMatcher is no longer active, calling
        this method on the UnionMatcher will return the other child.
        r   r   
minqualitys     r   replacezMatcher.replace   s	     r   c                     t         )z(Returns a copy of this matcher.
        r   r   s    r   copyzMatcher.copy   
    
 "!r   c                      y)zsReturns the depth of the tree under this matcher, or 0 if this
        matcher does not have any children.
        r   r   r   s    r   depthzMatcher.depth   s    
 r   c                      y)zdReturns True if this matcher supports the use of ``quality`` and
        ``block_quality``.
        Fr   r   s    r   supports_block_qualityzMatcher.supports_block_quality   s    
 r   c                 ,    t        | j                        )zReturns the maximum possible quality measurement for this matcher,
        according to the current weighting algorithm. Raises
        ``NoQualityAvailable`` if the matcher or weighting do not support
        quality measurements.
        r   	__class__r   s    r   max_qualityzMatcher.max_quality        !00r   c                 ,    t        | j                        )zReturns a quality measurement of the current block of postings,
        according to the current weighting algorithm. Raises
        ``NoQualityAvailable`` if the matcher or weighting do not support
        quality measurements.
        r:   r   s    r   block_qualityzMatcher.block_quality   r=   r   c                     t         )z/Returns the ID of the current posting.
        r   r   s    r   r%   z
Matcher.id   r4   r   c              #      K   d}| }|j                         rP|j                          |j                          |dz  }|dk(  r|j                         }d}|j                         rOyyw)a<  Returns a generator of all IDs in the matcher.

        What this method returns for a matcher that has already read some
        postings (whether it only yields the remaining postings or all postings
        from the beginning) is undefined, so it's best to only use this method
        on fresh matchers.
        r      
   N)r   r%   nextr1   r   ir#   s      r   all_idszMatcher.all_ids   sZ      kkm$$&LFFHFABwIIK kkms   A#A(&A(c              #      K   d}| }| j                         r`|j                         |j                         f |j                          |dz  }|dk(  r|j	                         }d}| j                         r_yyw)aR  Returns a generator of all (ID, encoded value) pairs in the matcher.

        What this method returns for a matcher that has already read some
        postings (whether it only yields the remaining postings or all postings
        from the beginning) is undefined, so it's best to only use this method
        on fresh matchers.
        r   rB   rC   N)r   r%   valuerD   r1   rE   s      r   	all_itemszMatcher.all_items   sh      nn4461779%%FFHFABwIIK nns   A3A86A8c              #      K   | j                         rE| j                         | j                  |      f | j                          | j                         rDyyw)aR  Returns a generator of all (ID, decoded value) pairs in the matcher.

        What this method returns for a matcher that has already read some
        postings (whether it only yields the remaining postings or all postings
        from the beginning) is undefined, so it's best to only use this method
        on fresh matchers.
        N)r   r%   value_asrD   r   astypes     r   items_aszMatcher.items_as   sA      nn779dmmF344IIK nns   AAAc                     t         )z:Returns the encoded value of the current posting.
        r   r   s    r   rI   zMatcher.value   r4   r   c                 2    t        d| j                  z        )zzReturns True if the field's format supports the named data type,
        for example 'frequency' or 'characters'.
        zsupports not implemented in %sr   r;   rM   s     r   supportszMatcher.supports  s      ""B$(NN#3 4 	4r   c                 2    t        d| j                  z        )zGReturns the value(s) of the current posting as the given type.
        zvalue_as not implemented in %srR   rM   s     r   rL   zMatcher.value_as  s     
 ""B$(NN#3 4 	4r   c           
      *   ddl m} | j                  d      r0| j                  d      D cg c]  \  }}} ||||       c}}}S | j                  d      r%| j                  d      D cg c]
  } ||       c}S t	        d      c c}}}w c c}w )zReturns a list of :class:`~whoosh.query.spans.Span` objects for the
        matches in this document. Raises an exception if the field being
        searched does not store positions.
        r   Span
characters	startcharendchar	positionszField does not support spans)whoosh.query.spansrW   rS   rL   	Exceptionr   rW   posrZ   r[   s        r   spanszMatcher.spans  s     	,==&37==3NP P/Y 	7C P P]];')-{)CD#DIDD:;;P Es   B	,Bc                     | j                         rJ| j                         |k  r6| j                          | j                         r| j                         |k  r4yyyy)zjMoves this matcher to the first posting with an ID equal to or
        greater than the given ID.
        N)r   r%   rD   r   r%   s     r   skip_tozMatcher.skip_to%  sC    
 nn4779r>IIK nn4779r>>r   c                 @    t        | j                  j                        )zhMoves this matcher to the next block with greater than the given
        minimum quality value.
        r   r;   r
   r/   s     r   skip_to_qualityzMatcher.skip_to_quality-      
 "$.."9"9::r   c                 @    t        | j                  j                        )z0Moves this matcher to the next posting.
        rf   r   s    r   rD   zMatcher.next4  rh   r   c                 $    | j                  d      S )z3Returns the weight of the current posting.
        weight)rL   r   s    r   rk   zMatcher.weight;  s     }}X&&r   c                 @    t        | j                  j                        )z2Returns the score of the current posting.
        rf   r   s    r   scorezMatcher.scoreA  rh   r   c                 0    | j                   t        |      u S r*   )r;   typer   others     r   __eq__zMatcher.__eq__H  s    ~~e,,r   c                 0    t        |      | j                  u S r*   )ro   r;   rp   s     r   __lt__zMatcher.__lt__K  s    E{dnn,,r   c                 &    | j                  |       S r*   )rr   rp   s     r   __ne__zMatcher.__ne__N  s    ;;u%%%r   c                 L    | j                  |      xs | j                  |       S r*   )rt   rr   rp   s     r   __gt__zMatcher.__gt__Q  s"    KK&<$++e*<==r   c                 J    | j                  |      xs | j                  |      S r*   )rr   rt   rp   s     r   __le__zMatcher.__le__T      {{5!7T[[%77r   c                 J    | j                  |      xs | j                  |      S r*   )rr   rx   rp   s     r   __ge__zMatcher.__ge__W  r{   r   r*   r   )%r
   r   r   r   r   r   r   r   r!   r&   r,   r    r1   r3   r6   r8   r<   r?   r%   rG   rJ   rO   rI   rS   rL   ra   rd   rg   rD   rk   rm   rr   rt   rv   rx   rz   r}   r   r   r   r   r   L   s.    " " " "	*) " "11 " "&& " " 4 4 4 4< ; ; ;' ; ;--&>88r   r   c                   2    e Zd ZddZd Zd Zd Zd Zd Zy)	ConstantScoreMatcherc                     || _         y r*   _score)r   rm   s     r   __init__zConstantScoreMatcher.__init__^  s	    r   c                      yNTr   r   s    r   r8   z+ConstantScoreMatcher.supports_block_qualitya      r   c                     | j                   S r*   r   r   s    r   r<   z ConstantScoreMatcher.max_qualityd      {{r   c                     | j                   S r*   r   r   s    r   r?   z"ConstantScoreMatcher.block_qualityg  r   r   c                 D    || j                   k\  r| j                          y y r*   )r   go_inactiver/   s     r   rg   z$ConstantScoreMatcher.skip_to_qualityj  s    $ %r   c                     | j                   S r*   r   r   s    r   rm   zConstantScoreMatcher.scoren  r   r   N)      ?)	r
   r   r   r   r8   r<   r?   rg   rm   r   r   r   r   r   ]  s     r   r   c                   L    e Z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)NullMatcherClassz4Matcher with no postings which is never active.
    c                     | S r*   r   r   s    r   __call__zNullMatcherClass.__call__x      r   c                      y)Nz<NullMatcher>r   r   s    r   __repr__zNullMatcherClass.__repr__{  s    r   c                      yr   r   r   s    r   r8   z'NullMatcherClass.supports_block_quality~  r   r   c                      yNr   r   r   s    r   r<   zNullMatcherClass.max_quality      r   c                      yr   r   r   s    r   r?   zNullMatcherClass.block_quality  r   r   c                      yr   r   r/   s     r   rg   z NullMatcherClass.skip_to_quality  r   r   c                      y)NFr   r   s    r   r   zNullMatcherClass.is_active  s    r   c                      y r*   r   r   s    r   r   zNullMatcherClass.reset  s    r   c                     g S r*   r   r   s    r   rG   zNullMatcherClass.all_ids  s    	r   c                     | S r*   r   r   s    r   r3   zNullMatcherClass.copy  r   r   N)r
   r   r   r   r   r   r8   r<   r?   rg   r   r   rG   r3   r   r   r   r   r   t  s9    r   r   c                       e Zd ZdZ	 	 	 d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d Zd Zd Zd Zd Zy)ListMatcherz/Synthetic matcher backed by a list of IDs.
    Nc
                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        y)ak  
        :param ids: a list of doc IDs.
        :param weights: a list of weights corresponding to the list of IDs.
            If this argument is not supplied, a list of 1.0 values is used.
        :param values: a list of encoded values corresponding to the list of
            IDs.
        :param format: a :class:`whoosh.formats.Format` object representing the
            format of the field.
        :param scorer: a :class:`whoosh.scoring.BaseScorer` object for scoring
            the postings.
        :param term: a ``("fieldname", "text")`` tuple, or None if this is not
            a term matcher.
        N)	_ids_weights_all_weights_values_i_format_scorer_term	_terminfo)
r   idsweightsvaluesformatscorerpositionall_weightsr   terminfos
             r   r   zListMatcher.__init__  sD    " 	'
!r   c                 4    d| j                   j                  z  S )Nz<%s>)r;   r
   r   s    r   r   zListMatcher.__repr__  s    ////r   c                 F    | j                   t        | j                        k  S r*   )r   lenr   r   s    r   r   zListMatcher.is_active  s    wwTYY''r   c                     d| _         y r   r   r   s    r   r   zListMatcher.reset  s	    r   c                    | j                         st        || j                         k  ry | j                  t	        | j
                        k  rs| j
                  | j                     |k  rV| xj                  dz  c_        | j                  t	        | j
                        k  r| j
                  | j                     |k  rTy y y y NrB   )r   r   r%   r   r   r   rc   s     r   rd   zListMatcher.skip_to  s    ~~	>ggDII&499TWW+=+BGGqLG ggDII&499TWW+=+B&+B&r   c                     | j                   S r*   r   r   s    r   r   zListMatcher.term      zzr   c           	          | j                  | j                  | j                  | j                  | j                  | j
                  | j                  | j                        S r*   )r;   r   r   r   r   r   r   r   r   s    r   r3   zListMatcher.copy  sA    ~~dii"llDLL$''"//1 	1r   c                 x    | j                         s
t               S |r| j                         |k  r
t               S | S r*   )r   NullMatcherr<   r/   s     r   r1   zListMatcher.replace  s4    ~~= D,,.;= Kr   c                 V    | j                   d uxr | j                   j                         S r*   )r   r8   r   s    r   r8   z"ListMatcher.supports_block_quality  s(    D( :LL779	;r   c                 p    | j                   r| j                   j                  |       S | j                         S r*   )r   r?   block_max_weightr   s    r   r<   zListMatcher.max_quality  s/     <<<<--d33((**r   c                 8    | j                   j                  |       S r*   )r   r?   r   s    r   r?   zListMatcher.block_quality  s    ||))$//r   c                    | j                   t        | j                        k  r^| j                         |k  rK| xj                   dz  c_         | j                   t        | j                        k  r| j                         |k  rKy)NrB   r   )r   r   r   r?   r/   s     r   rg   zListMatcher.skip_to_quality  s\    ggDII&4+=+=+?:+MGGqLG ggDII&4+=+=+?:+Mr   c                 4    | j                   | j                     S r*   )r   r   r   s    r   r%   zListMatcher.id  s    yy!!r   c                 ,    t        | j                        S r*   )iterr   r   s    r   rG   zListMatcher.all_ids  s    DIIr   c                 `    | j                   }|t        d      }t        | j                  |      S )N )r   r   r   r   )r   r   s     r   rJ   zListMatcher.all_items  s*    >BZFDIIv&&r   c                 "   | j                   r| j                   | j                     }t        |t              rXt	        |      dkD  sJ t	        |      dk(  r|d   }n| j
                  j                  |      }|| j                   | j                  <   |S y)Nr   rB   r   )r   r   
isinstancelistr   r   combine)r   vs     r   rI   zListMatcher.value  sy    <<TWW%A!T" 1vz!zq6Q;!A,,Q/A()TWW%Hr   c                 d    | j                   j                  |      } || j                               S r*   )r   decoderrI   r   rN   r   s      r   rL   zListMatcher.value_as  s'    ,,&&v.tzz|$$r   c                 8    | j                   j                  |      S r*   )r   rS   rM   s     r   rS   zListMatcher.supports  s    ||$$V,,r   c                 .    | xj                   dz  c_         y r   r   r   s    r   rD   zListMatcher.next  s    1r   c                 ~    | j                   r| j                   S | j                  r| j                  | j                     S yNr   )r   r   r   r   s    r   rk   zListMatcher.weight  s4    $$$]]==))r   c                 6    | j                   j                         S r*   )r   
min_lengthr   s    r   block_min_lengthzListMatcher.block_min_length'      ~~((**r   c                 6    | j                   j                         S r*   )r   
max_lengthr   s    r   block_max_lengthzListMatcher.block_max_length*  r   r   c                     | j                   r| j                   S | j                  rt        | j                        S | j                  | j                  j	                         S yr   )r   r   maxr   
max_weightr   s    r   r   zListMatcher.block_max_weight-  sM    $$$]]t}}%%^^'>>,,..r   c                 p    | j                   r| j                   j                  |       S | j                         S r*   )r   rm   rk   r   s    r   rm   zListMatcher.score7  s*    <<<<%%d++;;= r   )NNNNr   NNNr~   )r
   r   r   r   r   r   r   r   rd   r   r3   r1   r8   r<   r?   rg   r%   rG   rJ   rI   rL   rS   rD   rk   r   r   r   rm   r   r   r   r   r     s     ?CAE"60(1
;+0
"'0%-++!r   r   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)LeafMatcherc                 x    | j                   j                  d| j                         d| j                         dS )N(z, ))r;   r
   r   r   r   s    r   r   zLeafMatcher.__repr__E  s+    #~~66		#~~/1 	1r   c                     | j                   S r*   r   r   s    r   r   zLeafMatcher.termI  r   r   c              #      K   | j                   j                  |      }| j                         D ]  \  }}| ||      f  y wr*   )r   r   rJ   )r   rN   r   r%   rI   s        r   rO   zLeafMatcher.items_asL  sC     ++%%f-) 	'IBwu~&&	's   AAc                 8    | j                   j                  |      S r*   )r   rS   rM   s     r   rS   zLeafMatcher.supportsQ  s    {{##F++r   c                 d    | j                   j                  |      } || j                               S r*   )r   r   rI   r   s      r   rL   zLeafMatcher.value_asT  s'    ++%%f-tzz|$$r   c           
      L   ddl m} | j                  d      r0| j                  d      D cg c]  \  }}} ||||       c}}}S | j                  d      r%| j                  d      D cg c]
  } ||       c}S t	        d| j                         z        c c}}}w c c}w )Nr   rV   rX   rY   r\   z%Field does not support positions (%r))r]   rW   rS   rL   r^   r   r_   s        r   ra   zLeafMatcher.spansX  s    +==&37==3NP P/Y 	7C P P]];')-{)CD#DIDDC"iik* + +P Es   B,B!c                 R    | j                   xr | j                   j                         S r*   )r   r8   r   s    r   r8   z"LeafMatcher.supports_block_qualityd  s    {{Ct{{AACCr   c                 6    | j                   j                         S r*   )r   r<   r   s    r   r<   zLeafMatcher.max_qualityg  s    {{&&((r   c                 8    | j                   j                  |       S r*   )r   r?   r   s    r   r?   zLeafMatcher.block_qualityj  s    {{((..r   c                 8    | j                   j                  |       S r*   )r   rm   r   s    r   rm   zLeafMatcher.scorem  s    {{  &&r   N)r
   r   r   r   r   rO   rS   rL   ra   r8   r<   r?   rm   r   r   r   r   r   @  s5    
1'
,%
+D)/'r   r   )r   sys	itertoolsr   whoosh.compatr   r   r   r^   r   r   objectr   r   r   r   r   r   r   r   r   <module>r      s}   80   & (
  L8f L8b7 . w  H  `!' `!J.'' .'r   