| Server IP : 54.37.205.81 / Your IP : 216.73.216.76 Web Server : nginx/1.22.1 System : Linux vps-249481fa 6.1.0-50-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.176-1 (2026-07-02) x86_64 User : debian ( 1000) PHP Version : 8.2.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /lib/python3/dist-packages/nltk/__pycache__/ |
Upload File : |
�
T�c� � �N � d Z ddlZddlZ ddlZn!# e$ r ed� � ed� � Y nw xY wddlZ G d� de� � Z d� Z
d� Zd � Zd
� Z
d� Zd� Zd
� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd� Zd(d�Zd� Zd� Zd� Zd� Zd� Z d� Z!d � Z"d)d"�Z#d#� Z$d$� Z%d%� Z&d)d&�Z'd)d'�Z(dS )*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 Zd ZdZdS )�TgrepExceptionzTgrep exception type.N)�__name__�
__module__�__qualname__�__doc__� � �,/usr/lib/python3/dist-packages/nltk/tgrep.pyr r | s � � � � � ����Dr
r c � � g } | � � � }n# t $ r |cY S w xY w|r+|� |� � |� � � }|�+|S )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 � s} � � �G���+�+�-�-����� � � ��������� � #����w�����.�.�"�"�� � #� �N� � �(�(c � � g } | � � � }n# t $ r |cY S w xY w|rQt |� � dk r>|� |� � |� � � }|rt |� � dk �>|S )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 � s� � �
�G���+�+�-�-����� � � ��������� � #�c�'�l�l�a�'�'����w�����.�.�"�"�� � #�c�'�l�l�a�'�'� �Nr c � � � � � � � }n# t $ r g cY S w xY w� fd�|dd� D � � S )ze
Returns the list of all nodes which are descended from the given
tree node in some way.
c � �� g | ]
}�| ��S r r ��.0�xr s �r �
<listcomp>z _descendants.<locals>.<listcomp>� s �� �)�)�)��D��G�)�)�)r
r N��
treepositionsr �r �treeposs ` r �_descendantsr$ � sa �� �
��$�$�&�&����� � � �� � � �����)�)�)�)�W�Q�R�R�[�)�)�)�)� � �'�'c � � � � � � � }n# t $ r g cY S w xY w� fd�|dd� D � � S )zf
Returns the set of all nodes descended in some way through
left branches from this node.
c �R �� g | ]#}t d � |D � � � � ��| ��$S )c 3 �"