Ë
    \›¾jH  ã                   ó”   — 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„ Zd„ Zd	„ Z G d
„ de«      Zd„ Zedk(  r e«        yy)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                 ó,   — t        | t        t        «      S ©N)r   r   ÚTSURGEON_JAVA)Úrequests    ú`/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/stanza/server/tsurgeon.pyÚsend_tsurgeon_requestr      s   € Ü˜Ô!1´=ÓAÐAó    c                 ó”  — t        | t        «      r| f} t        «       }| D ]'  }|j                  j	                  t        |d«      «       Œ) t        d„ |D «       «      r|f}|D ]d  }t        |«      dk(  rt        d«      ‚|j                  j                  «       }|d   |_        |dd D ]  }|j                  j	                  |«       Œ Œf |S )z‚
    Build the TsurgeonRequest object

    trees: a list of trees
    operations: a list of (tregex, tsurgeon, tsurgeon, ...)
    g        c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   )Ú
isinstanceÚstr)Ú.0Úxs     r   ú	<genexpr>z build_request.<locals>.<genexpr>)   s   è ø€ Ò
2 !Œ:�aœ×Ñ
2ùs   ‚é   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"   s          r   Úbuild_requestr&      sÓ   € ô �%œÔØ�ˆäÓ€GØò 4ˆØ�‰×ÑœZ¨¨cÓ2Õ3ð4ä
Ñ
2 zÔ
2Ô2Ø �]ˆ
Øò 8ˆ	Üˆy‹>˜QÒÜÐUÓVÐVØ#×.Ñ.×2Ñ2Ó4ÐØ#,¨Q¡<ÐÔ Ø! ! "˜ò 	8ˆHØ×&Ñ&×-Ñ-¨hÕ7ñ	8ð8ð €Nr   c                 ó†   — t        | |«      }t        |«      }|j                  D �cg c]  }t        |«      d   ‘Œ c}S c c}w )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
    r   )r&   r   r   r	   )r   r   r   ÚresultÚts        r   Úprocess_treesr*   5   s:   € ô ˜E :Ó.€GÜ" 7Ó+€Fà%+§\¡\Ö2 ŒI�a‹L˜‹OÒ2Ð2ùÒ2s   ¦>c                   ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ x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.
    c                 ó@   •— t         t        | �  |t        t        «       y r   )Úsuperr,   Ú__init__r   r   )ÚselfÚ	classpathÚ	__class__s     €r   r/   zTsurgeon.__init__H   s   ø€ ÜŒh˜Ñ& yÔ2BÄMÕRr   c                 ó’   — t        ||«      }| j                  |«      }|j                  D �cg c]  }t        |«      d   ‘Œ c}S c c}w )Nr   )r&   Úprocess_requestr   r	   )r0   r   r   r   r(   r)   s         r   ÚprocesszTsurgeon.processK   s>   € Ü  zÓ2ˆØ×%Ñ% gÓ.ˆØ)/¯©Ö6 A”	˜!“˜Q“Ò6Ð6ùÒ6s   ¬Ar   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r5   Ú__classcell__)r2   s   @r   r,   r,   A   s   ø„ ñõSö7r   r,   c                  ól   — d} t        j                  | «      }d}d}t        |||f«      }t        |«       y)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(   s        r   Úmainr?   Q   s<   € ð 
g€DÜ×"Ñ" 4Ó(€Eà€FØ"€Hä˜5 6¨8Ð"4Ó5€FÜ	ˆ&…Mr   Ú__main__N)r9   Ú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?   r6   © r   r   ú<module>rF      sV   ðñõ  3Ý 6ß =ß iÓ iàO€òBòò2	3ô7Ð"ô 7ò ð ˆzÒÙ…Fð r   