-
Notifications
You must be signed in to change notification settings - Fork 0
/
cefr.py
100 lines (79 loc) · 2.33 KB
/
cefr.py
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
import json
import argparse
cefr_to_num = {
"A1": 0,
"A2": 1,
"B1": 2,
"B2": 3,
"C": 4,
"C1": 4,
"C2": 5,
}
num_to_cefr = {
0: "A1",
1: "A2",
2: "B1",
3: "B2",
4: "C",
5: "C2",
}
def get_numerical_cefr(cefr):
if type(cefr) == str:
return cefr_to_num[cefr]
return cefr
def get_char_cefr(cefr):
if type(cefr) == int:
return num_to_cefr[cefr]
return cefr
class CEFR:
def __init__(self, cefr_path, level_type="min"):
self.level_type = level_type
self.path = cefr_path
# use mean of different levels for different pos of the word
def _mean(self, k, v):
values = [cefr_to_num[x.upper()] for x in v.values()]
return sum(values)/len(values)
# use minimum of all levels for different pos of the word
def _minimum(self, k, v):
min_value = 6
for value in v.values():
if (cefr_to_num[value.upper()] < min_value):
min_value = cefr_to_num[value.upper()]
return min_value
def _get_dictionary(self):
with open(self.path, "r") as fp:
all_cefr = json.load(fp)
cefr = {}
for k, v in all_cefr.items():
if self.level_type == "min":
cefr[k] = self._minimum(k, v)
elif self.level_type == "avg":
cefr[k] = self._mean(k, v)
return cefr
def _save(self, cefr, save_path):
with open(save_path, "w") as f:
print("saving to:", save_path)
json.dump(cefr, f)
def dictionary(self, save_path=None):
cefr = self._get_dictionary()
if save_path:
self._save(cefr, save_path)
else:
return cefr
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--cefr_path",
type=str,
default="data/word_difficulty_classifier/cefr.json",
help="path_to_cefr",
)
parser.add_argument(
"--cefr_output_path",
type=str,
default="data/word_difficulty_classifier/cefr_min.json",
help="output path for cefr json file",
)
args = parser.parse_args()
cefr = CEFR(args.cefr_path)
cefr.dictionary(save_path=args.cefr_output_path)