-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_util.py
54 lines (46 loc) · 1.87 KB
/
data_util.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
import csv
import random
from sentence_transformers.readers import InputExample
def load_kor_sts_samples(filename):
samples = []
with open(filename, "rt", encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
score = float(row["score"]) / 5.0 # Normalize score to range 0 ... 1
samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score))
return samples
def load_kor_nli_samples(filename):
data = {}
def add_to_samples(sent1, sent2, label):
if sent1 not in data:
data[sent1] = {"contradiction": set(), "entailment": set(), "neutral": set()}
data[sent1][label].add(sent2)
with open(filename, "r", encoding="utf-8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
sent1 = row["sentence1"].strip()
sent2 = row["sentence2"].strip()
add_to_samples(sent1, sent2, row["gold_label"])
add_to_samples(sent2, sent1, row["gold_label"]) # Also add the opposite
samples = []
for sent, others in data.items():
if len(others["entailment"]) > 0 and len(others["contradiction"]) > 0:
samples.append(
InputExample(
texts=[
sent,
random.choice(list(others["entailment"])),
random.choice(list(others["contradiction"])),
]
)
)
samples.append(
InputExample(
texts=[
random.choice(list(others["entailment"])),
sent,
random.choice(list(others["contradiction"])),
]
)
)
return samples