7INT corrupt_head(INT
id, INT h, INT r,
bool filter_flag =
true) {
8 INT lef, rig, mid, ll, rr;
10 INT tmp = rand_max(
id, entityTotal - 1);
18 while (lef + 1 < rig) {
19 mid = (lef + rig) >> 1;
20 if (trainHead[mid].r >= r)
28 while (lef + 1 < rig) {
29 mid = (lef + rig) >> 1;
30 if (trainHead[mid].r <= r)
36 INT tmp = rand_max(
id, entityTotal - (rr - ll + 1));
37 if (tmp < trainHead[ll].t)
39 if (tmp > trainHead[rr].t - rr + ll - 1)
40 return tmp + rr - ll + 1;
41 lef = ll, rig = rr + 1;
42 while (lef + 1 < rig) {
43 mid = (lef + rig) >> 1;
44 if (trainHead[mid].t - mid + ll - 1 < tmp)
49 return tmp + lef - ll + 1;
52INT corrupt_tail(INT
id, INT t, INT r,
bool filter_flag =
true) {
53 INT lef, rig, mid, ll, rr;
54 if (not filter_flag) {
55 INT tmp = rand_max(
id, entityTotal - 1);
63 while (lef + 1 < rig) {
64 mid = (lef + rig) >> 1;
65 if (trainTail[mid].r >= r)
73 while (lef + 1 < rig) {
74 mid = (lef + rig) >> 1;
75 if (trainTail[mid].r <= r)
81 INT tmp = rand_max(
id, entityTotal - (rr - ll + 1));
82 if (tmp < trainTail[ll].h)
84 if (tmp > trainTail[rr].h - rr + ll - 1)
85 return tmp + rr - ll + 1;
86 lef = ll, rig = rr + 1;
87 while (lef + 1 < rig) {
88 mid = (lef + rig) >> 1;
89 if (trainTail[mid].h - mid + ll - 1 < tmp)
94 return tmp + lef - ll + 1;
97INT corrupt_rel(INT
id, INT h, INT t, INT r,
bool p =
false,
98 bool filter_flag =
true) {
99 INT lef, rig, mid, ll, rr;
100 if (not filter_flag) {
101 INT tmp = rand_max(
id, relationTotal - 1);
109 while (lef + 1 < rig) {
110 mid = (lef + rig) >> 1;
111 if (trainRel[mid].t >= t)
119 while (lef + 1 < rig) {
120 mid = (lef + rig) >> 1;
121 if (trainRel[mid].t <= t)
129 tmp = rand_max(
id, relationTotal - (rr - ll + 1));
131 INT start = r * (relationTotal - 1);
133 bool *record = (
bool *)calloc(relationTotal - 1,
sizeof(
bool));
134 for (INT i = ll; i <= rr; ++i) {
135 if (trainRel[i].r > r) {
136 sum -= prob[start + trainRel[i].r - 1];
137 record[trainRel[i].r - 1] =
true;
138 }
else if (trainRel[i].r < r) {
139 sum -= prob[start + trainRel[i].r];
140 record[trainRel[i].r] =
true;
144 (REAL *)calloc(relationTotal - (rr - ll + 1),
sizeof(REAL));
147 for (INT i = start; i < start + relationTotal - 1; ++i) {
148 if (record[i - start])
150 rec += prob[i] / sum;
151 prob_tmp[cnt++] = rec;
153 REAL m = rand_max(
id, 10000) / 10000.0;
157 mid = (lef + rig) >> 1;
158 if (prob_tmp[mid] < m)
167 if (tmp < trainRel[ll].r)
169 if (tmp > trainRel[rr].r - rr + ll - 1)
170 return tmp + rr - ll + 1;
171 lef = ll, rig = rr + 1;
172 while (lef + 1 < rig) {
173 mid = (lef + rig) >> 1;
174 if (trainRel[mid].r - mid + ll - 1 < tmp)
179 return tmp + lef - ll + 1;
182bool _find(INT h, INT t, INT r) {
184 INT rig = tripleTotal - 1;
186 while (lef + 1 < rig) {
187 INT mid = (lef + rig) >> 1;
188 if ((tripleList[mid].h < h) ||
189 (tripleList[mid].h == h && tripleList[mid].r < r) ||
190 (tripleList[mid].h == h && tripleList[mid].r == r &&
191 tripleList[mid].t < t))
196 if (tripleList[lef].h == h && tripleList[lef].r == r &&
197 tripleList[lef].t == t)
199 if (tripleList[rig].h == h && tripleList[rig].r == r &&
200 tripleList[rig].t == t)
205INT corrupt(INT h, INT r) {
206 INT ll = tail_lef[r];
207 INT rr = tail_rig[r];
211 t = tail_type[rand(ll, rr)];
212 if (not _find(h, t, r)) {
217 return corrupt_head(0, h, r);