o
    h                     @   st   d dl Z d dlmZmZmZ d dlmZmZmZ dZ	dd Z
dddZG d	d
 d
eZdd Zedkr8e  dS dS )    N)DependencyEnhancerRequestDocumentLanguage)send_requestadd_sentenceJavaProtobufContextz9edu.stanford.nlp.trees.ud.ProcessUniversalEnhancerRequestc                 C   s(  t |t |krtdt }|r|| n| dv r"tj|_n| dv r-tj|_ntd| d |j	}| j
|_
d}t| jD ]N\}}t|j||}|tdd |jD  }|j}	g }
d}|jD ],}|jD ]&}|d	 }|	j }||_||_|jdkr|	j }|j|_||_|j|_qiqdqC|S )
N7Should set exactly one of language and pronouns_pattern)enenglish)zhzzh-hanschinesezSorry, but language z is not supported yet.  Either set a pronouns pattern or file an issue at https://stanfordnlp.github.io/stanza suggesting a mechanism for converting this languager   c                 s   s    | ]}t |jV  qd S N)lenwords).0token r   T/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/server/ud_enhancer.py	<genexpr>   s    z)build_enhancer_request.<locals>.<genexpr>   )bool
ValueErrorr   setRelativePronounslowerr   UniversalEnglishlanguageUniversalChinesedocumenttext	enumerate	sentencesr   sentencesumtokensbasicDependenciesr   nodeaddsentenceIndexindexheadedgesourcetargetdepreldep)docr   pronouns_patternrequestrequest_doc
num_tokenssent_idxr!   request_sentencegraphnodes
word_indexr   wordr%   r*   r   r   r   build_enhancer_request	   sD   






r:   c                 C   s   t | ||}t|ttS r   )r:   r   r   ENHANCER_JAVA)r/   r   r0   r1   r   r   r   process_doc1   s   r<   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )UniversalEnhancerz
    UniversalEnhancer 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 t|t|krtd|| _|| _d S )Nr   )	superr=   __init__r   r;   r   r   r   r0   )selfr   r0   	classpath	__class__r   r   r?   <   s
   
zUniversalEnhancer.__init__c                 C   s   t || j| j}| |S r   )r:   r   r0   process_request)r@   r/   r1   r   r   r   processC   s   
zUniversalEnhancer.process)NNN)__name__
__module____qualname____doc__r?   rE   __classcell__r   r   rB   r   r=   5   s    r=   c                  C   s`   t jddd} tdd}| d}||}t|jd j W d    d S 1 s)w   Y  d S )Nr	   ztokenize,pos,lemma,depparse)
processors)r   zThis is the car that I boughtr   )stanzaPipeliner=   rE   printr!   enhancedDependencies)nlpenhancerr/   resultr   r   r   mainG   s   
"rS   __main__)NN)rL   stanza.protobufr   r   r   $stanza.server.java_protobuf_requestsr   r   r   r;   r:   r<   r=   rS   rF   r   r   r   r   <module>   s   
(	
