
    Of%                         S r SSKrSSKJr  SSKJr  SSKJrJr  SSK	J
r
Jr   " S S\R                  5      r " S	 S
\R                  5      r " S S\R                  5      rg)z
Tests for stack decoder
    N)defaultdict)log)PhraseTableStackDecoder)_Hypothesis_Stackc                   ^    \ rS rSrS rS rS rS rS rS r	S r
\S	 5       r\S
 5       rSrg)TestStackDecoder   c                 p   [         R                  5       n[        US 5      nSnUR                  U5      nU R	                  US   S/5        U R	                  US   S/5        U R	                  US   S/5        U R	                  US   SS/5        U R                  US   5        U R	                  US   S/5        g )	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        M/usr/lib/python3/dist-packages/nltk/test/unit/translate/test_stack_decoder.pytest_find_all_src_phrases*TestStackDecoder.test_find_all_src_phrases   s    '@@B$\48C )==hG 	)!,qc2)!,qc2)!,qc2)!,q!f5)!,-)!,qc2    c                     [        S S 5      nSUl        [        5       nSUl        UR	                  US5      n[        UR                  5      S-  nU R                  X45        g )N      ?r   r      
   r   )r   distortion_factorr   src_phrase_spandistortion_scorer   r   )r   r    
hypothesisscoreexpected_scores        r#   test_distortion_score&TestStackDecoder.test_distortion_score&   s^    $T40*-' ]
%+
" ..z7C ]<<=G/r&   c                     [        S S 5      nSUl        [        5       nUR                  US5      nU R	                  US5        g )Nr(   r*           )r   r-   r   r/   r   )r   r    r0   r1   s       r#   (test_distortion_score_of_first_expansion9TestStackDecoder.test_distortion_score_of_first_expansion4   sC    $T40*-' ]
 ..z7C 	$r&   c                    [         R                  5       n[         R                  5       n[        X5      nSnUR	                  U5      nU R                  US   S   UR                  S5      S   R                  UR                  S5      -   5        U R                  US   S   UR                  S5      S   R                  UR                  S5      -   5        g )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_costs*TestStackDecoder.test_compute_future_costsA   s    '@@B)DDF$\BC &;;HE 	!Q--o>qAJJ ,,_=>	
 	!Q--.BCAFOO ,,-ABC	
r&   c                     [         R                  5       n[         R                  5       n[        X5      nSnUR	                  U5      nU R                  US   S   US   S   US   S   -   5        g )Nr   r   r   r   )r
   r   r<   r   r=   r   rA   s         r#   9test_compute_future_costs_for_phrases_not_in_phrase_tableJTestStackDecoder.test_compute_future_costs_for_phrases_not_in_phrase_table[   sx    '@@B)DDF$\BC &;;HE 	!Q!Q-"21"55	
r&   c                     [        5       nS Ul        [        S 5      nSUS   S'   SUS   S'   [        S S 5      nUR	                  XS5      nU R                  US	5        g )
Nc                 
    SS/$ )Nr   r   )r   r+    _s    r#   <lambda>4TestStackDecoder.test_future_score.<locals>.<lambda>n   	    662Br&   c                       [        [        5      $ N)r   floatrL   r&   r#   rO   rP   o   s	    U1Cr&   皙?r   r   r(   r   r+   ?)r   untranslated_spansr   r   future_scorer   )r   r0   future_score_tabler    rX   s        r#   test_future_score"TestStackDecoder.test_future_scorek   sp     ]
(B
%()CD#&1a #&1a $T40 %11*RST 	y1r&   c                     [        5       nS Ul        SS/S// S// SQ/ S//n[        R                  " X!5      nU R	                  U/ SQ5        g )	Nc                 
    SS/$ )NrK   )r   r   rL   rM   s    r#   rO   5TestStackDecoder.test_valid_phrases.<locals>.<lambda>~   rQ   r&   r   r   r   r   )r   r      r_   )r   r   r   r   r)   )r   r   )r   r   )r   rW   r   valid_phrasesr   )r   r0   all_phrases_fromphrase_spanss       r#   test_valid_phrases#TestStackDecoder.test_valid_phrasesz   sV     ]
(B
%FQCaS)R!E $112BO 	'OPr&   c                     [        5       n U R                  SSS5        U R                  SSS5        U R                  SSS5        U R                  SSS5        U R                  SSS	5        U R                  S
SS5        U R                  SSS	5        U R                  SSS	5        U R                  SSS	5        U R                  SSS	5        U $ )Nr:   ) g?r;   )rh   rh   gffffff?)r   cheeser   r(   )r   r   {Gz?)r   r   r   )rh   rh   rh   )r   r   spamr   )rl   )r   add)r   s    r#   r   )TestStackDecoder.create_fake_phrase_table   s    "}%5-x=)8S9%-%-48/sC/3?E3/E3/r&   c                    ^ [        S 5      m[        S5      TS'   [        S5      TS'   [        S5      TS'   [        S5      TS'   [        S5      TS'   [        S5      TS'   [        S	5      TS
'   [        S[        4SU4S j05      " 5       n U $ )Nc                      g)Ng     8rL   rL   r&   r#   rO   =TestStackDecoder.create_fake_language_model.<locals>.<lambda>   s    Fr&   皙?)r   r:   rj   )r   )r   rm   333333?r;   rh   r@   c                    > TU   $ rS   rL   )rN   phraselanguage_probs     r#   rO   rr      s
    ]6=Rr&   )r   r   typeobject)rB   rw   s    @r#   r<   +TestStackDecoder.create_fake_language_model   s     $N3!$Sg),So&!$Sg#&s8i !$Sg#&s8i .1#h*+	M+RS
  r&   rL   N)__name__
__module____qualname____firstlineno__r$   r3   r7   rD   rG   rZ   re   staticmethodr   r<   __static_attributes__rL   r&   r#   r
   r
      sN    3"0%
4
 2Q    r&   r
   c                   >    \ rS rSrS rS rS rS rS rS r	S r
S	rg
)TestHypothesis   c                 T    [        5       n[        SSSUS9n[        SSSUS9nX0l        g )Nr(   )r   r_   )helloworld)	raw_scorer.   
trg_phrasepreviousrU   ra   )andgoodbye)r   hypothesis_chain)r   rootchild
grandchilds       r#   setUpTestHypothesis.setUp   s?    }")	
 !")	

 !+r&   c                 `    U R                   R                  5       nU R                  U/ SQ5        g )N)r   r   r   r   )r   translation_so_farr   )r   translations     r#   test_translation_so_far&TestHypothesis.test_translation_so_far   s)    ++>>@ 	&JKr&   c                 \    [        5       nUR                  5       nU R                  U/ 5        g rS   )r   r   r   )r   r0   r   s      r#   ,test_translation_so_far_for_empty_hypothesis;TestHypothesis.test_translation_so_far_for_empty_hypothesis   s+     ]
 !335 	b)r&   c                 \    U R                   R                  5       nU R                  US5        g )Nr   )r   total_translated_wordsr   )r   r   s     r#   test_total_translated_words*TestHypothesis.test_total_translated_words   s*    !%!6!6!M!M!O 	/3r&   c                     U R                   R                  5       nUR                  5         U R                  U/ SQ5        g )N)r   r   r   r   r   )r   translated_positionssortr   )r   r   s     r#   test_translated_positions(TestHypothesis.test_translated_positions   s6    #44IIK 	!!#-?r&   c                 b    U R                   R                  S5      nU R                  U/ SQ5        g )Nr,   )r`   )r   r   )r_   r,   )r   rW   r   )r   rW   s     r#   test_untranslated_spans&TestHypothesis.test_untranslated_spans   s-    !22EEbI 	+-FGr&   c                 `    [        5       nUR                  S5      nU R                  US/5        g )Nr,   )r   r,   )r   rW   r   )r   r0   rW   s      r#   ,test_untranslated_spans_for_empty_hypothesis;TestHypothesis.test_untranslated_spans_for_empty_hypothesis   s1     ]
 (::2> 	+gY7r&   )r   N)r{   r|   r}   r~   r   r   r   r   r   r   r   r   rL   r&   r#   r   r      s(    + L*4@H8r&   r   c                   2    \ rS rSrS rS rS rS rS rSr	g)		TestStack   c                    [        S5      n[        S5      nUR                  [        S5      5        UR                  U5        UR                  [        S5      5        UR                  [        S5      5        U R                  X!;   5        g )Nr   rk   g?rs   rt   r   r   pushr   r   stackpoor_hypothesiss      r#   7test_push_bumps_off_worst_hypothesis_when_stack_is_fullATestStack.test_push_bumps_off_worst_hypothesis_when_stack_is_full   si    q	%d+ 	

;s#$

?#

;s#$

;s#$ 	12r&   c                    [        SS5      n[        S5      n[        S5      nUR                  U5        UR                  U5        UR                  [        S5      5        U R                  X!;   5        U R                  X1;   5        g )Nr   r(   rk   g;On?rV   r   )r   r   r   worse_hypothesiss       r#   ;test_push_removes_hypotheses_that_fall_below_beam_thresholdETestStack.test_push_removes_hypotheses_that_fall_below_beam_threshold   ss    q#%d+&u- 	

?#

#$

;s#$ 	12)23r&   c                     [        SS5      n[        S5      nUR                  [        S5      5        UR                  U5        U R                  X!;   5        g )Nr   r(   rk   rV   r   r   s      r#   Atest_push_does_not_add_hypothesis_that_falls_below_beam_thresholdKTestStack.test_push_does_not_add_hypothesis_that_falls_below_beam_threshold  sI    q#%d+ 	

;s#$

?# 	12r&   c                     [        S5      n[        S5      nUR                  [        S5      5        UR                  U5        UR                  [        S5      5        U R                  UR	                  5       U5        g )Nr   gGz?r6   r(   )r   r   r   r   best)r   r   best_hypothesiss      r#   %test_best_returns_the_best_hypothesis/TestStack.test_best_returns_the_best_hypothesis  s]    q	%d+ 	

;s#$

?#

;s#$ 	7r&   c                 Z    [        S5      nU R                  UR                  5       S 5        g )Nr   )r   r   r   )r   r   s     r#   *test_best_returns_none_when_stack_is_empty4TestStack.test_best_returns_none_when_stack_is_empty!  s#    q	 	t,r&   rL   N)
r{   r|   r}   r~   r   r   r   r   r   r   rL   r&   r#   r   r      s    34
38-r&   r   )__doc__unittestcollectionsr   mathr   nltk.translater   r   nltk.translate.stack_decoderr   r   TestCaser
   r   r   rL   r&   r#   <module>r      sW     #  4 <Px(( Pf@8X&& @8F<-!! <-r&   