-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevalution_utils.py
More file actions
151 lines (119 loc) · 4.95 KB
/
evalution_utils.py
File metadata and controls
151 lines (119 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Utility for evaluating various tasks, e.g., translation & summarization."""
import codecs
import os
import re
import subprocess
import tensorflow as tf
from scripts import bleu
from scripts import rouge
__all__ = ["evaluate"]
def evaluate(ref_file, trans_file, metric, bpe_delimiter=None):
"""Pick a metric and evaluate depending on task."""
# BLEU scores for translation task
if metric.lower() == "bleu":
evaluation_score = _bleu(ref_file, trans_file,
bpe_delimiter=bpe_delimiter)
# ROUGE scores for summarization tasks
elif metric.lower() == "rouge":
evaluation_score = _rouge(ref_file, trans_file,
bpe_delimiter=bpe_delimiter)
elif metric.lower() == "accuracy":
evaluation_score = _accuracy(ref_file, trans_file)
else:
raise ValueError("Unknown metric %s" % metric)
return evaluation_score
def _clean(sentence, bpe_delimiter):
"""Clean and handle BPE delimiter."""
sentence = sentence.strip()
# BPE
if bpe_delimiter:
sentence = re.sub(bpe_delimiter + " ", "", sentence)
return sentence
# Follow //transconsole/localization/machine_translation/metrics/bleu_calc.py
def _bleu(ref_file, trans_file, bpe_delimiter=None):
"""Compute BLEU scores and handling BPE."""
max_order = 4
smooth = False
ref_files = [ref_file]
reference_text = []
for reference_filename in ref_files:
with codecs.getreader("utf-8")(
tf.gfile.GFile(reference_filename, "rb")) as fh:
reference_text.append(fh.readlines())
per_segment_references = []
for references in zip(*reference_text):
reference_list = []
for reference in references:
reference = _clean(reference, bpe_delimiter)
reference_list.append(reference.split(" "))
per_segment_references.append(reference_list)
translations = []
with codecs.getreader("utf-8")(tf.gfile.GFile(trans_file, "rb")) as fh:
for line in fh:
line = _clean(line, bpe_delimiter)
translations.append(line.split(" "))
# bleu_score, precisions, bp, ratio, translation_length, reference_length
bleu_score, _, _, _, _, _ = bleu.compute_bleu(
per_segment_references, translations, max_order, smooth)
return 100 * bleu_score
def _rouge(ref_file, summarization_file, bpe_delimiter=None):
"""Compute ROUGE scores and handling BPE."""
references = []
with codecs.getreader("utf-8")(tf.gfile.GFile(ref_file, "rb")) as fh:
for line in fh:
references.append(_clean(line, bpe_delimiter))
hypotheses = []
with codecs.getreader("utf-8")(
tf.gfile.GFile(summarization_file, "rb")) as fh:
for line in fh:
hypotheses.append(_clean(line, bpe_delimiter))
rouge_score_map = rouge.rouge(hypotheses, references)
return 100 * rouge_score_map["rouge_l/f_score"]
def _accuracy(label_file, pred_file):
"""Compute accuracy, each line contains a label."""
with codecs.getreader("utf-8")(tf.gfile.GFile(label_file, "rb")) as label_fh:
with codecs.getreader("utf-8")(tf.gfile.GFile(pred_file, "rb")) as pred_fh:
count = 0.0
match = 0.0
for label in label_fh:
label = label.strip()
pred = pred_fh.readline().strip()
if label == pred:
match += 1
count += 1
return 100 * match / count
def _moses_bleu(multi_bleu_script, tgt_test, trans_file, bpe_delimiter=None):
"""Compute BLEU scores using Moses multi-bleu.perl script."""
# BPE
if bpe_delimiter:
debpe_tgt_test = tgt_test + ".debpe"
if not os.path.exists(debpe_tgt_test):
# TODO(thangluong): not use shell=True, can be a security hazard
subprocess.call("cp %s %s" % (tgt_test, debpe_tgt_test), shell=True)
subprocess.call("sed s/%s //g %s" % (bpe_delimiter, debpe_tgt_test),
shell=True)
tgt_test = debpe_tgt_test
cmd = "%s %s < %s" % (multi_bleu_script, tgt_test, trans_file)
# subprocess
bleu_output = subprocess.check_output(cmd, shell=True)
# extract BLEU score
m = re.search("BLEU = (.+?),", bleu_output)
bleu_score = float(m.group(1))
return bleu_score
if __name__ == "__main__":
score = evaluate("nmt_data/tst2013.en", "nmt_output", "bleu", bpe_delimiter=None)
print("bleu:{}".format(score))