-
Notifications
You must be signed in to change notification settings - Fork 1
/
sts.py
149 lines (123 loc) · 5.03 KB
/
sts.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
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
#!/usr/bin/env python
# coding=utf-8
# Lint as: python3
'''
STS-{2012,2013,2014,2015,2016} (unsupervised) and
STS-benchmark (supervised) tasks
'''
from __future__ import absolute_import, division, unicode_literals
import os
import io
import numpy as np
import logging
from pprint import pprint
from scipy.stats import spearmanr, pearsonr
class STSEval(object):
def loadFile(self, fpath):
self.data = []
for dataset in self.datasets:
lines = io.open(fpath + '/STS.input.%s.txt' % dataset,
encoding='utf8').read().splitlines()
scores = io.open(fpath + '/STS.gs.%s.txt' % dataset,
encoding='utf8').read().splitlines()
for l, raw_score in zip(lines, scores):
if raw_score != '':
data_item = {}
data_item['sentence1'], data_item['sentence2'] = l.split("\t")[:2]
data_item['label'] = float(raw_score)
self.data.append(data_item)
class STS12Eval(STSEval):
def __init__(self, taskpath, seed=1111):
logging.debug('***** Transfer task : STS12 *****\n\n')
self.seed = seed
self.datasets = ['MSRpar', 'MSRvid', 'SMTeuroparl',
'surprise.OnWN', 'surprise.SMTnews']
self.loadFile(taskpath)
class STS13Eval(STSEval):
# STS13 here does not contain the "SMT" subtask due to LICENSE issue
def __init__(self, taskpath, seed=1111):
logging.debug('***** Transfer task : STS13 (-SMT) *****\n\n')
self.seed = seed
self.datasets = ['FNWN', 'headlines', 'OnWN']
self.loadFile(taskpath)
class STS14Eval(STSEval):
def __init__(self, taskpath, seed=1111):
logging.debug('***** Transfer task : STS14 *****\n\n')
self.seed = seed
self.datasets = ['deft-forum', 'deft-news', 'headlines',
'images', 'OnWN', 'tweet-news']
self.loadFile(taskpath)
class STS15Eval(STSEval):
def __init__(self, taskpath, seed=1111):
logging.debug('***** Transfer task : STS15 *****\n\n')
self.seed = seed
self.datasets = ['answers-forums', 'answers-students',
'belief', 'headlines', 'images']
self.loadFile(taskpath)
class STS16Eval(STSEval):
def __init__(self, taskpath, seed=1111):
logging.debug('***** Transfer task : STS16 *****\n\n')
self.seed = seed
self.datasets = ['answer-answer', 'headlines', 'plagiarism',
'postediting', 'question-question']
self.loadFile(taskpath)
class STSBenchmarkEval(STSEval):
def __init__(self, taskpath, seed=1111):
logging.debug('\n\n***** Transfer task : STSBenchmark*****\n\n')
self.seed = seed
self.train = self.loadFile(os.path.join(taskpath, 'sts-train.csv'))
self.dev = self.loadFile(os.path.join(taskpath, 'sts-dev.csv'))
self.test = self.loadFile(os.path.join(taskpath, 'sts-test.csv'))
self.data = self.test
def loadFile(self, fpath):
data = []
with io.open(fpath, 'r', encoding='utf-8') as f:
for l in f:
data_item = {}
text = l.strip().split('\t')
data_item['sentence1'] = text[5]
data_item['sentence2'] = text[6]
data_item['label'] = float(text[4])
data.append(data_item)
return data
class SICKRelatednessEval(object):
def __init__(self, task_path, seed=1111):
logging.debug('***** Transfer task : SICK-Relatedness*****\n\n')
self.seed = seed
self.train = self.loadFile(os.path.join(task_path, 'SICK_train.txt'))
self.dev = self.loadFile(os.path.join(task_path, 'SICK_trial.txt'))
self.test = self.loadFile(os.path.join(task_path, 'SICK_test_annotated.txt'))
self.data = self.test
def loadFile(self, fpath):
skipFirstLine = True
data = []
with io.open(fpath, 'r', encoding='utf-8') as f:
for line in f:
if skipFirstLine:
skipFirstLine = False
else:
text = line.strip().split('\t')
data_item = {}
data_item['sentence1'] = text[1]
data_item['sentence2'] = text[2]
data_item['label'] = float(text[3])
data.append(data_item)
return data
if __name__ == "__main__":
years = [12, 13, 14, 15, 16]
taskpath ="/datadrive/shawn/code/SentEval/data/downstream/STS/STS%d-en-test"
for year in years:
sts = eval("STS%dEval" % year)(taskpath % year)
for d in sts.data:
pass
print(len(sts.data))
taskpath ="/datadrive/shawn/code/SentEval/data/downstream/STS/STSBenchmark"
sts = STSBenchmarkEval(taskpath)
for d in sts.data:
pass
print(len(sts.data))
taskpath ="/datadrive/shawn/code/SentEval/data/downstream/SICK"
sts = SICKRelatednessEval(taskpath)
for d in sts.data:
print(d)
print(len(sts.data))