-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_pipe.py
53 lines (46 loc) · 1.97 KB
/
data_pipe.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
import csv
import random
from sentence_transformers.readers import InputExample
def load_train_kor_sts(filename):
samples = []
with open(filename, "rt", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
score = float(row["score"]) / 5.0
samples.append(InputExample(texts=[row["sentence1"], row["sentence2"]], label=score))
return samples
def load_train_kor_nli(filename):
data = {}
def add_sampling(samplingA, samplingB, label):
if samplingA not in data:
data[samplingA] = {"contradiction": set(), "entailment": set(), "neutral": set()}
data[samplingA][label].add(samplingB)
with open(filename, "r", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
samplingA = row["sentence1"].strip()
samplingB = row["sentence2"].strip()
add_sampling(samplingA, samplingB, row["gold_label"])
add_sampling(samplingB, samplingA, row["gold_label"])
samples = []
for sampling, etc in data.items():
if len(etc["entailment"]) > 0 and len(etc["contradiction"]) > 0:
samples.append(
InputExample(
texts=[
sampling,
random.choice(list(etc["entailment"])),
random.choice(list(etc["contradiction"])),
]
)
)
samples.append(
InputExample(
texts=[
random.choice(list(etc["entailment"])),
sampling,
random.choice(list(etc["contradiction"])),
]
)
)
return samples