-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathofficial_scorer.py
63 lines (43 loc) · 1.77 KB
/
official_scorer.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
import os
import json
import subprocess
def create_key_file(wsd_df, key_path, level):
"""
create key file based on information in
wsd_output.p in experiment folder
:param pandas.core.frame.DataFrame wsd_df: wsd dataframe
:param str key_path: path where keyfile will be stored
:param str level: supported sensekey | synset
"""
with open(key_path, 'w') as outfile:
for row in wsd_df.itertuples():
lstm_output = row.lstm_output
if level == 'synset':
target_sensekey = row.synset2sensekey[lstm_output]
elif level == 'sensekey':
target_sensekey = lstm_output
token_id = row.token_ids[0]
export = [token_id, target_sensekey]
outfile.write(' '.join(export) + '\n')
def score_using_official_scorer(scorer_folder,
system,
key,
json_output_path):
"""
score using official scorer
:param str scorer_folder: scorer folder
:param str system: path to system output
:param str key: path to gold file (with answers from manual annotation)
:param str json_output_path: path to where json with results are stored
"""
command = 'cd {scorer_folder} && java Scorer "{key}" "../../{system}"'.format_map(locals())
output = subprocess.check_output(command, shell=True)
output = output.decode("utf-8")
results = dict()
for metric_output in output.split('\n')[:3]:
metric, value = metric_output[:-1].split('=\t')
value = value.replace(',', '.')
results[metric] = float(value) / 100
assert set(results) == {'P', 'R', 'F1'}
with open(json_output_path, 'w') as outfile:
json.dump(results, outfile)