-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_reader.py
More file actions
executable file
·111 lines (95 loc) · 3.04 KB
/
Copy pathdata_reader.py
File metadata and controls
executable file
·111 lines (95 loc) · 3.04 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
import csv
import pandas as pd
def read_annotated_file(path, index="index", comet=True, bertScore=True, features=False, ref=False, hter=False,
replace_hter=False, replace_comet=False):
indices = []
originals = []
translations = []
z_means = []
refs = []
comets = []
bertScores = []
hters = []
lans = []
unigrams = []
bigrams = []
grams3 = []
grams4 = []
grams5 = []
with open(path, mode="r", encoding="utf-8-sig") as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
indices.append(row[index])
originals.append(row["original"])
translations.append(row["translation"])
z_means.append(float(row["z_mean"]))
if ref:
refs.append(row["ref"])
if comet:
comets.append(float(row["comet"]))
if bertScore:
bertScores.append(float(row["bertScore"]))
if hter:
hters.append(float(row["hter"]))
if features:
lans.append(float(row["lan"]))
unigrams.append(float(row["unigram"]))
bigrams.append(float(row["bigram"]))
grams3.append(float(row["3gram"]))
grams4.append(float(row["4gram"]))
grams5.append(float(row["5gram"]))
else:
lans.append(0)
unigrams.append(0)
bigrams.append(0)
grams3.append(0)
grams4.append(0)
grams5.append(0)
# print(row["5gram"])
df = pd.DataFrame(
{'index': indices,
'original': originals,
'translation': translations,
'z_mean': z_means
})
if ref:
df["original"] = refs # override the source sentence.
if comet:
df['comet'] = comets
if bertScore:
df['bertScore'] = bertScores
if hter:
df['hter'] = hters
df['hter'] = -df['hter']
# if features:
df['unigram'] = unigrams
df['bigram'] = bigrams
df['3gram'] = grams3
df['4gram'] = grams4
df['5gram'] = grams5
df['lan'] = lans
# print(df)
if replace_hter:
tmp_hter = df['hter'].copy()
df['hter'] = df['z_mean'].copy()
df['z_mean'] = tmp_hter
if replace_comet:
tmp_comet = df['comet'].copy()
df['comet'] = df['z_mean'].copy()
df['z_mean'] = tmp_comet
return df
def read_test_file(path, index="index"):
indices = []
originals = []
translations = []
with open(path, mode="r", encoding="utf-8-sig") as csvfile:
reader = csv.DictReader(csvfile, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
indices.append(row[index])
originals.append(row["original"])
translations.append(row["translation"])
return pd.DataFrame(
{'index': indices,
'original': originals,
'translation': translations,
})