o
    hH                     @   s   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ dZdd Zd	d
 Zdd ZG dd deZdd ZedkrEe  dS dS )a?  Invokes the Java tsurgeon on a list of trees

Included with CoreNLP is a mechanism for modifying trees based on
existing patterns within a tree.  The patterns are found using tregex:

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/tregex/TregexPattern.html

The modifications are then performed using tsurgeon:

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/tregex/tsurgeon/Tsurgeon.html

This module accepts Tree objects as produced by the conparser and
returns the modified trees that result from one or more tsurgeon
operations.
    )tree_reader)Tree)TsurgeonRequestTsurgeonResponse)send_request
build_tree	from_treeJavaProtobufContextz=edu.stanford.nlp.trees.tregex.tsurgeon.ProcessTsurgeonRequestc                 C   s   t | ttS N)r   r   TSURGEON_JAVA)request r   Q/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/server/tsurgeon.pysend_tsurgeon_request   s   r   c                 C   s   t | tr| f} t }| D ]}|jt|d qtdd |D r%|f}|D ]%}t|dkr3td|j	
 }|d |_|dd D ]}|j| qCq'|S )z
    Build the TsurgeonRequest object

    trees: a list of trees
    operations: a list of (tregex, tsurgeon, tsurgeon, ...)
    g        c                 s   s    | ]}t |tV  qd S r
   )
isinstancestr).0xr   r   r   	<genexpr>)   s    z build_request.<locals>.<genexpr>   z6Expected [tregex, tsurgeon, ...] but just got a tregexr   N)r   r   r   treesappendr   alllen
ValueError
operationsaddtregextsurgeon)r   r   r   tree	operationoperation_requestr   r   r   r   build_request   s    


r"   c                 G   s"   t | |}t|}dd |jD S )z
    Returns the result of processing the given tsurgeon operations on the given trees

    Returns a list of modified trees, eg, the result is already processed
    c                 S      g | ]}t |d  qS r   r   r   tr   r   r   
<listcomp>>       z!process_trees.<locals>.<listcomp>)r"   r   r   )r   r   r   resultr   r   r   process_trees5   s   
r+   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )Tsurgeonz
    Tsurgeon context window

    This is a context window which keeps a process open.  Should allow
    for multiple requests without launching new java processes each time.
    Nc                    s   t t| |tt d S r
   )superr,   __init__r   r   )self	classpath	__class__r   r   r.   H   s   zTsurgeon.__init__c                 G   s$   t ||}| |}dd |jD S )Nc                 S   r#   r$   r%   r&   r   r   r   r(   N   r)   z$Tsurgeon.process.<locals>.<listcomp>)r"   process_requestr   )r/   r   r   r   r*   r   r   r   processK   s   

zTsurgeon.processr
   )__name__
__module____qualname____doc__r.   r4   __classcell__r   r   r1   r   r,   A   s    r,   c                  C   s0   d} t | }d}d}t|||f}t| dS )z7
    A small demonstration of a tsurgeon operation
    z[( (SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))zWP=wpzrelabel wp WWWPPPN)r   
read_treesr+   print)textr   r   r   r*   r   r   r   mainQ   s   
r=   __main__N)r8   stanza.models.constituencyr   %stanza.models.constituency.parse_treer   stanza.protobufr   r   $stanza.server.java_protobuf_requestsr   r   r   r	   r   r   r"   r+   r,   r=   r5   r   r   r   r   <module>   s    
