o
    hLd                     @   s  d dl Z d dlZd dlZe je je je jeZeje d dlm	Z	 d dl
mZ d dlmZ d dlZd dlmZ d dlmZ d dlmZmZ d dlZd dlmZmZmZ d d	lmZmZ d/dedededee fddZdedefddZdee ddfddZ 	 			
d0dedee dede!de!dededee fddZ"	 				
d1d edee dede!de!dedefd!d"Z#d#edefd$d%Z$d&edefd'd(Z%d)edefd*d+Z&d,d- Z'e(d.kre'  dS dS )2    N)Pipeline)Semgrex)is_right_to_left)displacy)DocdisplayHTML)ListTupleAny)find_nth
round_baseFdoclanguagevisualize_xposreturnc                 C   s  |sdnd}g g }}t d}| jD ]}g g g g g f\}}	}
}}t|rmt|j}t|jD ]<}||j |	|j	 ||j
 |rO|jrO||j n||j |jdkrc|
||j  q/|
||j  q/n@|jD ]<}||j |	|j	 ||j
 |r|jr||j n||j |jdkr|
|jd  qp|
|jd  qp|rt|j||	|
||d}nt|j||	|
||d}|| q|D ]}|tj|ddd	d
d|ddd q|S )a  
    Returns a list of HTML strings representing the dependency visualizations of a given stanza document.
    One HTML string is generated per sentence of the document object. Converts the stanza document object
    to a spaCy doc object and generates HTML with displaCy.

    @param doc: a stanza document object which can be generated with an NLP pipeline.
    @param language: the two letter language code for the document e.g. "en" for English.
    @param visualize_xpos: A toggled option to use xpos tags for part-of-speech labels instead of upos.

    @return: a list of HTML strings which visualize the dependencies of the doc object.
    FTenr      )wordslemmasheadsdepstags)r   r   r   r   posdep   d      )compactword_spacingdistancearrow_spacingfine_grained)styleoptionsjupyter)spacyblank	sentencesr   lenr   reversedappendtextlemmadeprelxposuposheadidr   vocabr   render)r   r   r   USE_FINE_GRAINEDhtml_stringssentences_to_visualizenlpsentencer   r   r   r   r   sentence_lenwordstanza_to_spacy_docline r?   h/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/utils/visualization/semgrex_visualizer.pyget_sentences_html   sn   







rA   	orig_htmlc           %      C   s  i }d}d}g d}d}|  d}| d|d  | | |d d  } |jD ]`}t|jD ]=\}	}
||	 }d}|
jD ].}|j|j}}t| d|}||vr|  d	|}|  d
|}| ||| d  d }|dddd| d}| d| | | || d d  } |||< t| |d d	|| d | }t| |d d
|| d | }| ||| d  }t| |d d
|| | }| d|| d  }| || d d }| d}||d d  dd }||d ||  }d\}}||kr||
jd kr|}||
jd krHt	|
jdkrHd} |
jd j|v rH||
jd j } || |  }!|!dkr>||! d }|}n
|t
|!d  }|}d| d| d| d|dd   d	}"||" | } ||  d7  < q<q.t| ddd}#d}$t	| |#|$ kr| d|#|$  } q&| S )a  
    Modifies the HTML of a sentence's dependency visualization, highlighting words involved in the
    semgrex_sentence search queries and adding the label of the word inside of the match.


    @param orig_html: unedited HTML of a sentence's dependency visualization.
    @param semgrex_sentence: a Semgrex result object containing the matches to a provided query.
    @return: edited HTML containing the visual changes described above.
          )z#4477AAz#66CCEEz#228833z#CCBB44z#EE6677z#AA3377z#BBBBBBz-<style> .bolded{font-weight: bold;} </style>

Nr   z<textz<tspanz</tspan>zclass="displacy-word"zclass="bolded"zfill="currentColor"zfill=""zx="   )rC   rC   r   z#  <tspan class="displacy-word" dy="z
em" fill="z" x=>z
.</tspan>
z</svghaystackneedlen   )findresult	enumeratematchnodename
matchIndexr   replacer*   abstitle)%rB   semgrex_sentencetrackerDEFAULT_TSPAN_COUNTCLOSING_TSPAN_LENcolorscss_bolded_classopening_svg_end_idxqueryirQ   color	paired_dyrR   rS   match_indexstarttspan_start	tspan_endtspan_substredited_tspanprev_tspan_startprev_tspan_end
prev_tspanclosing_tspan_startup_to_new_tspanrestx_value_startx_value_endx_valueDEFAULT_DY_VALdypaired_node_leveldif	new_tspanendLENGTH_OF_END_SVGr?   r?   r@   semgrexify_htmla   s   
	



 
*^ry   edited_html_stringsc                 C   s   | D ]}t t| qdS )z/
    Renders the HTML of each HTML string.
    Nr   )rz   html_stringr?   r?   r@   render_html_strings   s   r|      Tsemgrex_queries	lang_codestart_match	end_matchr5   c                 C   s  d}t ddr}g }	|j| g|R  }
t| ||d}tt|D ]B}||kr( n;d}|
j| jD ]}|jD ]}|r;d}q5q0|sb||  krI|k r^n nt|| |
j| }t|}|		| |d7 }q |rqt
|	 W d   |	S W d   |	S 1 s|w   Y  |	S )	a  
    Visualizes the result of running Semgrex search on a document. The i-th element of
    the returned list is the HTML representation of the i-th sentence's dependency
    relationships. Only shows sentences that have a match on the Semgrex search.

    @param doc: A Stanza document object that contains dependency relationships .
    @param semgrex_queries: A list of Semgrex queries to search for in the document.
    @param lang_code: A two letter language abbreviation for the language that the Stanza document is written in.
    @param start_match: Beginning of the splice for which to display elements with.
    @param end_match: End of the splice for which to display elements with.
    @param render: A toggled option to render the HTML strings within the returned list
    @param visualize_xpos: A toggled option to use xpos tags in part-of-speech labels, defaulting to upos tags.

    @return: A list of HTML strings representing the dependency relations of the doc object.
    r   z
$CLASSPATH)	classpath)r   TFr   N)r   processrA   ranger*   rO   rQ   ry   adjust_dep_arrowsr,   r|   )r   r~   r   r   r   r5   r   matches_countsemrz   semgrex_resultsunedited_html_stringsr`   has_noner_   rQ   edited_stringr?   r?   r@   visualize_search_doc   sB   




r   r-   c           
   	   C   s8   |du rt |dd}n|}|| }	t|	||||||dS )a  
    Visualizes the result of running Semgrex search on a string. The i-th element of
    the returned list is the HTML representation of the i-th sentence's dependency
    relationships. Only shows sentences that have a match on the Semgrex search.

    @param text: The string for which Semgrex search will be run on.
    @param semgrex_queries: A list of Semgrex queries to search for in the document.
    @param lang_code: A two letter language abbreviation for the language that the Stanza document is written in.
    @param start_match: Beginning of the splice for which to display elements with.
    @param end_match: End of the splice for which to display elements with.
    @param pipe: An NLP pipeline through which the text will be processed.
    @param render: A toggled option to render the HTML strings within the returned list.
    @param visualize_xpos: A toggled option to use xpos tags for part-of-speech labeling, defaulting to upos tags

    @return: A list of HTML strings representing the dependency relations of the doc object.
    Nztokenize, pos, lemma, depparse
processors)r   r   r5   r   )r   r   )
r-   r~   r   r   r   piper5   r   r9   r   r?   r?   r@   visualize_search_str0  s   r   raw_htmlc                 C   s   d}d}d}t | ddd}| d| | |d }}|}d}t |||dt |||d}	}
|	dkra||	|
|  }|d dkrE||	d }t|}|| }|d7 }t |d|}	t |d|}
|	dks1|S )	a2  
    Default spaCy dependency visualizations have misaligned arrows. Fix arrows by aligning arrow ends and bodies
    to the word that they are directed to.

    @param raw_html: Dependency relation visualization generated HTML from displaCy
    @return: Edited HTML string with fixed arrow placements
    z<g class="displacy-arrow">z</g>   r   rI   N<)r   edit_dep_arrow)r   HTML_ARROW_BEGINNINGHTML_ARROW_ENDINGHTML_ARROW_ENDING_LENarrows_start_idx
words_htmlarrows_html
final_htmlarrow_number	start_idxend_of_class_idxarrow_sectionedited_arrow_sectionr?   r?   r@   r   Z  s:   	


r   
arrow_htmlc                 C   s  d}d}d}t | ddt | dd}}| d|| d|}}t| || | t| || | }}	t | dddt | dd	d}
}| ||
 | || }}|d|d}}||	krt||d
 }}|d dd d t| }dt| d | d d|dd  }|d dd d t||  }|d dd d t||  }dt| d | d | d d|d	d  }npt|	|d
}|d dd d t| }|d	 dd d t| }|d |d ||g|dd  }d|}d| }|d dd d t||  }|d dd d t||  }|||g|d	d  }d|}| d| | d | |
|  | d | |d  S )aZ  
    The formatting of a single displacy arrow in svg is the following:
    <g class="displacy-arrow">
        <path class="displacy-arc" id="arrow-c628889ffbf343e3848193a08606f10a-0-0" stroke-width="2px" d="M70,352.0 C70,177.0 390.0,177.0 390.0,352.0" fill="none" stroke="currentColor"/>
        <text dy="1.25em" style="font-size: 0.8em; letter-spacing: 1px">
            <textPath xlink:href="#arrow-c628889ffbf343e3848193a08606f10a-0-0" class="displacy-label" startOffset="50%" side="left" fill="currentColor" text-anchor="middle">csubj</textPath>
        </text>
        <path class="displacy-arrowhead" d="M70,354.0 L62,342.0 78,342.0" fill="currentColor"/>
    </g>

    We edit the 'd = ...' parts of the <path class ...> section to fix the arrow direction and length to round to
    the nearest 50 units, centering on each word's center. This is because the words start at x=50 and have spacing
    of 100, so each word is at an x-value that is a multiple of 50.

    @param arrow_html: Original SVG for a single displaCy arrow.
    @return: Edited SVG for the displaCy arrow, adjusting its placement
    2      zd="Mr   rC   ,fillrL   rG   )base r   Nz L)r   rN   floatsplitr   strjoin)r   WORD_SPACINGM_OFFSETARROW_PIXEL_SIZEfirst_d_idxsecond_d_idxfirst_d_cutoffsecond_d_cutoffarrow_positionarrowhead_positionfirst_fill_start_idxsecond_fill_start_idxfirst_dsecond_dfirst_d_splitsecond_d_splitcorrected_arrow_poscorrected_arrowhead_possecond_term
third_termfourth_termterms
first_termr?   r?   r@   r     s   

















r   r{   c                 C   s:   d}t | ddd}d}| d| d | | || d  S )a  
    Adds to overflow and display settings to the SVG header to visualize overflowing HTML renderings in the
    Semgrex streamlit app. Prevents Semgrex search tags from being cut off at the bottom of visualizations.

    The opening of each HTML string looks similar to this; we add to the end of the SVG header.

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="en" id="fa9446a525de4862b233007f26dbbecb-0" class="displacy" width="850" height="242.0" direction="ltr" style="max-width: none; height: 242.0px; color: #000000; background: #ffffff; font-family: Arial; direction: ltr">
    <style> .bolded{font-weight: bold;} </style>
    <text class="displacy-token" fill="currentColor" text-anchor="middle" y="182.0">
        <tspan class="bolded" fill="#66CCEE" x="50">Banning</tspan>

       <tspan class="displacy-tag" dy="2em" fill="currentColor" x="50">VERB</tspan>
      <tspan class="displacy-word" dy="2em" fill="#66CCEE" x=50>Act.</tspan>
    </text>

    @param html_string: HTML of the result of running Semgrex search on a text
    @return: Edited HTML to visualize the dependencies even in the case of overflow.
       zdirection: ltrr   r   z!overflow: visible; display: blockNz; )r   )r{   
BUFFER_LENediting_start_idxSVG_HEADER_ADDITIONr?   r?   r@   edit_html_overflow  s   
r   c                  C   s   t ddd} | d}ddg}t }|jdt|dd	 |jd
tt |dd	 |jdtddd	 |jdtddd	 | }|jt	j
d< zt||d}t|d  W dS  ty\   tdw )aK  
    IMPORTANT: For the code in this module to run, you must have corenlp and Java installed on your machine. Additionally,
    set an environment variable CLASSPATH equal to the path of your corenlp directory.

    Example: CLASSPATH=C:\Users\Alex\PycharmProjects\pythonProject\stanford-corenlp-4.5.0\stanford-corenlp-4.5.0\*
    r   ztokenize,pos,lemma,depparser   zeBanning opal removed artifact decks from the meta. Banning tennis resulted in players banning people.z{pos:NN}=object <obl {}=actionz){cpos:NOUN}=thing <obj {cpos:VERB}=actionz--doczStanza document to process.)typedefaulthelpz	--querieszSemgrex queries to search forz--lang_codezETwo letter abbreviation the document's language e.g. 'en' for Englishz--CLASSPATHz2C:\stanford-corenlp-4.5.2\stanford-corenlp-4.5.2\*zPath to your coreNLP directory	CLASSPATHr   aW  For the code in this module to run, you must have corenlp and Java installed on your machine. 
            Once installed, you can pass in the path to your corenlp directory as a command-line argument named 
            "CLASSPATH". Alternatively, set an environment variable CLASSPATH equal to the path of your corenlp 
            directory.N)r   argparseArgumentParseradd_argumentr   r
   r   
parse_argsr   osenvironr   print	TypeError)r9   r   queriesparserargsresr?   r?   r@   main*  sP   r   __main__)F)r   r}   TF)r   r}   NTF))r   r   syspathdirname__file__root_dirr,   stanza.pipeline.corer   stanza.server.semgrexr   stanza.models.common.constantr   r'   r   spacy.tokensr   IPython.displayr   r	   typingr
   r   r    stanza.utils.visualization.utilsr   r   r   boolrA   ry   r|   intr   r   r   r   r   r   __name__r?   r?   r?   r@   <module>   s    $ L 
?
*0 5
