
    g%                         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y)z
Tests for stack decoder
    N)defaultdict)log)PhraseTableStackDecoder)_Hypothesis_Stackc                   V    e Zd Zd Zd Zd Zd Zd Zd Zd Z	e
d        Ze
d	        Zy
)TestStackDecoderc                 p   t         j                         }t        |d       }d}|j                  |      }| j	                  |d   dg       | j	                  |d   dg       | j	                  |d   dg       | j	                  |d   ddg       | j                  |d          | j	                  |d   dg       y )	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/openai/venv/lib/python3.12/site-packages/nltk/test/unit/translate/test_stack_decoder.pytest_find_all_src_phrasesz*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                     t        d d       }d|_        t               }d|_        |j	                  |d      }t        |j                        dz  }| j                  ||       y )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&   s`    $T40*-' ]
%+
" ..z7C ]<<=G/r$   c                     t        d d       }d|_        t               }|j                  |d      }| j	                  |d       y )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   sC    $T40*-' ]
 ..z7C 	$r$   c                    t         j                         }t         j                         }t        ||      }d}|j	                  |      }| j                  |d   d   |j                  d      d   j                  |j                  d      z          | j                  |d   d   |j                  d      d   j                  |j                  d      z          y )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)DDF$\>BC &;;HE 	!Q--o>qAJJ ,,_=>	
 	!Q--.BCAFOO ,,-ABC	
r$   c                     t         j                         }t         j                         }t        ||      }d}|j	                  |      }| j                  |d   d   |d   d   |d   d   z          y )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[   sz    '@@B)DDF$\>BC &;;HE 	!Q!Q-"21"55	
r$   c                     t               }d |_        t        d       }d|d   d<   d|d   d<   t        d d       }|j	                  ||d      }| j                  |d	       y )
Nc                 
    ddgS )Nr   r   )r   r)    _s    r"   <lambda>z4TestStackDecoder.test_future_score.<locals>.<lambda>n   	    662Br$   c                       t        t              S N)r   floatrF   r$   r"   rI   z4TestStackDecoder.test_future_score.<locals>.<lambda>o   s	    U1Cr$   皙?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   ss     ]
(B
%()CD#&1a #&1a $T40 %11*>PRST 	y1r$   c                     t               }d |_        ddgdgg dgg dg dgg}t        j                  ||      }| j	                  |g d       y )	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   sX     ]
(B
%FQCaS)R!E $112BJO 	'OPr$   c                     t               } | j                  ddd       | j                  ddd       | j                  ddd       | j                  ddd       | j                  ddd	       | j                  d
dd       | j                  ddd	       | j                  ddd	       | j                  ddd	       | j                  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-x=)8S9%-%-48/sC/3?E3/E3/r$   c                     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                       y)Ng     8rF   rF   r$   r"   rI   z=TestStackDecoder.create_fake_language_model.<locals>.<lambda>   s    Fr$   皙?)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=Rr$   )r   r   typeobject)r>   rk   s    @r"   r8   z+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$   N)__name__
__module____qualname__r#   r1   r4   r@   rB   rS   r\   staticmethodr   r8   rF   r$   r"   r
   r
      sN    3"0%
4
 2Q    r$   r
   c                   6    e Zd Zd Zd Zd Zd Zd Zd Zd Z	y)	TestHypothesisc                 b    t               }t        ddd|      }t        ddd|      }|| _        y )Nr&   )r   rV   )helloworld)	raw_scorer,   
trg_phrasepreviousrN   rX   )andgoodbye)r   hypothesis_chain)r   rootchild
grandchilds       r"   setUpzTestHypothesis.setUp   sA    }")	
 !")	

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

;s#$

?#

;s#$

;s#$ 	E12r$   c                    t        dd      }t        d      }t        d      }|j                  |       |j                  |       |j                  t        d             | j                  ||v        | j                  ||v        y )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   sw    q#%d+&u- 	

?#

#$

;s#$ 	E12)U23r$   c                     t        dd      }t        d      }|j                  t        d             |j                  |       | j                  ||v        y )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2r$   c                     t        d      }t        d      }|j                  t        d             |j                  |       |j                  t        d             | j                  |j	                         |       y )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#$ 	7r$   c                 Z    t        d      }| j                  |j                         d        y )Nr   )r   r   r   )r   r   s     r"   *test_best_returns_none_when_stack_is_emptyz4TestStack.test_best_returns_none_when_stack_is_empty!  s#    q	 	t,r$   N)rn   ro   rp   r   r   r   r   r   rF   r$   r"   r   r      s    34
38-r$   r   )__doc__unittestcollectionsr   mathr   nltk.translater   r   nltk.translate.stack_decoderr   r   TestCaser
   rs   r   rF   r$   r"   <module>r      sW     #  4 <Px(( Pf@8X&& @8F<-!! <-r$   