Repository navigation
Expand file tree
/
Copy pathData Processing.py
More file actions
160 lines (140 loc) · 3.87 KB
/
Copy pathData Processing.py
File metadata and controls
160 lines (140 loc) · 3.87 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 10 16:06:28 2023
@author: Zhao Li
# @File: Knowledge Hypergraph Data Processing
"""
import os
os.getcwd()
# new_path = '/your/new/directory/path'
# os.chdir(new_path)
### n-ary dataset generation ###
ary_2 = []
ary_3 = []
ary_4 = []
ary_5 = []
ary_6 = []
ary_7 = []
ary_8 = []
ary_9 = []
## train dataset
with open("./train.txt", "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip().split("\t")
rel = line[0]
ents = line[1:]
if len(ents) == 2:
ary_2.append([rel] + ents)
elif len(ents) == 3:
ary_3.append([rel] + ents)
elif len(ents) == 4:
ary_4.append([rel] + ents)
elif len(ents) == 5:
ary_5.append([rel] + ents)
elif len(ents) == 6:
ary_6.append([rel] + ents)
elif len(ents) == 7:
ary_7.append([rel] + ents)
elif len(ents) == 8:
ary_8.append([rel] + ents)
elif len(ents) == 9:
ary_9.append([rel] + ents)
f.close()
for i in [2, 3, 4, 5, 6, 7, 8, 9]:
with open("./train_{}.txt".format(i), "w") as f:
for tuple_ in eval("ary_{}".format(i)):
for k in range(len(tuple_)):
if k != len(tuple_) - 1:
f.write(tuple_[k])
f.write("\t")
else:
f.write(tuple_[k])
f.write("\n")
f.close()
## test dataset
ary_2 = []
ary_3 = []
ary_4 = []
ary_5 = []
ary_6 = []
ary_7 = []
ary_8 = []
ary_9 = []
with open("./test.txt", "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip().split("\t")
rel = line[0]
ents = line[1:]
if len(ents) == 2:
ary_2.append([rel] + ents)
elif len(ents) == 3:
ary_3.append([rel] + ents)
elif len(ents) == 4:
ary_4.append([rel] + ents)
elif len(ents) == 5:
ary_5.append([rel] + ents)
elif len(ents) == 6:
ary_6.append([rel] + ents)
elif len(ents) == 7:
ary_7.append([rel] + ents)
elif len(ents) == 8:
ary_8.append([rel] + ents)
elif len(ents) == 9:
ary_9.append([rel] + ents)
f.close()
for i in [2, 3, 4, 5, 6, 7, 8, 9]:
with open("./test_{}.txt".format(i), "w") as f:
for tuple_ in eval("ary_{}".format(i)):
for k in range(len(tuple_)):
if k != len(tuple_) - 1:
f.write(tuple_[k])
f.write("\t")
else:
f.write(tuple_[k])
f.write("\n")
f.close()
## valid dataset
ary_2 = []
ary_3 = []
ary_4 = []
ary_5 = []
ary_6 = []
ary_7 = []
ary_8 = []
ary_9 = []
with open("./valid.txt", "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip().split("\t")
rel = line[0]
ents = line[1:]
if len(ents) == 2:
ary_2.append([rel] + ents)
elif len(ents) == 3:
ary_3.append([rel] + ents)
elif len(ents) == 4:
ary_4.append([rel] + ents)
elif len(ents) == 5:
ary_5.append([rel] + ents)
elif len(ents) == 6:
ary_6.append([rel] + ents)
elif len(ents) == 7:
ary_7.append([rel] + ents)
elif len(ents) == 8:
ary_8.append([rel] + ents)
elif len(ents) == 9:
ary_9.append([rel] + ents)
f.close()
for i in [2, 3, 4, 5, 6, 7, 8, 9]:
with open("./valid_{}.txt".format(i), "w") as f:
for tuple_ in eval("ary_{}".format(i)):
for k in range(len(tuple_)):
if k != len(tuple_) - 1:
f.write(tuple_[k])
f.write("\t")
else:
f.write(tuple_[k])
f.write("\n")
f.close()