o
    h(                     @   s\   d Z ddlZddlmZ ddlmZmZ ddlmZ	 ddl
mZmZmZ G dd deZdS )	z4
Defines a base class that can be used to annotate.
    N)Process)BaseHTTPRequestHandler
HTTPServer)client)DocumentparseFromDelimitedStringwriteToDelimitedStringc                   @   sj   e Zd ZdZedd Zedd Zedd Zdd	 Zed
d Z	G dd de
ZdddZdd ZdS )	Annotatora  
    This annotator base class hosts a lightweight server that accepts
    annotation requests from CoreNLP.
    Each annotator simply defines 3 functions: requires, provides and annotate.

    This class takes care of defining appropriate endpoints to interface
    with CoreNLP.
    c                 C      t  )z9
        Name of the annotator (used by CoreNLP)
        NotImplementedErrorself r   R/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/server/annotator.pyname      zAnnotator.namec                 C   r
   )zd
        Requires has to specify all the annotations required before we
        are called.
        r   r   r   r   r   requires   s   zAnnotator.requiresc                 C   r
   )a  
        The set of annotations guaranteed to be provided when we are done.
        NOTE: that these annotations are either fully qualified Java
        class names or refer to nested classes of
        edu.stanford.nlp.ling.CoreAnnotations (as is the case below).
        r   r   r   r   r   provides#   s   zAnnotator.providesc                 C   r
   )zd
        @ann: is a protobuf annotation object.
        Actually populate @ann with tokens.
        r   )r   annr   r   r   annotate-   r   zAnnotator.annotatec              
   C   s8   d | jddd | j| jdd| jdd| jiS )zN
        Defines a Java property to define this annotator to CoreNLP.
        zcustomAnnotatorClass.{}z4edu.stanford.nlp.pipeline.GenericWebServiceAnnotatorzgeneric.endpointzhttp://{}:{}zgeneric.requires,zgeneric.provides)formatr   hostportjoinr   r   r   r   r   r   
properties4   s
   zAnnotator.propertiesc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )zAnnotator._HandlerNc                 C   s   t | ||| d S )N)r   __init__)r   requestclient_addressserverr   r   r   r   C   s   zAnnotator._Handler.__init__c                 C   s   | j ds|  j d7  _ | j dkr7dd}| tj | dd | dt| |   | j	
| dS | tj |   dS )	z/
            Handle a ping request
            /z/ping/pongzUTF-8Content-Typeztext/applicationContent-LengthN)pathendswithencodesend_response
HTTPStatusOKsend_headerlenend_headerswfilewriteBAD_REQUEST)r   msgr   r   r   do_GETF   s   

zAnnotator._Handler.do_GETc                 C   s   | j ds|  j d7  _ | j dkrkt| jd}| j|}t }t|| | j	
| t }t|| | }W d   n1 sFw   Y  | tj | dd | dt| |   | j| dS | tj |   dS )z4
            Handle an annotate request
            r!   z
/annotate/zcontent-lengthNr#   zapplication/x-protobufr$   )r%   r&   intheadersgetrfilereadr   r   	annotatorr   ioBytesIOr   getvaluer(   r)   r*   r+   r,   r-   r.   r/   r0   )r   lengthr1   docstreamr   r   r   do_POSTW   s$   




zAnnotator._Handler.do_POST)__name__
__module____qualname__r8   r   r2   r?   r   r   r   r   _Handler@   s
    rC       c                 C   s$   t |  ||| _| _| | j_dS )zH
        Launches a server endpoint to communicate with CoreNLP
        N)r   r   r   r   rC   r8   )r   r   r   r   r   r   r   u   s   
zAnnotator.__init__c                 C   sp   t | j| jf| j}|j }d}t|j|d |d d z|  W dS  t	y7   td |
  Y dS w )zj
        Runs the server using Python's simple HTTPServer.
        TODO: make this multithreaded.
        z>Serving HTTP on {host} port {port} (http://{host}:{port}/) ...r      )r   r   z&
Keyboard interrupt received, exiting.N)r   r   r   rC   socketgetsocknameprintr   serve_foreverKeyboardInterruptshutdown)r   httpdsaserve_messager   r   r   run}   s   
zAnnotator.runN)rD   rE   )r@   rA   rB   __doc__propertyr   r   r   r   r   r   rC   r   rP   r   r   r   r   r	      s    


	

5r	   )rQ   r9   multiprocessingr   http.serverr   r   httpr   r)   stanza.protobufr   r   r   r	   r   r   r   r   <module>   s    