-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_triples.py
53 lines (38 loc) · 1.12 KB
/
clean_triples.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
'''
Remove redundant relations
'''
import csv
def write_to_file(data, filename):
path = "data/openke/"
with open(path+filename,"w+", encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)
def read_file(filename):
path = "data/openke/"
with open(path+filename,"r", encoding='utf-8') as f:
return f.read().split("\n")
train = read_file("train.csv")
valid = read_file("valid.csv")
test = read_file("test.csv")
print("Train:", len(train))
print("Valid:", len(valid))
print("Test:", len(test))
# Removing entries whose entity pairs are directly linked to train
train_e_pairs = set()
for each in train:
e1, r, e2 = each.split(",")
train_e_pairs.add((e1,e2))
_valid = []
for each in valid:
e1, r, e2 = each.split(",")
if (e1, e2) and (e2, e1) not in train_e_pairs:
_valid.append((e1,r,e2))
print("Valid:", len(_valid))
write_to_file(_valid, "valid.csv")
_test = []
for each in test:
e1, r, e2 = each.split(",")
if (e1, e2) and (e2, e1) not in train_e_pairs:
_test.append((e1,r,e2))
print("Test:", len(_test))
write_to_file(_test, "test.csv")