| 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/parse/__pycache__/ |
Upload File : |
�
T�c�@ � � � d dl mZ d dlmZ d dlmZ G d� de� � Z G d� de� � Zd� Ze d k r e� � d
S d
S )� )�Nonterminal)�ParserI)�Treec �\ � e Zd ZdZdd�Zd� Zd� Zd� Zd� Zdd �Z dd�Z
dd
�Zd� Zd� Z
d� ZdS )�ShiftReduceParsera�
A simple bottom-up CFG parser that uses two operations, "shift"
and "reduce", to find a single parse for a text.
``ShiftReduceParser`` maintains a stack, which records the
structure of a portion of the text. This stack is a list of
strings and Trees that collectively cover a portion of
the text. For example, while parsing the sentence "the dog saw
the man" with a typical grammar, ``ShiftReduceParser`` will produce
the following stack, which covers "the dog saw"::
[(NP: (Det: 'the') (N: 'dog')), (V: 'saw')]
``ShiftReduceParser`` attempts to extend the stack to cover the
entire text, and to combine the stack elements into a single tree,
producing a complete parse for the sentence.
Initially, the stack is empty. It is extended to cover the text,
from left to right, by repeatedly applying two operations:
- "shift" moves a token from the beginning of the text to the
end of the stack.
- "reduce" uses a CFG production to combine the rightmost stack
elements into a single Tree.
Often, more than one operation can be performed on a given stack.
In this case, ``ShiftReduceParser`` uses the following heuristics
to decide which operation to perform:
- Only shift if no reductions are available.
- If multiple reductions are available, then apply the reduction
whose CFG production is listed earliest in the grammar.
Note that these heuristics are not guaranteed to choose an
operation that leads to a parse of the text. Also, if multiple
parses exists, ``ShiftReduceParser`` will return at most one of
them.
:see: ``nltk.grammar``
r c �J � || _ || _ | � � � dS )a�
Create a new ``ShiftReduceParser``, that uses ``grammar`` to
parse texts.
:type grammar: Grammar
:param grammar: The grammar used to parse texts.
:type trace: int
:param trace: The level of tracing that should be used when
parsing a text. ``0`` will generate no tracing output;
and higher numbers will produce more verbose tracing
output.
N)�_grammar�_trace�_check_grammar)�self�grammar�traces �8/usr/lib/python3/dist-packages/nltk/parse/shiftreduce.py�__init__zShiftReduceParser.__init__; s* � � ��
������������ c � � | j S �N�r �r s r r
zShiftReduceParser.grammarL s
� ��}�r c # � K � t |� � }| j � |� � g }|}| j r;t dd� |� � z � � | � ||� � t |� � dk rV| � ||� � | � ||� � r | � ||� � �t |� � dk �Vt |� � dk rS|d �
� � | j � � � � � � k r|d V � d S d S d S )Nz
Parsing %r� r � )
�listr �check_coverager
�print�join�_trace_stack�len�_shift�_reduce�label�start�symbol)r �tokens�stack�remaining_texts r �parsezShiftReduceParser.parseO sE � � � ��f�����
�$�$�V�,�,�,� ���� �;� 5��,����&�!1�!1�1�2�2�2����e�^�4�4�4� �.�!�!�A�%�%��K�K��~�.�.�.��,�,�u�n�5�5�
�� �,�,�u�n�5�5�
� �.�!�!�A�%�%� �u�:�:��?�?��Q�x�~�~���4�=�#6�#6�#8�#8�#?�#?�#A�#A�A�A��A�h������ �?�A�Ar c � � |� |d � � |� |d � � | j r| � ||� � dS dS )a�
Move a token from the beginning of ``remaining_text`` to the
end of ``stack``.
:type stack: list(str and Tree)
:param stack: A list of strings and Trees, encoding
the structure of the text that has been parsed so far.
:type remaining_text: list(str)
:param remaining_text: The portion of the text that is not yet
covered by ``stack``.
:rtype: None
r N)�append�remover
�_trace_shift�r r% r&