-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidateSAIL.py
91 lines (78 loc) · 2.73 KB
/
validateSAIL.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
#!/usr/bin/env python3
"""
Script to validate the labelled test dataset for the SAIL (Codemixed) 2017 shared task @ICON-2017.
This script requires the original unlabelled file provided by the organizers.
If your system is unable to predict sentiment of a sentence, then tag -2 in case of sentiment.
To run:
python validateSAIL.py gold.json predicted.json
"""
import sys, codecs, json
sentiment = [0, 1, -1, -2]
def validate(gold_file, pred_file):
gold_data, pred_data = [], []
with codecs.open(gold_file) as f:
try:
gold_data = json.load(f)
except Exception as e:
print(e)
f.close()
with codecs.open(pred_file) as f:
try:
pred_data = json.load(f)
except Exception as e:
print(e)
f.close()
if len(gold_data) > len(pred_data):
print('There are less items in predicted file')
sys.exit()
elif len(gold_data) < len(pred_data):
print('There are more items in predicted file')
sys.exit()
for gold_item, pred_item in zip(gold_data, pred_data):
gold_id = gold_item['id']
pred_id = pred_item['id']
if not gold_id == pred_id:
print('ID miss match: gold sentence ID = {} and predicted sentence ID = {}'.format(gold_id, pred_id))
sys.exit()
gold_text, pred_text = '', ''
gold_tagged_text, pred_tagged_text = '', ''
pred_sentiment = ''
try:
gold_sent = gold_item['text']
gold_tagged_sent = gold_item['lang_tagged_text']
pred_sent = pred_item['text']
pred_tagged_sent = pred_item['lang_tagged_text']
pred_sentiment = pred_item['sentiment']
except Exception as e:
print(e)
sys.exit()
if not gold_sent == pred_sent:
print('Text miss match at ID = {}'.format(gold_id))
sys.exit()
elif not gold_tagged_sent == pred_tagged_sent:
print('Language tagged text miss match at ID = {}'.format(gold_id))
sys.exit()
elif not pred_sentiment in sentiment:
print('Sentiment tag is different at ID = {}. It should be one of the 0, 1, -1 or -2.'.format(gold_id))
sys.exit()
print('Validation Successful. Files are ready to submit.')
if __name__ == '__main__':
if len(sys.argv) < 3 :
print('Give two input files')
print('Format should be: python validateSAIL.py gold.json pred.json')
sys.exit()
elif len(sys.argv) > 3 :
print('Input should not be more than two')
print('Format should be: python validateSAIL.py gold.json pred.json')
sys.exit()
else:
gold_file = sys.argv[1]
pred_file = sys.argv[2]
if not gold_file.endswith('.json'):
print('original file format should be .json')
sys.exit()
elif not pred_file.endswith('.json'):
print('labelled file format should be .json')
sys.exit()
else:
validate(gold_file, pred_file)