o
    h1                     @   s   d dl Z d dlmZ d dlmZ d dlT e jje jjg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dd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%Zd&Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Z d1d2 Z!dS )3    N)Tree)tree_reader)*c                  C   s   t dd} |  sJ |  rJ t| jdksJ t| dks"J t d| d}| r.J | s4J t|jdks=J t|dksEJ t d|gd}| rRJ | rXJ t|jdksaJ t|d	ksiJ d S )
Nfoo)labelr   bar)r   children   z	(bar foo)bazz(baz (bar foo)))r   is_leafis_preterminallenr   str)r   r   r
    r   d/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/tests/constituency/test_parse_tree.pytest_leaf_preterminal
   s   
r   c                  C   sB   d} t | }t|d  }t|dksJ t|dksJ d S )N/((S (VP (VB Unban)) (NP (NNP Mox) (NNP Opal))))r      z#[(VB Unban), (NNP Mox), (NNP Opal)])r   
read_treeslistyield_preterminalsr   r   )texttreespreterminalsr   r   r   test_yield_preterminals   s
   
r   c                  C   s:   d} t | }|d  dksJ |d  dksJ d S )Nz5(foo) ((S (VP (VB Unban)) (NP (NNP Mox) (NNP Opal))))r   r	      )r   r   depthr   r   r   r   r   
test_depth&   s   
r   c                  C   0   d} t | }t|}g d}||ksJ dS )z
    Test getting the unique labels from a tree

    Assumes tree_reader works, which should be fine since it is tested elsewhere
    z((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?))) ((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?))))NPPPROOTSBARQSQVPWHNPN)r   r   r   get_unique_constituent_labels)r   r   labelsexpectedr   r   r   test_unique_labels,   s
   

r*   c                  C   r   )z2
    Test getting the unique tags from a tree
    Z((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))).DTINNNVBZWPN)r   r   r   get_unique_tags)r   r   tagsr)   r   r   r   test_unique_tags:   
   

r4   c                  C   r   )3
    Test getting the unique words from a tree
    r+   )?WhoinseatsitsthisN)r   r   r   get_unique_wordsr   r   wordsr)   r   r   r   test_unique_wordsG   r5   r@   c                  C   2   d} t | }t|d}g d}||ksJ dS )r6   ((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))  ((SBARQ (NP (DT this) (NN seat)) (. ?)))g      ?)r8   r9   r;   N)r   r   r   get_rare_wordsr>   r   r   r   test_rare_wordsS   
   
rD   c                  C   rA   )r6   rB   r   )r7   r:   r<   N)r   r   r   get_common_wordsr>   r   r   r   test_common_words_   rE   rG   c                  C   sl   d} t | }dgt|ksJ d} t | }dgt|ks"J d} t | }ddgt|ks4J d S )Nzj( (SBARQ-FOO (WHNP-BAR (WP Who)) (SQ#ASDF (VP=1 (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))r"   a>  ( (SBARQ-FOO (WHNP-BAR (WP Who)) (SQ#ASDF (VP=1 (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))( (SBARQ-FOO (WHNP-BAR (WP Who)) (SQ#ASDF (VP=1 (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))( (SBARQ-FOO (WHNP-BAR (WP Who)) (SQ#ASDF (VP=1 (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))z(FOO) (BAR)BARFOO)r   r   r   get_root_labelsr   r   r   r   test_root_labelsk   s   


rK   c                  C   s^   g d} g d}t | |D ]\}}t|}t|dksJ |d  }|t|ks,J qd S )N)z^((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (-NONE- in) (NP (DT this) (NN seat))))) (. ?)))z^((SBARQ (WHNP (-NONE- Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))zb((SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (-NONE- this) (-NONE- seat))))) (. ?))))zW(ROOT (SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (NP (DT this) (NN seat))))) (. ?)))zO(ROOT (SBARQ (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (NN seat))))) (. ?)))F(ROOT (SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in)))) (. ?)))r	   r   )zipr   r   r   
prune_noner   )r   r)   ter   treer   r   r   test_prune_nonez   s   
rR   c                  C   sH   d} d}t | }dd |D }t|dksJ |t|d ks"J d S )Nzf( (SBARQ-FOO (WHNP-BAR (WP Who)) (SQ#ASDF (VP=1 (VBZ sits) (PP (IN in) (NP (DT this) (- -))))) (. ?)))z[(ROOT (SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in) (NP (DT this) (- -))))) (. ?)))c                 S   s   g | ]}|  qS r   )simplify_labels.0rO   r   r   r   
<listcomp>   s    z(test_simplify_labels.<locals>.<listcomp>r	   r   r   r   r   r   r   r)   r   r   r   r   test_simplify_labels   s   
rY   c                     sT   d} d}ddi t | } fdd|D }t|dksJ |t|d ks(J d S )	NrL   zD(ROOT (FOO (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in)))) (. ?)))r#   rI   c                       g | ]}|  qS r   )remap_constituent_labelsrT   	label_mapr   r   rV          z1test_remap_constituent_labels.<locals>.<listcomp>r	   r   rW   rX   r   r\   r   test_remap_constituent_labels   s   
r_   c                     sX   d} d}dddd t | } fdd|D }t|d	ks J |t|d
 ks*J d S )NrL   I(ROOT (SBARQ (WHNP (WP unban)) (SQ (VP (VBZ mox) (PP (IN opal)))) (. ?)))unbanmoxopal)r8   r;   r9   c                    rZ   r   )remap_wordsrT   word_mapr   r   rV      r^   z0test_remap_constituent_words.<locals>.<listcomp>r	   r   rW   rX   r   re   r   test_remap_constituent_words   s   
rg   c                  C   sP   d} d}g d}t | }t|dksJ |d }||}|t|ks&J d S )NrL   r`   )ra   rb   rc   r7   r	   r   )r   r   r   replace_wordsr   )r   r)   	new_wordsr   rQ   new_treer   r   r   test_replace_words   s   

rk   c                  C   sn   d} t | }t|dgksJ d} t | }t|g dks#J d} t | }t|g dks5J d S )Nz((VP (VB Unban)))r"   r%   rL   )r!   r"   r#   r$   r%   r&   zZ((VP (VB Unban)))   (ROOT (SBARQ (WHNP (WP Who)) (SQ (VP (VBZ sits) (PP (IN in)))) (. ?))))rm   rn   rl   ro   rp   )r   r   r   get_compound_constituentsr   r   r   r   test_compound_constituents   s   


rr   c                  C   s`   d} t | }t|dksJ |d }||ksJ t | }|d }||us(J ||ks.J dS )z
    Check one tree from the actual dataset for ==

    when built with compound Open, this didn't work because of a silly bug
    a+  (ROOT (S (NP (DT The) (NNP Arizona) (NNPS Corporations) (NNP Commission)) (VP (VBD authorized) (NP (NP (DT an) (ADJP (CD 11.5)) (NN %) (NN rate) (NN increase)) (PP (IN at) (NP (NNP Tucson) (NNP Electric) (NNP Power) (NNP Co.))) (, ,) (UCP (ADJP (ADJP (RB substantially) (JJR lower)) (SBAR (IN than) (S (VP (VBN recommended) (NP (JJ last) (NN month)) (PP (IN by) (NP (DT a) (NN commission) (NN hearing) (NN officer))))))) (CC and) (NP (NP (QP (RB barely) (PDT half)) (DT the) (NN rise)) (VP (VBN sought) (PP (IN by) (NP (DT the) (NN utility)))))))) (. .)))r	   r   N)r   r   r   )r   r   rQ   trees2tree2r   r   r   test_equals   s   

ru   u  
(ROOT
 (IP
  (NP (NNP 证券法))
  (VP
   (PP
    (IN 对)
    (NP
     (DNP
      (NP
       (NP (NNP 中国))
       (NP
        (NN 证券)
        (NN 市场)))
      (DEC 的))
     (NP (NN 运作))))
   (, ，)
   (PP
    (PP
     (IN 从)
     (NP
      (NP (NN 股票))
      (NP (VV 发行) (EC 、) (VV 交易))))
    (, ，)
    (PP
     (VV 到)
     (NP
      (NP (NN 上市) (NN 公司) (NN 收购))
      (EC 、)
      (NP (NN 证券) (NN 交易所))
      (EC 、)
      (NP (NN 证券) (NN 公司))
      (EC 、)
      (NP (NN 登记) (NN 结算) (NN 机构))
      (EC 、)
      (NP (NN 交易) (NN 服务) (NN 机构))
      (EC 、)
      (NP (NN 证券业) (NN 协会))
      (EC 、)
      (NP (NN 证券) (NN 监督) (NN 管理) (NN 机构))
      (CC 和)
      (NP
       (DNP
        (NP (CP (CP (IP (VP (VV 违法))))))
        (DEC 的))
       (NP (NN 法律) (NN 责任))))))
   (ADVP (RB 都))
   (VP
    (VV 作)
    (AS 了)
    (NP
     (ADJP (JJ 详细))
     (NP (NN 规定)))))
  (. 。)))
u  
  (DNP
    (NP (CP (CP (IP (VP (ASDF
      (NP (NN 上市) (NN 公司) (NN 收购))
      (EC 、)
      (NP (NN 证券) (NN 交易所))
      (EC 、)
      (NP (NN 证券) (NN 公司))
      (EC 、)
      (NP (NN 登记) (NN 结算) (NN 机构))
      (EC 、)
      (NP (NN 交易) (NN 服务) (NN 机构))
      (EC 、)
      (NP (NN 证券业) (NN 协会))
      (EC 、)
      (NP (NN 证券) (NN 监督) (NN 管理) (NN 机构))))))))
    (DEC 的))
c                  C   s`   t t} t| dksJ | d  dksJ t t} t| dks$J | d  dks.J d S )Nr	   r      )r   r   CHINESE_LONG_LIST_TREEr   count_unary_depthWEIRD_UNARY)r   r   r   r   test_count_unaries"  s   

rz   c                  C   s<   d} d}t | }t|dksJ d|d |ksJ d S )Nr   zZ(_ROOT (_S (_VP (_VB Unban )_VB )_VP (_NP (_NNP Mox )_NNP (_NNP Opal )_NNP )_NP )_S )_ROOTr	   z{:L}r   r   r   r   formatrX   r   r   r   test_str_bracket_labels+  s
   
r}   c                  C   s`   d} t | }t|dksJ |d  sJ d} t | }t|dks&J |d  r.J d S )Nr   r	   r   z+((S (VP (VB Unban)) (NP (Mox) (NNP Opal)))))r   r   r   all_leaves_are_preterminalsr   r   r   r    test_all_leaves_are_preterminals3  s   

r   c                  C   s0   d} d}t |d }d|}|| ksJ dS )z)
    Test the latex format for trees
    z;\Tree [.S [.NP Jennifer ] [.VP has [.NP nice antennae ] ] ]zK(ROOT (S (NP (NNP Jennifer)) (VP (VBZ has) (NP (JJ nice) (NNS antennae)))))r   z{:T}N)r   r   r|   )r)   rQ   r   r   r   r   
test_latex>  s
   
r   c                  C   sh   d} t | }t|dksJ d}d|d |ksJ d}d|d |ks)J | dj| ks2J d	S )
z>
    Pretty print a couple trees - newlines & indentation
    a  (ROOT (S (VP (VB Unban)) (NP (NNP Mox) (NNP Opal)))) (ROOT (S (NP (DT The) (NNP Arizona) (NNPS Corporations) (NNP Commission)) (VP (VBD authorized) (NP (NP (DT an) (ADJP (CD 11.5)) (NN %) (NN rate) (NN increase)) (PP (IN at) (NP (NNP Tucson) (NNP Electric)))))))   z?(ROOT
  (S
    (VP (VB Unban))
    (NP (NNP Mox) (NNP Opal))))
z{:P}r   a>  (ROOT
  (S
    (NP (DT The) (NNP Arizona) (NNPS Corporations) (NNP Commission))
    (VP
      (VBD authorized)
      (NP
        (NP
          (DT an)
          (ADJP (CD 11.5))
          (NN %)
          (NN rate)
          (NN increase))
        (PP
          (IN at)
          (NP (NNP Tucson) (NNP Electric)))))))
r	   z	{:O} {:O}Nr{   )r   r   r)   r   r   r   test_pretty_printH  s   
r   c                  C   s>   d} t | }t|dksJ |d  }t|dksJ d S )Nzx(ROOT (S (NP (PRP I)) (VP (VBP want) (S (VP (TO to) (VP (VB lick) (NP (NP (NNP Jennifer) (POS 's)) (NNS antennae))))))))r	   r   zx(ROOT (S (VP (S (VP (VP (NP (NNS antennae) (NP (POS 's) (NNP Jennifer))) (VB lick)) (TO to))) (VBP want)) (NP (PRP I)))))r   r   r   reverser   )r   r   reversed_treer   r   r   test_reversel  s
   
r   )"pytest%stanza.models.constituency.parse_treer   stanza.models.constituencyr   stanza.testsmarkpipelinetravis
pytestmarkr   r   r   r*   r4   r@   rD   rG   rK   rR   rY   r_   rg   rk   rr   ru   rw   ry   rz   r}   r   r   r   r   r   r   r   r   <module>   s:    

8	
$