Commit 30711873 authored by lintangsutawika's avatar lintangsutawika
Browse files

modify to conform to pre-commit

parent 3f090027
#!/usr/bin/python #!/usr/bin/python
import os
import re
import sys
import math
import subprocess
import xml.sax.saxutils
from typing import List, Pattern, Tuple, Union, Dict, Any, Optional
""" """
This script was adapted from the original version by hieuhoang1972 which is part of MOSES. This script was adapted from the original version by hieuhoang1972 which is part of MOSES.
...@@ -17,17 +25,13 @@ score_set(s, testid, refids, n=4): Interface with dataset.py; calculate BLEU sco ...@@ -17,17 +25,13 @@ score_set(s, testid, refids, n=4): Interface with dataset.py; calculate BLEU sco
The reason for breaking the BLEU computation into three phases cook_refs(), cook_test(), and score_cooked() is to allow the caller to calculate BLEU scores for multiple test sets as efficiently as possible. The reason for breaking the BLEU computation into three phases cook_refs(), cook_test(), and score_cooked() is to allow the caller to calculate BLEU scores for multiple test sets as efficiently as possible.
""" """
import sys, math, re, xml.sax.saxutils
import subprocess
import os
# Added to bypass NIST-style pre-processing of hyp and ref files -- wade # Added to bypass NIST-style pre-processing of hyp and ref files -- wade
nonorm = 0 nonorm = 0
preserve_case = False preserve_case = False
eff_ref_len = "shortest" eff_ref_len = "shortest"
normalize1 = [ normalize1: List[Tuple[Union[Pattern[str], str], str]] = [
("<skipped>", ""), # strip "skipped" tags ("<skipped>", ""), # strip "skipped" tags
(r"-\n", ""), # strip end-of-line hyphenation and join lines (r"-\n", ""), # strip end-of-line hyphenation and join lines
(r"\n", " "), # join lines (r"\n", " "), # join lines
...@@ -35,7 +39,7 @@ normalize1 = [ ...@@ -35,7 +39,7 @@ normalize1 = [
] ]
normalize1 = [(re.compile(pattern), replace) for (pattern, replace) in normalize1] normalize1 = [(re.compile(pattern), replace) for (pattern, replace) in normalize1]
normalize2 = [ normalize2: List[Tuple[Union[Pattern[str], str], str]] = [
( (
r"([\{-\~\[-\` -\&\(-\+\:-\@\/])", r"([\{-\~\[-\` -\&\(-\+\:-\@\/])",
r" \1 ", r" \1 ",
...@@ -74,7 +78,7 @@ def normalize(s): ...@@ -74,7 +78,7 @@ def normalize(s):
def count_ngrams(words, n=4): def count_ngrams(words, n=4):
counts = {} counts: Dict[Any, int] = {}
for k in range(1, n + 1): for k in range(1, n + 1):
for i in range(len(words) - k + 1): for i in range(len(words) - k + 1):
ngram = tuple(words[i : i + k]) ngram = tuple(words[i : i + k])
...@@ -88,7 +92,7 @@ def cook_refs(refs, n=4): ...@@ -88,7 +92,7 @@ def cook_refs(refs, n=4):
needs to know about them.""" needs to know about them."""
refs = [normalize(ref) for ref in refs] refs = [normalize(ref) for ref in refs]
maxcounts = {} maxcounts: Dict[Tuple[str], int] = {}
for ref in refs: for ref in refs:
counts = count_ngrams(ref, n) counts = count_ngrams(ref, n)
for (ngram, count) in counts.items(): for (ngram, count) in counts.items():
...@@ -101,7 +105,7 @@ def cook_test(test, item, n=4): ...@@ -101,7 +105,7 @@ def cook_test(test, item, n=4):
encapsulates everything that BLEU needs to know about it.""" encapsulates everything that BLEU needs to know about it."""
(reflens, refmaxcounts) = item (reflens, refmaxcounts) = item
test = normalize(test) test = normalize(test)
result = {} result: Dict[str, Any] = {}
result["testlen"] = len(test) result["testlen"] = len(test)
# Calculate effective reference sentence length. # Calculate effective reference sentence length.
...@@ -111,7 +115,7 @@ def cook_test(test, item, n=4): ...@@ -111,7 +115,7 @@ def cook_test(test, item, n=4):
elif eff_ref_len == "average": elif eff_ref_len == "average":
result["reflen"] = float(sum(reflens)) / len(reflens) result["reflen"] = float(sum(reflens)) / len(reflens)
elif eff_ref_len == "closest": elif eff_ref_len == "closest":
min_diff = None min_diff: Optional[int] = None
for reflen in reflens: for reflen in reflens:
if min_diff is None or abs(reflen - len(test)) < min_diff: if min_diff is None or abs(reflen - len(test)) < min_diff:
min_diff = abs(reflen - len(test)) min_diff = abs(reflen - len(test))
...@@ -128,7 +132,12 @@ def cook_test(test, item, n=4): ...@@ -128,7 +132,12 @@ def cook_test(test, item, n=4):
def score_cooked(allcomps, n=4, ground=0, smooth=1): def score_cooked(allcomps, n=4, ground=0, smooth=1):
totalcomps = {"testlen": 0, "reflen": 0, "guess": [0] * n, "correct": [0] * n} totalcomps: Dict[str, Any] = {
"testlen": 0,
"reflen": 0,
"guess": [0] * n,
"correct": [0] * n,
}
for comps in allcomps: for comps in allcomps:
for key in ["testlen", "reflen"]: for key in ["testlen", "reflen"]:
totalcomps[key] += comps[key] totalcomps[key] += comps[key]
...@@ -136,7 +145,7 @@ def score_cooked(allcomps, n=4, ground=0, smooth=1): ...@@ -136,7 +145,7 @@ def score_cooked(allcomps, n=4, ground=0, smooth=1):
for k in range(n): for k in range(n):
totalcomps[key][k] += comps[key][k] totalcomps[key][k] += comps[key][k]
logbleu = 0.0 logbleu = 0.0
all_bleus = [] all_bleus: List[float] = []
for k in range(n): for k in range(n):
correct = totalcomps["correct"][k] correct = totalcomps["correct"][k]
guess = totalcomps["guess"][k] guess = totalcomps["guess"][k]
...@@ -147,7 +156,7 @@ def score_cooked(allcomps, n=4, ground=0, smooth=1): ...@@ -147,7 +156,7 @@ def score_cooked(allcomps, n=4, ground=0, smooth=1):
guess + addsmooth + sys.float_info.min guess + addsmooth + sys.float_info.min
) )
if guess == 0: if guess == 0:
all_bleus.append(-10000000) all_bleus.append(-10000000.0)
else: else:
all_bleus.append(math.log(correct + sys.float_info.min) - math.log(guess)) all_bleus.append(math.log(correct + sys.float_info.min) - math.log(guess))
...@@ -175,8 +184,8 @@ def splitPuncts(line): ...@@ -175,8 +184,8 @@ def splitPuncts(line):
def computeMaps(predictions, goldfile): def computeMaps(predictions, goldfile):
predictionMap = {} predictionMap: Dict[str, list] = {}
goldMap = {} goldMap: Dict[str, list] = {}
gf = open(goldfile, "r") gf = open(goldfile, "r")
for row in predictions: for row in predictions:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment