CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 3/Neural Machine Translation/__pycache__/bleu.cpython-36.pyc
Views: 13380
3

��?Z
�@s*dZddlZddlZdd�Zd	dd�ZdS)
a;Python implementation of BLEU and smooth-BLEU.
This module provides a Python implementation of BLEU and smooth-BLEU.
Smooth BLEU is computed following the method outlined in the paper:
Chin-Yew Lin, Franz Josef Och. ORANGE: a method for evaluating automatic
evaluation metrics for machine translation. COLING 2004.
�NcCshtj�}xZtd|d�D]H}xBtdt|�|d�D](}t||||��}||d7<q4WqW|S)aExtracts all n-grams upto a given maximum order from an input segment.
    Args:
      segment: text segment from which n-grams will be extracted.
      max_order: maximum length in tokens of the n-grams returned by this
          methods.
    Returns:
      The Counter containing all n-grams upto max_order in segment
      with a count of how many times each n-gram occurred.
    �r)�collections�Counter�range�len�tuple)�segment�	max_orderZngram_counts�order�i�ngram�r
�;/home/jovyan/work/Week 3/Neural Machine Translation/bleu.py�_get_ngramss
r�Fcsdg�}dg�}d}d}x�t||�D]�\}}	|tdd�|D��7}|t|	�7}tj�}
x|D]}|
t|��O}
q`Wt|	��}||
@}
x(|
D] }|t|�d|
|7<q�WxBtd�d�D]0}t|	�|d}|dkr�||d|7<q�Wq(Wdg�}xhtd��D]Z}|�r8||d||d||<n0||dk�r`t||�||||<nd||<�qWt|�dk�r�t�fdd�|D��}t	j
|�}nd}|dk�r�t|�|nd}|dk�r�d}n |dk�r�t	j
dd|�nd}||}||||||fS)aqComputes BLEU score of translated segments against one or more references.
    Args:
      reference_corpus: list of lists of references for each translation. Each
          reference should be tokenized into a list of tokens.
      translation_corpus: list of translations to score. Each translation
          should be tokenized into a list of tokens.
      max_order: Maximum n-gram order to use when computing BLEU score.
      smooth: Whether or not to apply Lin et al. 2004 smoothing.
    Returns:
      3-Tuple with the BLEU score, n-gram precisions, geometric mean of n-gram
      precisions and brevity penalty.
    rcss|]}t|�VqdS)N)r)�.0�rr
r
r�	<genexpr>Aszcompute_bleu.<locals>.<genexpr>rg�?gc3s |]}d�tj|�VqdS)g�?N)�math�log)r�p)r	r
rr]s)�zip�minrrrrr�float�sumr�exp)Zreference_corpusZtranslation_corpusr	�smoothZmatches_by_orderZpossible_matches_by_orderZreference_lengthZtranslation_lengthZ
references�translationZmerged_ref_ngram_counts�	referenceZtranslation_ngram_counts�overlaprr
�possible_matchesZ
precisionsrZ	p_log_sumZgeo_mean�ratio�bp�bleur
)r	r�compute_bleu-sL




 



 r$)rF)�__doc__rrrr$r
r
r
r�<module>s