o
    h-                     @   s   d Z ddlmZmZ ddlZddlZddlZddlmZ	 ddl
m  mZ ddlZddlZddlZddlmZ ddlT ejjejjgZdZdZd Zd	d
 ZG dd dZG dd dZdS )z*
Tests that call a running CoreNLPClient.
    )BaseHTTPRequestHandler
HTTPServerN)tree_reader)*zDChris wrote a simple sentence that he parsed with Stanford CoreNLP.
   a  
Sentence #1 (12 tokens):
Chris wrote a simple sentence that he parsed with Stanford CoreNLP.

Tokens:
[Text=Chris CharacterOffsetBegin=0 CharacterOffsetEnd=5 PartOfSpeech=NNP]
[Text=wrote CharacterOffsetBegin=6 CharacterOffsetEnd=11 PartOfSpeech=VBD]
[Text=a CharacterOffsetBegin=12 CharacterOffsetEnd=13 PartOfSpeech=DT]
[Text=simple CharacterOffsetBegin=14 CharacterOffsetEnd=20 PartOfSpeech=JJ]
[Text=sentence CharacterOffsetBegin=21 CharacterOffsetEnd=29 PartOfSpeech=NN]
[Text=that CharacterOffsetBegin=30 CharacterOffsetEnd=34 PartOfSpeech=WDT]
[Text=he CharacterOffsetBegin=35 CharacterOffsetEnd=37 PartOfSpeech=PRP]
[Text=parsed CharacterOffsetBegin=38 CharacterOffsetEnd=44 PartOfSpeech=VBD]
[Text=with CharacterOffsetBegin=45 CharacterOffsetEnd=49 PartOfSpeech=IN]
[Text=Stanford CharacterOffsetBegin=50 CharacterOffsetEnd=58 PartOfSpeech=NNP]
[Text=CoreNLP CharacterOffsetBegin=59 CharacterOffsetEnd=66 PartOfSpeech=NNP]
[Text=. CharacterOffsetBegin=66 CharacterOffsetEnd=67 PartOfSpeech=.]
c                    s*   G  fdddt }td| f|  d S )Nc                       s   e Zd Z fddZdS )z)run_webserver.<locals>.HTTPTimeoutHandlerc                    s8   t   | d | dd |   | jd d S )N   zContent-typeztext/plain; charset=utf-8HTTPMockServerTimeout)timesleepsend_responsesend_headerend_headerswfilewriteselftimeout_secs Z/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/tests/server/test_client.pydo_POST.   s
   

z1run_webserver.<locals>.HTTPTimeoutHandler.do_POSTN)__name__
__module____qualname__r   r   r   r   r   HTTPTimeoutHandler-   s    r   z	127.0.0.1)r   r   serve_forever)portr   r   r   r   r   run_webserver,   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	HTTPMockServerTimeoutContextzS For launching an HTTP server on certain port with an specified delay at responses c                 C   s   || _ || _d S N)r   r   )r   r   r   r   r   r   __init__9   s   
z%HTTPMockServerTimeoutContext.__init__c                 C   s.   t jt| j| jfd| _d| j_| j  d S )N)targetargsT)multiprocessingProcessr   r   r   pdaemonstartr   r   r   r   	__enter__=   s   z&HTTPMockServerTimeoutContext.__enter__c                 C   s   | j   d S r   )r%   	terminate)r   exc_type	exc_valueexc_tracebackr   r   r   __exit__B   s   z%HTTPMockServerTimeoutContext.__exit__N)r   r   r   __doc__r    r(   r-   r   r   r   r   r   7   s
    r   c                   @   s   e Zd Zejdddd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zejdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'S )(TestCoreNLPClientclass)scopec                 c   s"    t jddd}|V  |  dS )z Client to run tests on z&tokenize,ssplit,pos,lemma,ner,depparsestanza_main_test_server)
annotators	server_idN)corenlpCoreNLPClientstop)r   clientr   r   r   corenlp_clientF   s   z TestCoreNLPClient.corenlp_clientc                 C   s"   |   |js	J | sJ d S r   )ensure_alive	is_activeis_alive)r   r9   r   r   r   test_connectO   s   
zTestCoreNLPClient.test_connectc                 C   s^   t jddd}|t}t |jd td d ksJ W d    d S 1 s(w   Y  d S )Ntokenize,ssplithttp://localhost:9001)r3   endpointr   )r5   r6   annotateTEXTto_textsentence)r   context_clientannr   r   r   test_context_managerU   s   
""z&TestCoreNLPClient.test_context_managerc              	   C   sX   t tj tjdd}td1 sw   Y  W d   dS 1 s%w   Y  dS )z2We expect a second server on the same port to failr>   )r3   zThis should have failedN)pytestraisesr5   PermanentlyFailedExceptionr6   RuntimeError)r   duplicate_serverr   r   r   test_no_duplicate_servers[   s
   "z+TestCoreNLPClient.test_no_duplicate_serversc                 C   s.   | t}t|jd td d ksJ d S Nr   rA   )rB   rC   r5   rD   rE   r   r9   rG   r   r   r   test_annotatea   s   
$zTestCoreNLPClient.test_annotatec                 C   s8   | t}||}t|jd td d ksJ d S rO   )rB   rC   updater5   rD   rE   rP   r   r   r   test_updatef   s   

$zTestCoreNLPClient.test_updatec              	   C   sj   d}| t|}t|d dksJ |d d d dksJ |dddddddd	d
ddgiks3J d S )Nz9([ner: PERSON]+) /wrote/ /an?/ []{0,3} /sentence|article/	sentences   r   lengthzChris wrote a simple sentencer   Christextbeginend)rY   rZ   r[   1)0rV   )tokensregexrC   lenr   r9   patternmatchesr   r   r   test_tokensregexl   s"   	z"TestCoreNLPClient.test_tokensregexc              	   C   sD   d}|j t|dd}|ddddddd	d
ddd	ddgks J d S )Nz-{word:wrote} >nsubj {}=subject >obj {}=objectT)to_wordswroterU      rW   r   rX   rE      r   )rY   rZ   r[   z$subjectz$objectrE   )semgrexrC   r`   r   r   r   test_semgrex   s"   zTestCoreNLPClient.test_semgrexc                 C   sD   d}| t|}t| |dddddddddg d	igiks J d S )
NzPP < NPrT   r]   r   -   B   z3(PP (IN with)
  (NP (NNP Stanford) (NNP CoreNLP)))
zwith Stanford CoreNLP)	sentIndexcharacterOffsetBegincodepointOffsetBegincharacterOffsetEndcodepointOffsetEndmatch
spanString
namedNodes)tregexrC   printr`   r   r   r   test_tregex   s   zTestCoreNLPClient.test_tregexc              	   C   sP   t d}d}|j||d}|dddddg d	idd
ddg d	igiks&J dS )zE
        Test the results of tregex run on trees w/o parsing
        z(ROOT (S (NP (NNP Jennifer)) (VP (VBZ has) (NP (JJ blue) (NN skin)))))   (ROOT (S (NP (PRP I)) (VP (VBP like) (NP (PRP$ her) (NNS antennae)))))zVP < NP)ra   treesrT   r]   r   z*(VP (VBZ has)
  (NP (JJ blue) (NN skin)))
zhas blue skin)rl   rq   rr   rs   rU   z1(VP (VBP like)
  (NP (PRP$ her) (NNS antennae)))
zlike her antennaeN)r   
read_treesrt   )r   r9   rw   ra   rb   r   r   r   test_tregex_trees   s   
z#TestCoreNLPClient.test_tregex_treesc                 c   sX    t d }d| dt }|ot|}t|}|V  |s!J |  |d d S )Nzjava -Xmx5g -cp "z" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9001 -timeout 60000 -server_id stanza_external_server -serverProperties r   )	r8   resolve_classpathSERVER_TEST_PROPSshlexsplit
subprocessPopenr)   wait)r   corenlp_home	start_cmdexternal_server_processr   r   r   external_server_9001   s   

z&TestCoreNLPClient.external_server_9001c                 C   sR   t jddd}|jtddd}W d   n1 sw   Y  | tks'J dS )zY Test starting up an external server and accessing with a client with start_server=False Fr?   start_serverr@   tokenize,ssplit,posrY   r3   output_formatN)r5   r6   rB   rC   stripEN_GOLDr   r   external_server_clientrG   r   r   r   (test_external_server_legacy_start_server   s   z:TestCoreNLPClient.test_external_server_legacy_start_serverc                 C   s`   t d tjtjjdd}|jtddd}W d   n1 s!w   Y  | t	ks.J dS )zt Test starting up an external available server and accessing with a client with start_server=StartServer.DONT_START r   r?   r   r   rY   r   N)
r	   r
   r5   r6   StartServer
DONT_STARTrB   rC   r   r   r   r   r   r   test_external_server_available   s
   
z0TestCoreNLPClient.test_external_server_availablec              	   C   s   t tj2 tjtjjdd}|jtddd}W d   n1 s#w   Y  W d   dS W d   dS 1 s;w   Y  dS )zi Test accessing with a client with start_server=StartServer.DONT_START to an external unavailable server r?   r   r   rY   r   N)	rI   rJ   r5   AnnotationExceptionr6   r   r   rB   rC   r   r   rG   r   r   r    test_external_server_unavailable   s   "z2TestCoreNLPClient.test_external_server_unavailablec              
   C   s   t ddN td ttj* tjtjj	ddd}|j
tddd	}W d
   n1 s/w   Y  W d
   n1 s>w   Y  W d
   d
S W d
   d
S 1 sVw   Y  d
S )z Test starting up an external server with long response time (20 seconds) and accessing with a client with start_server=StartServer.DONT_START and timeout=5000i)#     r   r?   i  )r   r@   timeoutr   rY   r   N)r   r	   r
   rI   rJ   r5   TimeoutExceptionr6   r   r   rB   rC   r   r   r   r   test_external_server_timeout   s   
"z.TestCoreNLPClient.test_external_server_timeoutc                 C   st   t d tjtjjddd}|jtddd}|jdu s!J dW d   n1 s+w   Y  |	 t
ks8J dS )	zi Test starting up an external server and accessing with a client with start_server=StartServer.TRY_START r   r   r?   r   r3   r@   rY   r   NzbIf this is not None, that indicates the client started a server instead of reusing an existing one)r	   r
   r5   r6   r   	TRY_STARTrB   rC   serverr   r   r   r   r   r   ,test_external_server_try_start_with_external   s   

z>TestCoreNLPClient.test_external_server_try_start_with_externalc                 C   sX   t jt jjddd}|jtddd}W d   n1 sw   Y  | tks*J dS )zQ Test starting up a server with a client with start_server=StartServer.TRY_START r   r?   r   rY   r   N)r5   r6   r   r   rB   rC   r   r   r   r   r   r   test_external_server_try_start   s   
z0TestCoreNLPClient.test_external_server_try_startc              	   C   s   t d ttj2 tjtjjdd}|j	t
ddd}W d   n1 s(w   Y  W d   dS W d   dS 1 s@w   Y  dS )zk Test starting up an external server and accessing with a client with start_server=StartServer.FORCE_START r   r?   r   r   rY   r   N)r	   r
   rI   rJ   r5   rK   r6   r   FORCE_STARTrB   rC   r   r   r   r    test_external_server_force_start   s   
"z2TestCoreNLPClient.test_external_server_force_startN)r   r   r   rI   fixturer9   r=   rH   rN   rQ   rS   rc   ri   rv   ry   r   r   r   r   r   r   r   r   r   r   r   r   r/   E   s*    



r/   )r.   http.serverr   r   r#   rI   requestsstanza.serverr   r5   stanza.server.clientr8   r|   r~   r	   stanza.models.constituencyr   stanza.testsmarktravis
pytestmarkrC   MAX_REQUEST_ATTEMPTSr   r   r   r   r/   r   r   r   r   <module>   s*    