Formatting

This commit is contained in:
reshinthadithyan 2021-07-04 14:02:29 +05:30
parent 4b5cd33de5
commit 1cff3f903e
2 changed files with 102 additions and 86 deletions

View File

@ -47,8 +47,7 @@ def _get_ngrams(segment, max_order):
return ngram_counts
def compute_bleu(reference_corpus, translation_corpus, max_order=4,
smooth=True):
def compute_bleu(reference_corpus, translation_corpus, max_order=4, smooth=True):
"""Computes BLEU score of translated segments against one or more references.
Args:
@ -67,8 +66,7 @@ def compute_bleu(reference_corpus, translation_corpus, max_order=4,
possible_matches_by_order = [0] * max_order
reference_length = 0
translation_length = 0
for (references, translation) in zip(reference_corpus,
translation_corpus):
for (references, translation) in zip(reference_corpus, translation_corpus):
reference_length += min(len(r) for r in references)
translation_length += len(translation)
@ -87,17 +85,19 @@ def compute_bleu(reference_corpus, translation_corpus, max_order=4,
precisions = [0] * max_order
for i in range(0, max_order):
if smooth:
precisions[i] = ((matches_by_order[i] + 1.) /
(possible_matches_by_order[i] + 1.))
precisions[i] = (matches_by_order[i] + 1.0) / (
possible_matches_by_order[i] + 1.0
)
else:
if possible_matches_by_order[i] > 0:
precisions[i] = (float(matches_by_order[i]) /
possible_matches_by_order[i])
precisions[i] = (
float(matches_by_order[i]) / possible_matches_by_order[i]
)
else:
precisions[i] = 0.0
if min(precisions) > 0:
p_log_sum = sum((1. / max_order) * math.log(p) for p in precisions)
p_log_sum = sum((1.0 / max_order) * math.log(p) for p in precisions)
geo_mean = math.exp(p_log_sum)
else:
geo_mean = 0
@ -105,14 +105,21 @@ def compute_bleu(reference_corpus, translation_corpus, max_order=4,
ratio = float(translation_length) / reference_length
if ratio > 1.0:
bp = 1.
bp = 1.0
else:
bp = math.exp(1 - 1. / ratio)
bp = math.exp(1 - 1.0 / ratio)
bleu = geo_mean * bp
print(geo_mean)
bleu_score_dict = {"bleu":bleu,"precision":precisions,"bp":bp,"ratio":ratio,"trans_len":translation_length,"ref_len":reference_length}
bleu_score_dict = {
"bleu": bleu,
"precision": precisions,
"bp": bp,
"ratio": ratio,
"trans_len": translation_length,
"ref_len": reference_length,
}
return bleu_score_dict # (bleu, precisions, bp, ratio, translation_length, reference_length)
def bleu_test_case():
"""A simple functionality test case to evaluate BLEU"""
generated = [[["a", "=", "b", "\n", "y", "=", "a", "+", "1"]]]
@ -120,6 +127,7 @@ def bleu_test_case():
score_dict = compute_bleu(generated, reference, smooth=False)
return score_dict
if __name__ == "__main__":
score_dict = bleu_test_case()
print(score_dict)

View File

@ -12,7 +12,10 @@ def compute_exact_match(references,generated)->float:
returns:
exact_match_accuracy : Float
"""
assert(len(references[0])==len(generated),"Number of Samples should be equal in References and Synthesized Outputs..")
assert (
len(references[0]) == len(generated),
"Number of Samples should be equal in References and Synthesized Outputs..",
)
exact_match_count = 0.0
for gen, ref in zip(generated, references[0]):
if gen == ref:
@ -20,6 +23,7 @@ def compute_exact_match(references,generated)->float:
exact_match_acc = exact_match_count / len(generated)
return exact_match_acc
def compute_metrics(references, generated) -> dict:
"""
Calculates various metrics and returns the calculated dict of these matrics.
@ -31,7 +35,11 @@ def compute_metrics(references,generated) -> dict:
returns:
A dicitonary with different metrics intact.
"""
metrics_dict = {} #Update as in new metrics are added over here.
metrics_dict = {
"smoothed_bleu_4": None,
"blue_4": None,
"exact_match_acc": None,
} # Update as in new metrics are computed.
metrics_dict["smoothed_bleu_4"] = compute_bleu(references, generated, smooth=True)
metrics_dict["bleu_4"] = compute_bleu(references, generated, smooth=False)
metrics_dict["exact_match_acc"] = compute_exact_match(references, generated)