o
    h                     @   sv   d Z ddlmZ ddlZddlZddlmZ ddlmZ dd Zdd	 Z	d
d Z
dd Zdd Zedkr9e  dS dS )zL
Functions to visualize dependency relations in texts and Stanza documents 
    )is_right_to_leftN)displacy)Docc              	   C   sj  ddddddd}t d}g }| jD ]}g g g g g f\}}}}	}
t|rat|j}t|jD ]0}||j ||j	 |	|j
 |
|j |jd	krW|||j  q/|||j  q/n4|jD ]0}||j ||j	 |	|j
 |
|j |jd	kr||jd
  qd||jd
  qdt|j||||	|
d}|| q|D ]
}tj|d|d qdS )z
    Takes in a Document and visualizes it using displacy.

    The document to visualize must be from the stanza pipeline.

    right-to-left languages such as Arabic are displayed right-to-left based on the language code
    Tz#09a3d5whiteZ   zSource Sans Pro   )compactbgcolordistancefontarrow_spacingenr      )wordslemmasheadsdepsposdep)styleoptionsN)spacyblank	sentencesr   lenr   reversedappendtextlemmadepreluposheadidr   vocabr   render)doclanguagevisualization_optionsnlpsentences_to_visualizesentencer   r   r   r   tagssent_lenworddocument_resultline r1   n/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/utils/visualization/dependency_visualization.pyvisualize_doc   s>   






r3   c                 C   s   || }t || dS )a  
    Takes a string and visualizes it using displacy.

    The string is processed using the stanza pipeline and its
    dependencies are formatted into a spaCy doc object for easy
    visualization. Accepts valid stanza (UD) pipelines as the pipeline
    argument. Must supply the stanza pipeline code (the two-letter
    abbreviation of the language, such as 'en' for English. Must also
    supply the stanza pipeline object as the third argument.
    Nr3   )r   pipeline_codepiper&   r1   r1   r2   visualize_str9   s   r7   c                 C   s   | D ]}t || qdS )a  
    Takes in a list of Stanza document objects and a language code (ex: 'en' for English) and visualizes the
    dependency relationships within each document.

    This function uses spaCy visualizations. See the visualize_doc function for more details.
    Nr4   )docs	lang_coder&   r1   r1   r2   visualize_docsH   s   r:   c                 C   s(   t j|dd}| D ]}t||| q	dS )a  
    Takes a language code (ex: 'en' for English) and a list of strings to process and visualizes the
    dependency relationships in each text.

    This function loads the Stanza pipeline for the given language and uses it to visualize all of the strings provided.
    ztokenize,pos,lemma,depparse)
processorsN)stanzaPipeliner7   )textsr9   r6   r   r1   r1   r2   visualize_stringsS   s   r?   c                  C   s8   g d} ddg}dg}t | d t |d t |d d S )N)u~   برلين ترفض حصول شركة اميركية على رخصة تصنيع دبابة "ليوبارد" الالمانيةu$   هل بإمكاني مساعدتك؟u   أراك في مابعدu   لحظة من فضلكzThis is a sentence.zWBarack Obama was born in Hawaii. He was elected President of the United States in 2008.u'   中国是一个很有意思的国家。arr   zh)r?   )
ar_strings
en_strings
zh_stringsr1   r1   r2   main_   s   

rE   __main__)__doc__stanza.models.common.constantr   r<   r   r   spacy.tokensr   r3   r7   r:   r?   rE   __name__r1   r1   r1   r2   <module>   s    -
