Skip to content

Commit 98f3b3b

Browse files
committed
addressing reviews + fixing lint errors
1 parent 81bbdbb commit 98f3b3b

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

neuralmonkey/evaluators/chrf.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def score_instance(self,
3737
reference: List[str]) -> float:
3838
hyp_joined = " ".join(hypothesis)
3939
hyp_chars = [x for x in list(hyp_joined) if x not in self.ignored]
40-
hyp_ngrams = self._get_ngrams(hyp_chars, self.n)
40+
hyp_ngrams = _get_ngrams(hyp_chars, self.n)
4141

4242
ref_joined = " ".join(reference)
4343
ref_chars = [x for x in list(ref_joined) if x not in self.ignored]
44-
ref_ngrams = self._get_ngrams(ref_chars, self.n)
44+
ref_ngrams = _get_ngrams(ref_chars, self.n)
4545

4646
if not hyp_chars or not ref_chars:
4747
if "".join(hyp_chars) == "".join(ref_chars):
@@ -69,7 +69,7 @@ def chr_r(self, hyp_ngrams: NGramDicts, ref_ngrams: NGramDicts) -> float:
6969
ref_count, hyp_ngrams[m - 1][ngr])
7070
return np.mean(np.divide(
7171
count_matched, count_all, out=np.ones_like(count_all),
72-
where=(count_all!=0)))
72+
where=(count_all != 0)))
7373

7474
def chr_p(self, hyp_ngrams: NGramDicts, ref_ngrams: NGramDicts) -> float:
7575
count_all = np.zeros(self.n)
@@ -83,18 +83,18 @@ def chr_p(self, hyp_ngrams: NGramDicts, ref_ngrams: NGramDicts) -> float:
8383
hyp_count, ref_ngrams[m - 1][ngr])
8484
return np.mean(np.divide(
8585
count_matched, count_all, out=np.ones_like(count_all),
86-
where=(count_all!=0)))
87-
88-
def _get_ngrams(self, tokens: List[str], n: int) -> NGramDicts:
89-
ngr_dicts = []
90-
for m in range(1, n + 1):
91-
ngr_dict = {} # type: Dict[str, int]
92-
# if m > len(tokens), return an empty dict
93-
for i in range(m, len(tokens) + 1):
94-
ngr = "".join(tokens[i - m:i])
95-
ngr_dict[ngr] = ngr_dict.setdefault(ngr, 0) + 1
96-
ngr_dicts.append(ngr_dict)
97-
return ngr_dicts
86+
where=(count_all != 0)))
87+
88+
89+
def _get_ngrams(tokens: List[str], n: int) -> NGramDicts:
90+
ngr_dicts = []
91+
for m in range(1, n + 1):
92+
ngr_dict = {} # type: Dict[str, int]
93+
for i in range(m, len(tokens) + 1):
94+
ngr = "".join(tokens[i - m:i])
95+
ngr_dict[ngr] = ngr_dict.setdefault(ngr, 0) + 1
96+
ngr_dicts.append(ngr_dict)
97+
return ngr_dicts
9898

9999

100100
# pylint: disable=invalid-name

neuralmonkey/tests/test_chrf.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,21 @@
33

44
import unittest
55

6-
from neuralmonkey.evaluators.chrf import ChrFEvaluator
6+
from neuralmonkey.evaluators.chrf import ChrFEvaluator, _get_ngrams
7+
from neuralmonkey.tests.test_bleu import DECODED, REFERENCE
78

89

9-
CORPUS_DECODED = [
10-
"colorful thoughts furiously sleep",
11-
"little piglet slept all night",
12-
"working working working working working be be be be be be be",
13-
"ich bin walrus",
14-
"walrus for präsident"
15-
]
16-
17-
CORPUS_REFERENCE = [
18-
"the colorless ideas slept furiously",
19-
"pooh slept all night",
20-
"working class hero is something to be",
21-
"I am the working class walrus",
22-
"walrus for president"
23-
]
24-
2510
TOKENS = ["a", "b", "a"]
2611
NGRAMS = [
27-
{"a": 2, "b" : 1},
28-
{"ab": 1, "ba" : 1},
29-
{"aba" : 1},
12+
{"a": 2, "b": 1},
13+
{"ab": 1, "ba": 1},
14+
{"aba": 1},
3015
{}]
31-
32-
33-
DECODED = [d.split() for d in CORPUS_DECODED]
34-
REFERENCE = [r.split() for r in CORPUS_REFERENCE]
3516

3617
FUNC = ChrFEvaluator()
3718
FUNC_P = FUNC.chr_p
3819
FUNC_R = FUNC.chr_r
39-
FUNC_NGRAMS = FUNC._get_ngrams
20+
4021

4122
class TestChrF(unittest.TestCase):
4223

@@ -63,10 +44,11 @@ def test_chrf(self):
6344

6445
def test_get_ngrams(self):
6546
tokens = ["a", "b", "a"]
66-
ngrams_out = FUNC_NGRAMS(tokens, 4)
47+
ngrams_out = _get_ngrams(tokens, 4)
6748
self.assertEqual(len(ngrams_out), 4)
6849
for i, _ in enumerate(NGRAMS):
6950
self.assertDictEqual(ngrams_out[i], NGRAMS[i])
7051

52+
7153
if __name__ == "__main__":
7254
unittest.main()

0 commit comments

Comments
 (0)