forked from MorvanZhou/NLP-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8dfe8a2
commit 77fdcb6
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import numpy as np | ||
from collections import Counter | ||
|
||
|
||
def BLEU(references, generated, max_grams=4, weights=None): | ||
ref_list = [ref.lower().split(" ") for ref in references] | ||
gen = generated.lower().split(" ") | ||
cpn = np.empty((max_grams,), dtype=np.float32) | ||
for n in range(1, max_grams+1): | ||
gen_gram = [" ".join(gen[i:i+n]) for i in range(0, len(gen)-n+1)] | ||
refs_gram = [[" ".join(ref[i:i+n]) for i in range(0, len(ref)-n+1)] for ref in ref_list] | ||
g_counter = Counter(gen_gram) | ||
r_counters = [Counter(ref_gram) for ref_gram in refs_gram] | ||
count_clip = 0 | ||
for k, v in g_counter.items(): | ||
count_clip += min(v, max([r.get(k, 0) for r in r_counters])) | ||
cpn[n-1] = count_clip/sum(g_counter.values()) | ||
|
||
ls = len(gen) | ||
lc = max([len(ref) for ref in ref_list]) | ||
brevity_penalty = 1 if lc > ls else np.exp(1-ls/lc) | ||
|
||
if weights is None: | ||
weights = np.ones_like(cpn) | ||
bleu = brevity_penalty * np.exp(np.mean(weights * np.log(cpn))) | ||
return bleu | ||
|
||
|
||
bleu = BLEU(["The cat is on the mat", "There is a cat on the mat"], "The cat is on the mat", 3) | ||
print(bleu) |