
    zIf'                         d 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	m
Z
mZ  G d dej                  Z G d	 d
ej                  Z G d dej                  ZdS )z
Tests for stack decoder
    N)defaultdict)log)PhraseTableStackDecoder)_Hypothesis_Stackc                   d    e Zd Zd Zd Zd Zd Zd Zd Zd Z	e
d             Ze
d	             Zd
S )TestStackDecoderc                    t                                           }t          |d           }d}|                    |          }|                     |d         dg           |                     |d         dg           |                     |d         dg           |                     |d         ddg           |                     |d                    |                     |d         dg           d S )	Nmy
hovercraftisfullofeelsr                     )r
   create_fake_phrase_tabler   find_all_src_phrasesassertEqualassertFalse)selfphrase_tablestack_decodersentencesrc_phrase_spanss        _/var/www/piapp/venv/lib/python3.11/site-packages/nltk/test/unit/translate/test_stack_decoder.pytest_find_all_src_phrasesz*TestStackDecoder.test_find_all_src_phrases   s    '@@BB$\488C )==hGG 	)!,qc222)!,qc222)!,qc222)!,q!f555)!,---)!,qc22222    c                     t          d d           }d|_        t                      }d|_        |                    |d          }t          |j                  dz  }|                     ||           d S )N      ?r   r      
   r   )r   distortion_factorr   src_phrase_spandistortion_scorer   r   )r   r   
hypothesisscoreexpected_scores        r"   test_distortion_scorez&TestStackDecoder.test_distortion_score&   st    $T400*-' ]]
%+
" ..z7CC ]<==G/////r$   c                     t          d d           }d|_        t                      }|                    |d          }|                     |d           d S )Nr&   r(           )r   r+   r   r-   r   )r   r   r.   r/   s       r"   (test_distortion_score_of_first_expansionz9TestStackDecoder.test_distortion_score_of_first_expansion4   sW    $T400*-' ]]
 ..z7CC 	$$$$$r$   c                    t                                           }t                                           }t          ||          }d}|                    |          }|                     |d         d         |                    d          d         j        |                    d          z              |                     |d         d         |                    d          d         j        |                    d          z              d S )Nr   r   r   r   r   r   r   )	r
   r   create_fake_language_modelr   compute_future_scoresr   translations_forlog_probprobabilityr   r   language_modelr   r    future_scoress         r"   test_compute_future_costsz*TestStackDecoder.test_compute_future_costsA   s   '@@BB)DDFF$\>BBC &;;HEE 	!Q--o>>qAJ ,,_==>	
 	
 	
 	!Q--.BCCAFO ,,-ABBC	
 	
 	
 	
 	
r$   c                 2   t                                           }t                                           }t          ||          }d}|                    |          }|                     |d         d         |d         d         |d         d         z              d S )Nr   r   r   r   )r
   r   r8   r   r9   r   r=   s         r"   9test_compute_future_costs_for_phrases_not_in_phrase_tablezJTestStackDecoder.test_compute_future_costs_for_phrases_not_in_phrase_table[   s    '@@BB)DDFF$\>BBC &;;HEE 	!Q!Q-"21"55	
 	
 	
 	
 	
r$   c                     t                      }d |_        t          d           }d|d         d<   d|d         d<   t          d d           }|                    ||d          }|                     |d	           d S )
Nc                 
    ddgS )Nr   r   )r   r)    _s    r"   <lambda>z4TestStackDecoder.test_future_score.<locals>.<lambda>n       662B r$   c                  *    t          t                    S N)r   floatrF   r$   r"   rI   z4TestStackDecoder.test_future_score.<locals>.<lambda>o   s    U1C1C r$   皙?r   r   r&   r   r)   ?)r   untranslated_spansr   r   future_scorer   )r   r.   future_score_tabler   rQ   s        r"   test_future_scorez"TestStackDecoder.test_future_scorek   s     ]]
(B(B
%()C)CDD#&1a #&1a $T400 %11*>PRSTT 	y11111r$   c                     t                      }d |_        ddgdgg dgg dg dgg}t          j        ||          }|                     |g d           d S )	Nc                 
    ddgS )NrE   )r   r   rF   rG   s    r"   rI   z5TestStackDecoder.test_valid_phrases.<locals>.<lambda>~   rJ   r$   r   r   r   r   )r   r      rV   )r   r   r   r   r'   )r   r   )r   r   )r   rP   r   valid_phrasesr   )r   r.   all_phrases_fromphrase_spanss       r"   test_valid_phrasesz#TestStackDecoder.test_valid_phrasesz   ss     ]]
(B(B
%FQCaS)))R!E $12BJOO 	'O'O'OPPPPPr$   c                     t                      } |                     ddd           |                     ddd           |                     ddd           |                     ddd           |                     ddd	           |                     d
dd           |                     ddd	           |                     ddd	           |                     ddd	           |                     ddd	           | S )Nr6   ) g?r7   )r^   r^   gffffff?)r   cheeser   r&   )r   r   {Gz?)r   r   r   )r^   r^   r^   )r   r   spamr   )rb   )r   add)r   s    r"   r   z)TestStackDecoder.create_fake_phrase_table   s   "}}%555-x===)8S999%---%---4888/sCCC/3???E3///E3///r$   c                  l   t          d           t          d          d<   t          d          d<   t          d          d<   t          d          d<   t          d          d<   t          d          d<   t          d	          d
<    t          dt          fdfdi                      } | S )Nc                      dS )Ng     8rF   rF   r$   r"   rI   z=TestStackDecoder.create_fake_language_model.<locals>.<lambda>   s    F r$   皙?)r   r6   r`   )r   )r   rc   333333?r7   r^   r<   c                     |         S rL   rF   )rH   phraselanguage_probs     r"   rI   z=TestStackDecoder.create_fake_language_model.<locals>.<lambda>   s    ]6=R r$   )r   r   typeobject)r>   rk   s    @r"   r8   z+TestStackDecoder.create_fake_language_model   s     $NN33!$Sg),So&!$Sg#&s88i !$Sg#&s88i .1#hh*+
	M+R+R+R+RS
 
   r$   N)__name__
__module____qualname__r#   r1   r4   r@   rB   rS   r\   staticmethodr   r8   rF   r$   r"   r
   r
      s        3 3 3"0 0 0% % %
 
 
4
 
 
 2 2 2Q Q Q   \   \  r$   r
   c                   8    e Zd Zd Zd Zd Zd Zd Zd Zd Z	dS )	TestHypothesisc                 |    t                      }t          ddd|          }t          ddd|          }|| _        d S )Nr&   )r   rV   )helloworld)	raw_scorer,   
trg_phrasepreviousrN   rX   )andgoodbye)r   hypothesis_chain)r   rootchild
grandchilds       r"   setUpzTestHypothesis.setUp   s]    }}")	
 
 
 !")	
 
 

 !+r$   c                 h    | j                                         }|                     |g d           d S )N)ru   rv   rz   r{   )r|   translation_so_farr   )r   translations     r"   test_translation_so_farz&TestHypothesis.test_translation_so_far   s;    +>>@@ 	&J&J&JKKKKKr$   c                 v    t                      }|                                }|                     |g            d S rL   )r   r   r   )r   r.   r   s      r"   ,test_translation_so_far_for_empty_hypothesisz;TestHypothesis.test_translation_so_far_for_empty_hypothesis   s<     ]]
 !3355 	b)))))r$   c                 d    | j                                         }|                     |d           d S )Nr   )r|   total_translated_wordsr   )r   r   s     r"   test_total_translated_wordsz*TestHypothesis.test_total_translated_words   s6    !%!6!M!M!O!O 	/33333r$   c                     | j                                         }|                                 |                     |g d           d S )N)r   r   r   r   r   )r|   translated_positionssortr   )r   r   s     r"   test_translated_positionsz(TestHypothesis.test_translated_positions   sL    #4IIKK 	!!###-?????r$   c                 j    | j                             d          }|                     |g d           d S )Nr*   )rW   )r   r   )rV   r*   )r|   rP   r   )r   rP   s     r"   test_untranslated_spansz&TestHypothesis.test_untranslated_spans   s?    !2EEbII 	+-F-F-FGGGGGr$   c                 z    t                      }|                    d          }|                     |dg           d S )Nr*   )r   r*   )r   rP   r   )r   r.   rP   s      r"   ,test_untranslated_spans_for_empty_hypothesisz;TestHypothesis.test_untranslated_spans_for_empty_hypothesis   sB     ]]
 (::2>> 	+gY77777r$   N)
rn   ro   rp   r   r   r   r   r   r   r   rF   r$   r"   rs   rs      s        + + + L L L* * *4 4 4@ @ @H H H8 8 8 8 8r$   rs   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )	TestStackc                 f   t          d          }t          d          }|                    t          d                     |                    |           |                    t          d                     |                    t          d                     |                     ||v            d S )Nr   ra   g?rg   rh   r   r   pushr   r   stackpoor_hypothesiss      r"   7test_push_bumps_off_worst_hypothesis_when_stack_is_fullzATestStack.test_push_bumps_off_worst_hypothesis_when_stack_is_full   s    q		%d++ 	

;s##$$$

?###

;s##$$$

;s##$$$ 	E122222r$   c                 V   t          dd          }t          d          }t          d          }|                    |           |                    |           |                    t          d                     |                     ||v            |                     ||v            d S )Nr   r&   ra   g;On?rO   r   )r   r   r   worse_hypothesiss       r"   ;test_push_removes_hypotheses_that_fall_below_beam_thresholdzETestStack.test_push_removes_hypotheses_that_fall_below_beam_threshold   s    q#%d++&u-- 	

?###

#$$$

;s##$$$ 	E1222)U233333r$   c                     t          dd          }t          d          }|                    t          d                     |                    |           |                     ||v            d S )Nr   r&   ra   rO   r   r   s      r"   Atest_push_does_not_add_hypothesis_that_falls_below_beam_thresholdzKTestStack.test_push_does_not_add_hypothesis_that_falls_below_beam_threshold  sk    q#%d++ 	

;s##$$$

?### 	E122222r$   c                 D   t          d          }t          d          }|                    t          d                     |                    |           |                    t          d                     |                     |                                |           d S )Nr   gGz?r3   r&   )r   r   r   r   best)r   r   best_hypothesiss      r"   %test_best_returns_the_best_hypothesisz/TestStack.test_best_returns_the_best_hypothesis  s    q		%d++ 	

;s##$$$

?###

;s##$$$ 	77777r$   c                 t    t          d          }|                     |                                d            d S )Nr   )r   r   r   )r   r   s     r"   *test_best_returns_none_when_stack_is_emptyz4TestStack.test_best_returns_none_when_stack_is_empty!  s3    q		 	t,,,,,r$   N)rn   ro   rp   r   r   r   r   r   rF   r$   r"   r   r      s_        3 3 34 4 4
3 
3 
38 8 8- - - - -r$   r   )__doc__unittestcollectionsr   mathr   nltk.translater   r   nltk.translate.stack_decoderr   r   TestCaser
   rs   r   rF   r$   r"   <module>r      s     # # # # # #       4 4 4 4 4 4 4 4 < < < < < < < <P P P P Px( P P Pf@8 @8 @8 @8 @8X& @8 @8 @8F<- <- <- <- <-! <- <- <- <- <-r$   