o
    h                     @   s   d Z ddlZddlmZmZ ddlmZmZmZ dZ	dddZ
dd	 Zdd
dZdd ZG dd deZdd ZedkrAe  dS dS )z}
A converter from constituency trees to dependency trees using CoreNLP's UniversalEnglish converter.

ONLY works on English.
    N)DependencyConverterRequestDependencyConverterResponse)send_request
build_treeJavaProtobufContextz8edu.stanford.nlp.trees.ProcessDependencyConverterRequestc                 C   s   t | tt|dS )N	classpath)r   r   CONVERTER_JAVA)requestr    r   ]/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/server/dependency_converter.pysend_converter_request   s   r   c                 C   s*   t  }| jD ]}|jt|jd q|S )zI
    Request format is simple: one tree per sentence in the document
    N)r   	sentencestreesappendr   constituency)docr
   sentencer   r   r   build_request   s   
r   c                 C   s"   t | }t||d}t| | dS )zu
    Convert the constituency trees in the document,
    then attach the resulting dependencies to the sentences
    r   N)r   r   attach_dependencies)r   r   r
   responser   r   r   process_doc   s   r   c                 C   s  t | jt |jkrtdt | jt |jf tt| j|jD ]\}\}}|j}t |jt |jkrCtd|t |jt |jf t |jt |j	d kr`td|t |jd t |j	f t
tdt |jd }t
 }|j	D ]*}|j|v rtd|j|f ||j |j|j|jd  _|j|j|jd  _qr|| }	t |	dksJ |	D ]}
d|j|
d  _d|j|
d  _q|  q!d S )Nz-Sent %d sentences but got back %d conversionszUSentence %d of the conversion should have %d words but got back %d nodes in the graph   zsSentence %d of the conversion should have %d edges (one per word, plus the root) but got back %d edges in the graphz&Found two parents of %d in sentence %dr   root)lenr   conversions
ValueError	enumeratezipgraphwordsnodeedgesetrangetargetaddsourceheaddepdeprelbuild_dependencies)r   r   sent_idxr   
conversionr   expected_nodestargetsr"   rootsr   r   r   r   r   "   s.   "


r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )DependencyConverterz
    Context window for the dependency converter

    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 N)superr1   __init__r   r	   )selfr   	__class__r   r   r4   H   s   zDependencyConverter.__init__c                 C   s    t |}| |}t|| |S )zl
        Converts a constituency tree to dependency trees for each of the sentences in the document
        )r   process_requestr   )r5   r   r
   r   r   r   r   processK   s   

zDependencyConverter.processr2   )__name__
__module____qualname____doc__r4   r9   __classcell__r   r   r6   r   r1   A   s    r1   c                  C   s   t jddd} | d}td| t|dd td| | d}td| tdd}|| td| W d    d S 1 sGw   Y  d S )	Nenztokenize,pos,constituency)
processorszI like blue antennae.z{:C}z
$CLASSPATHr   zAnd I cannot lie.)stanzaPipelineprintformatr   r1   r9   )nlpr   	converterr   r   r   mainT   s   
"rG   __main__r2   )r=   rA   stanza.protobufr   r   $stanza.server.java_protobuf_requestsr   r   r   r	   r   r   r   r   r1   rG   r:   r   r   r   r   <module>   s    

		
