
    g	                     8   d Z ddlZddlZ	 ddlZddlZ G d d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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'd Z#d! Z$d" Z%d# Z&d'd$Z'd'd%Z(y# e$ r  ed        ed       Y w xY w)(a  
============================================
 TGrep search implementation for NLTK trees
============================================

This module supports TGrep2 syntax for matching parts of NLTK Trees.
Note that many tgrep operators require the tree passed to be a
``ParentedTree``.

External links:

- `Tgrep tutorial <https://www.stanford.edu/dept/linguistics/corpora/cas-tut-tgrep.html>`_
- `Tgrep2 manual <http://tedlab.mit.edu/~dr/Tgrep2/tgrep2.pdf>`_
- `Tgrep2 source <http://tedlab.mit.edu/~dr/Tgrep2/>`_

Usage
=====

>>> from nltk.tree import ParentedTree
>>> from nltk.tgrep import tgrep_nodes, tgrep_positions
>>> tree = ParentedTree.fromstring('(S (NP (DT the) (JJ big) (NN dog)) (VP bit) (NP (DT a) (NN cat)))')
>>> list(tgrep_nodes('NN', [tree]))
[[ParentedTree('NN', ['dog']), ParentedTree('NN', ['cat'])]]
>>> list(tgrep_positions('NN', [tree]))
[[(0, 2), (2, 1)]]
>>> list(tgrep_nodes('DT', [tree]))
[[ParentedTree('DT', ['the']), ParentedTree('DT', ['a'])]]
>>> list(tgrep_nodes('DT $ JJ', [tree]))
[[ParentedTree('DT', ['the'])]]

This implementation adds syntax to select nodes based on their NLTK
tree position.  This syntax is ``N`` plus a Python tuple representing
the tree position.  For instance, ``N()``, ``N(0,)``, ``N(0,0)`` are
valid node selectors.  Example:

>>> tree = ParentedTree.fromstring('(S (NP (DT the) (JJ big) (NN dog)) (VP bit) (NP (DT a) (NN cat)))')
>>> tree[0,0]
ParentedTree('DT', ['the'])
>>> tree[0,0].treeposition()
(0, 0)
>>> list(tgrep_nodes('N(0,0)', [tree]))
[[ParentedTree('DT', ['the'])]]

Caveats:
========

- Link modifiers: "?" and "=" are not implemented.
- Tgrep compatibility: Using "@" for "!", "{" for "<", "}" for ">" are
  not implemented.
- The "=" and "~" links are not implemented.

Known Issues:
=============

- There are some issues with link relations involving leaf nodes
  (which are represented as bare strings in NLTK trees).  For
  instance, consider the tree::

      (S (A x))

  The search string ``* !>> S`` should select all nodes which are not
  dominated in some way by an ``S`` node (i.e., all nodes which are
  not descendants of an ``S``).  Clearly, in this tree, the only node
  which fulfills this criterion is the top node (since it is not
  dominated by anything).  However, the code here will find both the
  top node and the leaf node ``x``.  This is because we cannot recover
  the parent of the leaf, since it is stored as a bare string.

  A possible workaround, when performing this kind of search, would be
  to filter out all leaf nodes.

Implementation notes
====================

This implementation is (somewhat awkwardly) based on lambda functions
which are predicates on a node.  A predicate is a function which is
either True or False; using a predicate function, we can identify sets
of nodes with particular properties.  A predicate function, could, for
instance, return True only if a particular node has a label matching a
particular regular expression, and has a daughter node which has no
sisters.  Because tgrep2 search strings can do things statefully (such
as substituting in macros, and binding nodes with node labels), the
actual predicate function is declared with three arguments::

    pred = lambda n, m, l: return True # some logic here

``n``
    is a node in a tree; this argument must always be given

``m``
    contains a dictionary, mapping macro names onto predicate functions

``l``
    is a dictionary to map node labels onto nodes in the tree

``m`` and ``l`` are declared to default to ``None``, and so need not be
specified in a call to a predicate.  Predicates which call other
predicates must always pass the value of these arguments on.  The
top-level predicate (constructed by ``_tgrep_exprs_action``) binds the
macro definitions to ``m`` and initialises ``l`` to an empty dictionary.
    NzAWarning: nltk.tgrep will not work without the `pyparsing` packagez
installed.c                       e Zd ZdZy)TgrepExceptionzTgrep exception type.N)__name__
__module____qualname____doc__     ?/var/www/openai/venv/lib/python3.12/site-packages/nltk/tgrep.pyr   r   |   s    r
   r   c                     g }	 | j                         }|r$|j                  |       |j                         }|r$|S # t        $ r |cY S w xY w)z
    Returns the list of all nodes dominating the given tree node.
    This method will not work with leaf nodes, since there is no way
    to recover the parent.
    )parentAttributeErrorappendnoderesultscurrents      r   	ancestorsr      sX     G++- w.."  N  s   < A
	A
c                     g }	 | j                         }|r@t        |      dk(  r2|j                  |       |j                         }|rt        |      dk(  r2|S # t        $ r |cY S w xY w)zt
    Returns the list of all nodes dominating the given node, where
    there is only a single path of descent.
       )r   r   lenr   r   s      r   unique_ancestorsr      sn    
 G++- c'la'w.." c'la' N  s   A A&%A&c                 |    	 | j                         }|dd D cg c]  }| |   	 c}S # t        $ r g cY S w xY wc c}w )ze
    Returns the list of all nodes which are descended from the given
    tree node in some way.
    r   N)treepositionsr   r   treeposxs      r   _descendantsr      sN    
$$& %QR[)[DG[))  	)s   ( 966c                     	 | j                         }|dd D cg c]  }t        d |D              s| |    c}S # t        $ r g cY S w xY wc c}w )zf
    Returns the set of all nodes descended in some way through
    left branches from this node.
    r   Nc              3   &   K   | ]	  }|d k(    yw)r   Nr	   ).0ys     r   	<genexpr>z(_leftmost_descendants.<locals>.<genexpr>   s     /B1Qs   )r   r   allr   s      r   _leftmost_descendantsr%      s[    
$$& %QR[C[C/B/B,BDG[CC  	Cs   ; AAA	A	c                     	 t        | j                               }t        dt	        |      dz         D cg c]
  }| |d|     c}S # t        $ r g cY S w xY wc c}w )zg
    Returns the set of all nodes descended in some way through
    right branches from this node.
    r   N)maxr   r   ranger   )r   rightmost_leafis      r   _rightmost_descendantsr+      sh    
T//12 /4As>7JQ7N.OP.OD#$.OPP  	Ps   A AAAc                 J    t        | t        j                  j                        S )z5Predicate to check whether `obj` is a nltk.tree.Tree.)
isinstancenltktreeTree)objs    r   _istreer2      s    c499>>**r
   c                     g }| }|rKt        |      r@t        |      dk(  r2|d   }|j                  |       |rt        |      rt        |      dk(  r2|S )zx
    Returns the list of all nodes descended from the given node, where
    there is only a single path of descent.
    r   r   )r2   r   r   r   s      r   _unique_descendantsr4      sV    
 GG
gg&3w<1+<!*w gg&3w<1+< Nr
   c           	          	 | j                         }| j                         }|j                         D cg c]%  }|dt	        |       |dt	        |       k  s!||   ' c}S # t        $ r g cY S w xY wc c}w )zF
    Returns the set of all nodes that are before the given node.
    Ntreepositionrootr   r   r   r   posr/   r   s       r   _beforer;      y    !yy{ "//1S1QzS]S3q6]5RDG1SS  	S    A! "A2A2!A/.A/c                 4   	 | j                         }| j                         }t        |      dz
  }d|k  r||   dk(  r|dz  }d|k  r	||   dk(  r|dk  rg S t	        |d|dz          }|dxx   dz  cc<   ||   }|gt        |      z   S # t        $ r g cY S w xY w)z
    Returns the set of all nodes that are immediately before the given
    node.

    Tree node A immediately precedes node B if the last terminal
    symbol (word) produced by A immediately precedes the first
    terminal symbol produced by B.
    r   r   N)r7   r8   r   r   listr+   )r   r:   r/   idxbefores        r   _immediately_beforerC      s    !yy{ c(Q,C
s(s3x1}q s(s3x1}
Qw	
s9S1W~
CGqLG#YF8,V444  	s    B	 	BBc           	          	 | j                         }| j                         }|j                         D cg c]%  }|dt	        |       |dt	        |       kD  s!||   ' c}S # t        $ r g cY S w xY wc c}w )zE
    Returns the set of all nodes that are after the given node.
    Nr6   r9   s       r   _afterrE     r<   r=   c                    	 | j                         }| j                         }| j                         }t	        |      dz
  }d|k  rC||   t	        |      dz
  k(  r/|dz  }|j                         }d|k  r||   t	        |      dz
  k(  r/|dk  rg S t        |d|dz          }|dxx   dz  cc<   ||   }|gt        |      z   S # t        $ r g cY S w xY w)z
    Returns the set of all nodes that are immediately after the given
    node.

    Tree node A immediately follows node B if the first terminal
    symbol (word) produced by A immediately follows the last
    terminal symbol produced by B.
    r   r   Nr?   )r7   r8   r   r   r   r@   r%   )r   r:   r/   r   rA   afters         r   _immediately_afterrH     s    !yy{++-
 c(Q,C
s(s3x3w<!#33q.." s(s3x3w<!#33 Qw	
s9S1W~
CGqLGIE7*5111  	s   0C CCc                 N    t        |       r| j                         S t        |       S )zw
    Gets the string value of a given parse tree node, for comparison
    using the tgrep node literal predicates.
    )r2   labelstr)r   s    r   _tgrep_node_literal_valuerL   *  s    
 #4=4::<7c$i7r
   c                 ^    t        |      dk(  sJ |d   d   dk(  sJ |d   dd dfd	}|S )zF
    Builds a lambda function which looks up the macro name used.
    r   r   @Nc                 H    ||vrt        d d       |   | ||      S )Nzmacro z not definedr   )nml
macro_names      r   	macro_usez*_tgrep_macro_use_action.<locals>.macro_use:  s8    9
!+ 6*\!BCCq}Q1%%r
   NNr   )_s_ltokensrU   rT   s       @r   _tgrep_macro_use_actionr[   2  sI     v;!!9Q<312J&
 r
   c                    |d   dk(  r|dd }t        |      dkD  rJt        t        |ddd               dgk(  sJ |ddd   D cg c]  }t        dd|g       }} d |      S t	        |d   d      r|d   S |d   d	k(  s|d   d
k(  rddS |d   j                  d      rG|d   j                  d      sJ |d   dd j                  dd      j                  dd      } d |      S |d   j                  d      r:|d   j                  d      sJ |d   dd } d t        j                  |            S |d   j                  d      r+t        | ||d   dd j                         g      } d |      S  d |d         S c c}w )zq
    Builds a lambda function representing a predicate on a tree node
    depending on the name of its node.
    r   'r   N   |c                      d fd	S )Nc                 6     t         fdD              S )Nc              3   2   K   | ]  } |        y wNr	   )r!   frS   rR   rQ   s     r   r#   zI_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>.<locals>.<genexpr>P  s     7NAq!Q
A   any)rQ   rR   rS   ts   ```r   <lambda>z6_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>P  s    C7NA7N4Nr
   rV   r	   )rh   s   `r   ri   z$_tgrep_node_action.<locals>.<lambda>P  s    Nr
   __call__*__c                      y)NTr	   )rQ   rR   rS   s      r   ri   z$_tgrep_node_action.<locals>.<lambda>W  s    Tr
   "r?   z\"z\\\c                      d fd	S )Nc                      t        |       k(  S rc   rL   rQ   rR   rS   ss      r   ri   z6_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>\      4Ma4PTU4Ur
   rV   r	   rt   s   `r   ri   z$_tgrep_node_action.<locals>.<lambda>\      Ur
   /c                      d fd	S )Nc                 8    j                  t        |             S rc   )searchrL   rQ   rR   rS   rs      r   ri   z6_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>b  s    AHH-a05r
   rV   r	   r}   s   `r   ri   z$_tgrep_node_action.<locals>.<lambda>b       r
   zi@c                      d fd	S )Nc                 B     t        |       j                               S rc   )rL   lower)rQ   rR   rS   rd   s      r   ri   z6_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>i  s    A-a06685r
   rV   r	   )rd   s   `r   ri   z$_tgrep_node_action.<locals>.<lambda>i  r   r
   c                      d fd	S )Nc                      t        |       k(  S rc   rr   rs   s      r   ri   z6_tgrep_node_action.<locals>.<lambda>.<locals>.<lambda>o  ru   r
   rV   r	   rv   s   `r   ri   z$_tgrep_node_action.<locals>.<lambda>o  rw   r
   rV   )r   r@   set_tgrep_node_actionhasattr
startswithendswithreplacerecompiler   )rX   rY   rZ   r   node_lit	node_funcs         r   r   r   B  s   
 ayC
6{QCqt!t%&3%///EKCaC[Q[T$T4$8[QNPVWW6!9j) !9AY#d!211AY!!#&!9%%c***ay2..uc:BB64PHU  AY!!#&!9%%c***ay2H jj"	$ $
 AY!!$'*2rF1IabM4G4G4I3JKI 	  VQi A Rs   Fc                 T    t        |      dk(  sJ |d   dk(  sJ |d   dk(  sJ |d   S )zm
    Builds a lambda function representing a predicate on a tree node
    from a parenthetical notation.
       r   (r^   )r   rW   rX   rY   rZ   s      r   _tgrep_parens_actionr   s  sD    
 v;!!9!9!9r
   c                 8    t        d |D              } d |      S )z
    Builds a lambda function representing a predicate on a tree node
    which returns true if the node is located at a specific tree
    position.
    c              3   T   K   | ]   }|j                         st        |       " y wrc   )isdigitint)r!   r   s     r   r#   z._tgrep_nltk_tree_pos_action.<locals>.<genexpr>  s     Ev!s1vvs   ((c                      d fd	S )Nc                 F    t        | d      xr | j                         k(  S )Nr7   )r   r7   )rQ   rR   rS   r*   s      r   ri   z?_tgrep_nltk_tree_pos_action.<locals>.<lambda>.<locals>.<lambda>  s!    A~&@1>>+;q+@@r
   rV   r	   )r*   s   `r   ri   z-_tgrep_nltk_tree_pos_action.<locals>.<lambda>  s     
r
   )tuple)rX   rY   rZ   node_tree_positions       r   _tgrep_nltk_tree_pos_actionr   ~  s)     EvEE	
 	 r
   c                 x   d}|d   dk(  rd}|dd }|d   dk(  r!t        |      dk(  sJ |d	   d
k(  sJ |d   }nst        |      d	k(  sJ |\  }|dk(  rdPfd	}nQ|dk(  rdPfd	}nD|dk(  s|dk(  rdPfd	}n2|dk(  s|dk(  rdPfd	}n |d   dk(  r1|dd j                         rt        |dd       } fd|dz
        }n|d   dk(  r1|dd j                         rt        |dd       } fd|dz
        }n|dk(  s
|dk(  s|dk(  rdPfd	}n|dk(  s
|dk(  s|dk(  rdPfd	}n|dd	 dk(  r/|d	d j                         rt        |d	d        } fd|      }nI|dd	 dk(  r/|d	d j                         rt        |d	d        } fd |      }n|d!k(  rdPfd"	}n|d#k(  rdPfd$	}n|d%k(  rdPfd&	}n|d'k(  rdPfd(	}n|d)k(  s|d*k(  rdPfd+	}n|d,k(  rdPfd-	}n|d.k(  rdPfd/	}n|d0k(  rdPfd1	}n|d2k(  rdPfd3	}n|d4k(  rdPfd5	}n|d6k(  rdPfd7	}n|d8k(  rdPfd9	}n||d:k(  rdPfd;	}np|d<k(  rdPfd=	}nd|d>k(  s|d?k(  rdPfd@	}nS|dAk(  s|dBk(  rdPfdC	}nB|dDk(  s|dEk(  rdPfdF	}n1|dGk(  s|dHk(  rdPfdI	}n |dJk(  s|dKk(  rdPfdL	}nt        dM| dN      |r	 dO |      S |S )Qz
    Builds a lambda function representing a predicate on a tree node
    depending on its relation to other nodes in the tree.
    Fr   !Tr   N[r   r^   ]<c                 N    t        |       xr t        fd| D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r!   r   rS   rR   	predicates     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>  s     "Aq!9Q1#5qre   )r2   rg   rQ   rR   rS   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s    
As"Aq"AAAr
   >c                     t        | d      xr3 t        | j                               xr  | j                         ||      S )Nr   r   boolr   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s<    8$ 0$0ahhj!Q/0r
   z<,z<1c                 d    t        |       xr# t        t        |             xr  | d   ||      S Nr   r2   r   r@   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s,    
FtDG}F1Q4A1FFr
   z>,z>1c                     t        | d      xrJ t        | j                               xr/ | | j                         d   u xr  | j                         ||      S Nr   r   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  sU    8$ 0$0!((*Q-'0 ahhj!Q/0r
   c                      d fd	S )Nc                     t        |       xr> t        t        |             xr( dcxk  xr t        |       k  nc xr  |    ||      S r   r2   r   r@   r   rQ   rR   rS   r*   r   s      r   ri   z:_tgrep_relation_action.<locals>.<lambda>.<locals>.<lambda>  sJ    AJ .T!W.QQ. "!A$1-.r
   rV   r	   r*   r   s   `r   ri   z(_tgrep_relation_action.<locals>.<lambda>       r
   c                      d fd	S )Nc                    t        | d      xrs t        | j                               xrX dcxk  xr t        | j                               k  nc xr/ | | j                            u xr  | j                         ||      S r   r   r   r   r   r   s      r   ri   z:_tgrep_relation_action.<locals>.<lambda>.<locals>.<lambda>  ss    Ax( 4QXXZ(4Q0QXXZ04 ahhjm+4 "!((*a3	4r
   rV   r	   r   s   `r   ri   z(_tgrep_relation_action.<locals>.<lambda>       r
   z<'z<-z<-1c                 d    t        |       xr# t        t        |             xr  | d   ||      S )Nr?   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s,    
GtDG}G1R5!Q1GGr
   z>'z>-z>-1c                     t        | d      xrJ t        | j                               xr/ | | j                         d   u xr  | j                         ||      S )Nr   r?   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  sU    8$ 0$0!((*R.(0 ahhj!Q/0r
   c                      d fd	S )Nc                     t        |       xrV t        t        |             xr@ dt        |       z   cxk  xr t        |       k  nc xr  | t        |       z      ||      S r   r   r   s      r   ri   z:_tgrep_relation_action.<locals>.<lambda>.<locals>.<lambda>  s\    AJ 7T!W7a#a&j2CF27 "!AAJ-A67r
   rV   r	   r   s   `r   ri   z(_tgrep_relation_action.<locals>.<lambda>  r   r
   c                      d fd	S )Nc                 n   t        | d      xr t        | j                               xr dt        | j                               z   cxk  xr t        | j                               k  nc xrI | | j                         t        | j                               z      u xr  | j                         ||      S r   r   r   s      r   ri   z:_tgrep_relation_action.<locals>.<lambda>.<locals>.<lambda>  s    Ax( 4QXXZ(4a#ahhj/1DS_D4 ahhjS_)<==4 "!((*a3	4r
   rV   r	   r   s   `r   ri   z(_tgrep_relation_action.<locals>.<lambda>  r   r
   z<:c                 X    t        |       xr t        |       dk(  xr  | d   ||      S )Nr   r   )r2   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s,    
Ds1v{Dy1q!/DDr
   z>:c                     t        | d      xrQ t        | j                               xr6 t        | j                               dk(  xr  | j                         ||      S )Nr   r   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>  sU    8$ 0$0
Oq(0 ahhj!Q/0r
   z<<c                 `    t        |       xr t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>  s     "O!9Q1#5re   )r2   rg   r   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>
  s!    
Os"O|A"OOOr
   z>>c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>  s      3,8q	!Q"Lre   rg   r   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s    s 3,5aL3 0r
   z<<,z<<1c                 `    t        |       xr t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>  s     "X?W!9Q1#5?Wre   )r2   rg   r%   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s$    
Xs"X?TUV?W"XXXr
   z>>,c                 J     t         fdt               D              S )Nc              3   P   K   | ]  } |      xr t        |      v   y wrc   )r%   r!   r   rS   rR   rQ   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>  s5      3%A 1a#E-B1-E(EE%   #&r   r   s   ```r   ri   z(_tgrep_relation_action.<locals>.<lambda>      s 3"13 0r
   z<<'c                 `    t        |       xr t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>!  s     N4Mq	!Q*4Mre   )r2   rg   r+   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>  s*    
 ON4J14MNNOr
   z>>'c                 J     t         fdt               D              S )Nc              3   P   K   | ]  } |      xr t        |      v   y wrc   )r+   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>%  s5      3%A 1a#F-CA-F(FF%r   r   r   s   ```r   ri   z(_tgrep_relation_action.<locals>.<lambda>%  r   r
   z<<:c                 `    t        |       xr t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>,  s     "V?U!9Q1#5?Ure   )r2   rg   r4   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>+  s$    
Vs"V?RST?U"VVVr
   z>>:c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>0  s      3,?q	!Q",?re   )rg   r   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>0  s    s 3,<Q,?3 0r
   .c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>5  s      3,Aq	!Q",Are   )rg   rH   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>5  s    s 3,>q,A3 0r
   ,c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>:  s      3,Bq	!Q",Bre   )rg   rC   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>:  s    s 3,?,B3 0r
   z..c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>?  s      3,5q	!Q"Ire   )rg   rE   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>?  s    s 3,21I3 0r
   z,,c                 F    t        fdt        |       D              S )Nc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>D  s      3,6q	!Q"Jre   )rg   r;   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>D  s    s 3,3AJ3 0r
   $%c                      t         d      xr@ t         j                               xr% t         fd j                         D              S )Nr   c              3   <   K   | ]  }|us |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>L  s!     MJq!1*	!Q*Js   	)r   r   r   rg   r   s   ```r   ri   z(_tgrep_relation_action.<locals>.<lambda>I  sA    8$ N$NMAHHJMMNr
   z$.z%.c                     t        | d      xr3 t        | j                               xr  | j                         ||      S )Nright_sibling)r   r   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>P  s>    ?+ 7*+7aoo/A67r
   z$,z%,c                     t        | d      xr3 t        | j                               xr  | j                         ||      S )Nleft_sibling)r   r   r   r   s      r   ri   z(_tgrep_relation_action.<locals>.<lambda>W  s>    >* 6)*6ann.156r
   z$..z%..c                     t        | d      xra t        | d      xrS t        | j                               xr8 t        fd| j                         | j	                         dz   d  D              S )Nr   parent_indexc              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>b  s     W4Vq	!Q*4Vre   r   r   r   r   rg   r   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>^  sh    8$ XA~.X$X WAHHJq~~?ORS?S?U4VWWXr
   z$,,z%,,c                     t        | d      xr^ t        | d      xrP t        | j                               xr5 t        fd| j                         d | j	                          D              S )Nr   r   c              3   2   K   | ]  } |        y wrc   r	   r   s     r   r#   z;_tgrep_relation_action.<locals>.<lambda>.<locals>.<genexpr>j  s     S4Rq	!Q*4Rre   r   r   s    ``r   ri   z(_tgrep_relation_action.<locals>.<lambda>f  sb    8$ TA~.T$T SAHHJ?QAQ4RSSTr
   z!cannot interpret tgrep operator "rn   c                      d fd	S )Nc                      | ||       S rc   r	   r|   s      r   ri   z:_tgrep_relation_action.<locals>.<lambda>.<locals>.<lambda>p  s    1a^r
   rV   r	   r~   s   `r   ri   z(_tgrep_relation_action.<locals>.<lambda>p  s    Cr
   rV   )r   r   r   r   )rX   rY   rZ   negatedretvaloperatorrA   r   s          @r   _tgrep_relation_actionr     s    GayCayC6{aayC 6{a$)s?F _F T!1F T!1F a[CHQRL$8$8$:hqrl#C AgF a[CHQRL$8$8$:hqrl#C AgF T!1X5FF
 T!1X5FF bq\T!hqrl&:&:&<x|$$C F bq\T!hqrl&:&:&<x|$$C F F F F F (e"3F F
 F
 F
 F F _F _F F F _CF T!1F T!1F (e"3F (e"3F !#DXJa!PQQDfMMr
   c                 n    |D cg c]
  }||k7  s	| }}t        |      dk(  r|d   S  d |      S c c}w )a  
    Builds a lambda function representing a predicate on a tree node
    from the conjunction of several other such lambda functions.

    This is prototypically called for expressions like
    (`tgrep_rel_conjunction`)::

        < NP & < AP < VP

    where tokens is a list of predicates representing the relations
    (`< NP`, `< AP`, and `< VP`), possibly with the character `&`
    included (as in the example here).

    This is also called for expressions like (`tgrep_node_expr2`)::

        NP < NN
        S=s < /NP/=n : s < /VP/=v : n .. v

    tokens[0] is a tgrep_expr predicate; tokens[1:] are an (optional)
    list of segmented patterns (`tgrep_expr_labeled`, processed by
    `_tgrep_segmented_pattern_action`).
    r   r   c                      d fd	S )Nc                 6     t         fdD              S )Nc              3   2   K   | ]  } |        y wrc   r	   )r!   r   rS   rR   rQ   s     r   r#   zP_tgrep_conjunction_action.<locals>.<lambda>.<locals>.<lambda>.<locals>.<genexpr>  s      546y	!Q"Bre   )r$   )rQ   rR   rS   tss   ```r   ri   z=_tgrep_conjunction_action.<locals>.<lambda>.<locals>.<lambda>  s     5465 2r
   rV   r	   )r   s   `r   ri   z+_tgrep_conjunction_action.<locals>.<lambda>  s     r
   rW   )rX   rY   rZ   	join_charr   s        r   _tgrep_conjunction_actionr   u  sM    0  2A1	>aF2
6{aay 	 		 3s   
22c                 ,    |d   |dd dfd	}|S )a  
    Builds a lambda function representing a segmented pattern.

    Called for expressions like (`tgrep_expr_labeled`)::

        =s .. =v < =n

    This is a segmented pattern, a tgrep2 expression which begins with
    a node label.

    The problem is that for segemented_pattern_action (': =v < =s'),
    the first element (in this case, =v) is specifically selected by
    virtue of matching a particular node in the tree; to retrieve
    the node, we need the label, not a lambda function.  For node
    labels inside a tgrep_node_expr, we need a lambda function which
    returns true if the node visited is the same as =v.

    We solve this by creating two copies of a node_label_use in the
    grammar; the label use inside a tgrep_expr_labeled has a separate
    parse action to the pred use inside a node_expr.  See
    `_tgrep_node_label_use_action` and
    `_tgrep_node_label_pred_use_action`.
    r   r   Nc                 j    vrt        d d         t        fdD              S )z2This predicate function ignores its node argument.node_label = not bound in patternc              3   2   K   | ]  } |        y wrc   r	   )r!   predrS   rR   r   s     r   r#   zP_tgrep_segmented_pattern_action.<locals>.pattern_segment_pred.<locals>.<genexpr>  s     ;
4a#
re   )r   r$   )rQ   rR   rS   r   
node_label
reln_predss    ``@r   pattern_segment_predz=_tgrep_segmented_pattern_action.<locals>.pattern_segment_pred  s@     9
!+ <
|;P!QRR};
;;;r
   rV   r	   )rX   rY   rZ   r  r  r  s       @@r   _tgrep_segmented_pattern_actionr    s(    2 J J<  r
   c                 ^    t        |      dk(  sJ |d   j                  d      sJ |d   dd S )aU  
    Returns the node label used to begin a tgrep_expr_labeled.  See
    `_tgrep_segmented_pattern_action`.

    Called for expressions like (`tgrep_node_label_use`)::

        =s

    when they appear as the first element of a `tgrep_expr_labeled`
    expression (see `_tgrep_segmented_pattern_action`).

    It returns the node label.
    r   r   =Nr   r   r   s      r   _tgrep_node_label_use_actionr	    s>     v;!!9$$$!9QR=r
   c                 p    t        |      dk(  sJ |d   j                  d      sJ |d   dd dfd	}|S )a  
    Builds a lambda function representing a predicate on a tree node
    which describes the use of a previously bound node label.

    Called for expressions like (`tgrep_node_label_use_pred`)::

        =s

    when they appear inside a tgrep_node_expr (for example, inside a
    relation).  The predicate returns true if and only if its node
    argument is identical the the node looked up in the node label
    dictionary using the node's label.
    r   r   r  Nc                 @    ||vrt        d d      |   }| |u S )Nr   r   rP   )rQ   rR   rS   r   r  s       r   node_label_use_predz>_tgrep_node_label_pred_use_action.<locals>.node_label_use_pred  s7    9
!+ <
|;P!QRR}Dyr
   rV   r  )rX   rY   rZ   r  r  s       @r   !_tgrep_node_label_pred_use_actionr    sK     v;!!9$$$12J r
   c                     t        |      dk(  r|d   S t        |      dk(  sJ |d   dk(  sJ |d   |d   dfd	}|S )z
    Builds a lambda function representing a predicate on a tree node
    which can optionally bind a matching node into the tgrep2 string's
    label_dict.

    Called for expressions like (`tgrep_node_expr2`)::

        /NP/
        @NP=n
    r   r   r   r  r^   c                 ^     | ||      r"|t        dj                              | |<   yy)Nz-cannot bind node_label {}: label_dict is NoneTF)r   format)rQ   rR   rS   r  	node_preds      r   node_label_bind_predz;_tgrep_bind_node_label_action.<locals>.node_label_bind_pred  sC    Aq!9(GNN& 
 !"*r
   rV   rW   )rX   rY   rZ   r  r  r  s       @@r   _tgrep_bind_node_label_actionr    s`     6{aay 6{aayC1I	AY
	 $#r
   c                     |D cg c]
  }|dk7  s	| }}t        |      dk(  r|d   S t        |      dk(  r d |d   |d         S yc c}w )z
    Builds a lambda function representing a predicate on a tree node
    from the disjunction of several other such lambda functions.
    r_   r   r   r^   c                      d fd	S )Nc                 0     | ||      xs
  | ||      S rc   r	   )rQ   rR   rS   abs      r   ri   zA_tgrep_rel_disjunction_action.<locals>.<lambda>.<locals>.<lambda>#  s    qAqz7OQq!QZ7Or
   rV   r	   )r  r  s   ``r   ri   z/_tgrep_rel_disjunction_action.<locals>.<lambda>#  s    Or
   NrW   )rX   rY   rZ   r   s       r   _tgrep_rel_disjunction_actionr    sd      ,A18aF,
6{aay	V	O1Ivay
 	
 
 -s
   
AAc                 J    t        |      dk(  sJ |d   dk(  sJ |d   |d   iS )zF
    Builds a dictionary structure which defines the given macro.
    r   r   rN   r   r^   rW   r   s      r   _macro_defn_actionr  (  s;     v;!!91Ivay!!r
   c                 B  	 t              dk(  rdfd	S D cg c]
  }|dk7  s	| c}i }D cg c]  }t        |t              s| }}|D ]  }|j                  |        D cg c]  }t        |t              r| c}	|df	fd	}|S c c}w c c}w c c}w )a  
    This is the top-lebel node in a tgrep2 search string; the
    predicate function it returns binds together all the state of a
    tgrep2 search string.

    Builds a lambda function representing a predicate on a tree node
    from the disjunction of several tgrep expressions.  Also handles
    macro definitions and macro name binding, and node label
    definitions and node label binding.
    r   Nc                      d   | d i       S r   r	   )rQ   rR   rS   rZ   s      r   ri   z%_tgrep_exprs_action.<locals>.<lambda>=  s    1dB)?r
   ;c                 :     i t         fdD              S )Nc              3   2   K   | ]  } |        y wrc   r	   )r!   r   
label_dictrR   rQ   s     r   r#   z>_tgrep_exprs_action.<locals>.top_level_pred.<locals>.<genexpr>L  s     L99Q:.re   rf   )rQ   rR   rS   r!  tgrep_exprss   `` @r   top_level_predz+_tgrep_exprs_action.<locals>.top_level_predI  s    
LLLLr
   rV   )r   r-   dictupdate)
rX   rY   rZ   r   
macro_dicttok
macro_defs	macro_defr#  r"  s
     `      @r   _tgrep_exprs_actionr*  1  s     6{a??,A18a,FJ!'A#:c4+@#JA	)$   #)F&3
30E3&FK '$ M
  - B Gs!   
BBB	B+BBc                 F   t        j                  d      t        j                  d      z   }t        j                  ddd      }t        j                  ddd      }t        j                  d      }t        j                  d	      }t        j                  d
      }t        j                         }t        j                         }t        j
                  d      |z   dz   }	t        j
                  d      t        j                  t        j                  t         j                        dz   t        j                  t        j                  t        j                  t         j                        d      t        j                  d      z         z         z   dz   }
t        j                  d      }t        j                  d|z         }|j                         }t        j                  d      }|j                  d       t        j                  d|z         }||z  |
z  |z  |z  |z  |z  dz  |z  }|t        j
                  d      j                  d      z   |j                         j                  d      z   |z  }|	t        j                  d      |z   t        j                  d|z         z   z  }t        j                  d      dz   |z   dz   }|||z   z  }t        j                         }||t        j                  t        j                  d      |z         z   z   ||t        j                  d|z         z   z   ||t        j                  |      z   z   |t        j                  |      z   }|t        j                  d|z         z   }t        j
                  d      t        j                         j                         z   |z   |z   }t        j                  |t        j                  d|z         z   dz         |z   t        j                  d||z  z         z   t        j                  d      j                         z   }| rP|j                  t                |j                  t"               |j                  t$               |j                  t&               |j                  t(               |	j                  t*               |
j                  t,               |j                  t.               |j                  t0               |j                  t2               |j                  t4               |j                  t0               |j                  t6               |j                  t9        j:                  t0        d             |j                  t<               |j?                  dt         j@                  z         S )zj
    Builds a pyparsing-based parser object for tokenizing and
    interpreting tgrep search strings.
    r   z[$%,.<>][%,.<>0-9-':]*rn   ro   F)	quoteCharescCharunquoteResultsrx   zi@\"(?:[^"\n\r\\]|(?:\\.))*\"zi@\/(?:[^/\n\r\\]|(?:\\.))*\/z[^][ 	
;:.,&|<>()$!@%'^=]+r   r   zN(r   )delimz[A-Za-z0-9]+r  z[^];:.,&|<>()[$!@%'^=	
 ]+ rN   rk   r]   r_   r   r   &:r  )r   #)!	pyparsingOptionalRegexQuotedStringForwardLiteralWordnumsdelimitedListCombinecopysetWhitespaceChars
ZeroOrMoreWhitesuppresssetParseActionr	  r  r[   r   r  r   r   r   r   r  r  r  	functoolspartialr*  ignore
restOfLine)set_parse_actionstgrep_optgrep_qstringtgrep_node_regextgrep_qstring_icasetgrep_node_regex_icasetgrep_node_literal
tgrep_exprtgrep_relationstgrep_parenstgrep_nltk_tree_postgrep_node_labeltgrep_node_label_usetgrep_node_label_use_predrT   rU   tgrep_node_exprtgrep_node_expr2
tgrep_nodetgrep_bracketstgrep_relationtgrep_rel_conjunctiontgrep_expr_labeledtgrep_expr2
macro_defnr"  s                             r   _build_tgrep_parserr_  Q  s'   
 !!#&9Q)RRH**tEM !--tE $//*QR&__-TU")IJ""$J'')O$$S)J6<L$


NN9>>*  ''	y~~(FcR$$S)*
	
 		  !~6$,,S3C-CD 4 9 9 ;!ABJ!!"%!!#
"23I!
	
	 	 !		!
 	 	 	 	  	


C
 
3
3B
7	8



!
4
4R
8	9 		
 3
	


s_4
5	6J
 '',s2_DsJN#x*'<=N%--/


y11#69NN
O	P ,y/C/Co0   *y11/BBB-	0B0B?0SSy33C:L4LMMK#!2!;!;!==
J[X  	:	(<(<S:=M(NNQTTU
	


sj;&>?
@	A 

s
#
,
,
.	/  ++,HI!001RS  !89!!"45''(EF##$89**+FG%%&<=,,-FG&&'DE!!"45 	!!";<))*IJ""73G	
 	""#67cI$8$8899r
   c                     t        d      }t        | t              r| j                         } t	        |j                  |             S )z?
    Tokenizes a TGrep search string into separate tokens.
    Fr_  r-   bytesdecoder@   parseStringtgrep_stringparsers     r   tgrep_tokenizerh    s<     !'F,&#**,""<011r
   c                     t        d      }t        | t              r| j                         } t	        |j                  | d            d   S )z`
    Parses (and tokenizes, if necessary) a TGrep search string into a
    lambda function.
    T)parseAllr   ra  re  s     r   tgrep_compilerk    sF    
 !&F,&#**,""<$"?@CCr
   c                     | j                         }t               }|D ]/  }t        t        |            D ]  }|j	                  |d|         1 |D cg c]	  }||v s| c}S c c}w )zX
    Returns all the tree positions in the given tree which are not
    leaf nodes.
    N)r   r   r(   r   add)r/   r   prefixesr:   lengths        r   treepositions_no_leavesrp    si    
 &&(M uHCHoFLLWf& &  )<=CC8OC=<<<s   	A$A$c              #     K   t        | t        t        f      rt        |       } |D ]?  }	 |r|j	                         }nt        |      }|D cg c]  } | ||         s| c} A yc c}w # t        $ r g  Y Ww xY ww)a  
    Return the tree positions in the trees which match the given pattern.

    :param pattern: a tgrep search pattern
    :type pattern: str or output of tgrep_compile()
    :param trees: a sequence of NLTK trees (usually ParentedTrees)
    :type trees: iter(ParentedTree) or iter(Tree)
    :param search_leaves: whether to return matching leaf nodes
    :type search_leaves: bool
    :rtype: iter(tree positions)
    Nr-   rb  rK   rk  r   rp  r   patterntreessearch_leavesr/   	positionspositions         r   tgrep_positionsry    s      'E3<((	 ..0	3D9	,5QIh9P8IQQ  R 	H	s@   'B"A.A)A)!A.&B)A..A>;B=A>>Bc              #     K   t        | t        t        f      rt        |       } |D ]B  }	 |r|j	                         }nt        |      }|D cg c]  } | ||         s||    c} D yc c}w # t        $ r g  Y Zw xY ww)a  
    Return the tree nodes in the trees which match the given pattern.

    :param pattern: a tgrep search pattern
    :type pattern: str or output of tgrep_compile()
    :param trees: a sequence of NLTK trees (usually ParentedTrees)
    :type trees: iter(ParentedTree) or iter(Tree)
    :param search_leaves: whether to return matching leaf nodes
    :type search_leaves: bool
    :rtype: iter(tree nodes)
    Nrr  rs  s         r   tgrep_nodesr{    s      'E3<((	 ..0	3D9	2;W)hwtH~?V4>)WW  X 	H	s@   'B"A1A,A,$A1)B,A11B>B BB)r1  )T))r   rD  r   r4  ImportErrorprint	nltk.treer.   	Exceptionr   r   r   r   r%   r+   r2   r4   r;   rC   rE   rH   rL   r[   r   r   r   r   r   r  r	  r  r  r  r  r*  r_  rh  rk  rp  ry  r{  r	   r
   r   <module>r     s   dL  	
 	Y 	$"	*	D	Q+

	T54	T2:8 .b dN F' T&:$$N
"@e:P2D=6G  	
MN	,s   B BB