-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraint_Mining.py
1864 lines (1698 loc) · 83.2 KB
/
Constraint_Mining.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import gc
import read_datasets
import Interval_Relations
import Graph_Structure
import time
import os
import Conflict_Detection
from multiprocessing.dummy import Pool as ThreadPool
import Refinement_Mining
# prune2_begin_percent=0.1
candidate_threshold=0.5
confidence_threshold=0.9
support_threshold=100
pruned_threshold=0.8*candidate_threshold
pruned_instances=0.5*support_threshold
relation_prune_threshold=10*support_threshold
# relation_prune_threshold=100
# relation_prune_threshold=100000000
truncate_threshold=0.9
def Mutual_Exclusion_mining(graph):
# a relation is temporally functional if its value's valid time has no overlaps
# find which relation is functional
# what a functional constraint is like?
# how to compute confidence? present strategy is consistent subsets/total subsets
# possible_subgraph = 0
print("Mutual_Exclusion_mining......")
st = time.time()
# output_filename = "functional_conflict.txt"
# a quick index dict
index_dict={}
Mutual_Exclusion_constraint = []
for i in range(len(graph.temporalRelationList)):
index_dict[graph.temporalRelationList[i]]=i
F_relations = graph.temporalRelationList.copy()
# index_dict["P54"] = 0
# F_relations =["P54"]
F_relations_statistics=[]
for r in F_relations:
F_relations_statistics.append([r,0,0])
#relation consistent_subset total_subset
#[[P1,0,0]]
vertex_count=0
pre = time.time()
if len(graph.eVertexList)<=50000:
mutual_exclusion_threshold = 0.96
else:
mutual_exclusion_threshold = 0.98
for i in graph.eVertexList:
vertex_count += 1
if vertex_count % 1000000 == 0:
ed = time.time()
print("have traversed nodes:", vertex_count)
print("time cost:", ed - pre, "s")
v = graph.eVertexList[i]
if v.isLiteral == True:
# return
continue
# if len(v.hasStatement)<2:
# continue
all_relation_pairs = {}
for s in v.hasStatement:
i1 = s.getStartTime()
i2 = s.getEndTime()
# we only care temporal facts
tail=s.hasValue.getId()
if i1 != -1 or i2 != -1:
relation = s.getId()
if index_dict.__contains__(relation):
index = index_dict[relation]
all_relation_pairs.setdefault(index, []).append(s)
#用一个set把index填进去
for j in all_relation_pairs.keys():
# hasRelation=true
# total subsets+=1
# if len(all_relation_pairs[j])==1:
# continue
F_relations_statistics[j][2] += 1
consistent = True
exist = False
if len(all_relation_pairs[j])!=1:
consistent=False
# possible_subgraph += len(all_relation_pairs[j])*(len(all_relation_pairs[j])-1)/2
if consistent == True:
# consistent subsets+=1
F_relations_statistics[j][1] += 1
all_relation_pairs.clear()
for f in F_relations_statistics:
total_subsets=f[2]
consistent_subsets=f[1]
relation=f[0]
if total_subsets == 0:
confidence = 0
else:
confidence = consistent_subsets * 1.0 / total_subsets
# print(relation, consistent_subsets, total_subsets, confidence)
if confidence > mutual_exclusion_threshold and consistent_subsets > support_threshold:
# if confidence > confidence_threshold and consistent_subsets > support_threshold:
constraint = "(a," + relation + ",b,t1,t2) & (a," + relation + ",c,t3,t4) => MutualExclusion|" + str(
confidence)
# print(constraint)
Mutual_Exclusion_constraint.append(constraint)
# x relation1 y & x relation2 z t1(t1=开始结束时间取平均数) & y relation3 w t2 = > t2 before t1 / t1 before t2 / t1 during t2 / t2 during t1
ed = time.time()
print("Mutual_Exclusion_Mining time is", ed - st, "s")
# print(possible_subgraph)
return Mutual_Exclusion_constraint
def functional_mining(graph):
# a relation is temporally functional if its value's valid time has no overlaps
# find which relation is functional
# what a functional constraint is like?
# how to compute confidence? present strategy is consistent subsets/total subsets
# possible_subgraph = 0
print("functional_mining......")
st = time.time()
# output_filename = "functional_conflict.txt"
# a quick index dict
index_dict={}
functional_constraint = []
for i in range(len(graph.temporalRelationList)):
index_dict[graph.temporalRelationList[i]]=i
F_relations = graph.temporalRelationList.copy()
# index_dict["P54"] = 0
# F_relations =["P54"]
F_relations_statistics=[]
for r in F_relations:
F_relations_statistics.append([r,0,0])
#relation consistent_subset total_subset
#[[P1,0,0]]
vertex_count=0
pre = time.time()
for i in graph.eVertexList:
vertex_count += 1
if vertex_count % 1000000 == 0:
ed = time.time()
print("have traversed nodes:", vertex_count)
print("time cost:", ed - pre, "s")
v = graph.eVertexList[i]
if v.isLiteral == True:
# return
continue
if len(v.hasStatement)<2:
continue
if len(v.hasStatement) >1000:
continue
all_relation_pairs = {}
for s in v.hasStatement:
i1 = s.getStartTime()
i2 = s.getEndTime()
# we only care temporal facts
tail=s.hasValue.getId()
if i1 != -1 or i2 != -1:
relation = s.getId()
if index_dict.__contains__(relation):
index = index_dict[relation]
all_relation_pairs.setdefault(index, []).append(s)
#用一个set把index填进去
for j in all_relation_pairs.keys():
# hasRelation=true
# total subsets+=1
# if len(all_relation_pairs[j])==1:
# continue
# F_relations_statistics[j][2] += 1
consistent = True
negative = False
# possible_subgraph += len(all_relation_pairs[j]) * (len(all_relation_pairs[j]) - 1) / 2
for k in range(len(all_relation_pairs[j])):
vertex1 = all_relation_pairs[j][k]
flag = True
for l in range(k + 1, len(all_relation_pairs[j])):
# possible_subgraph +=1
vertex2 = all_relation_pairs[j][l]
start1 = vertex1.getStartTime()
end1 = vertex1.getEndTime()
start2 = vertex2.getStartTime()
end2 = vertex2.getEndTime()
tail1 = vertex1.hasValue.getId()
tail2 = vertex2.hasValue.getId()
# confidence = positive+unknown/positive+unknown+negative
# if Interval_Relations.disjoint(start1, end1, start2, end2) == -1:
# consistent = False
# flag = False
# break
#confidence = positive/positive+negative
result=Interval_Relations.disjoint(start1, end1, start2, end2)
if result == -1:
consistent = False
flag = False
negative=True
break
if result == 0:
consistent = False
if flag == False:
break
if consistent == True:
# consistent subsets+=1
F_relations_statistics[j][1] += 1
# confidence = positive/positive+negative
F_relations_statistics[j][2] += 1
elif negative==True:
F_relations_statistics[j][2] += 1
all_relation_pairs.clear()
for f in F_relations_statistics:
total_subsets=f[2]
consistent_subsets=f[1]
relation=f[0]
if total_subsets == 0:
confidence = 0
else:
confidence = consistent_subsets * 1.0 / total_subsets
# print(relation, consistent_subsets, total_subsets, confidence)
if confidence > candidate_threshold and consistent_subsets > support_threshold:
# if confidence > confidence_threshold and consistent_subsets > support_threshold:
constraint = "(a," + relation + ",b,t1,t2) & (a," + relation + ",c,t3,t4) => disjoint(t1,t2,t3,t4)|" + str(
confidence)
# print(constraint)
functional_constraint.append(constraint)
# x relation1 y & x relation2 z t1(t1=开始结束时间取平均数) & y relation3 w t2 = > t2 before t1 / t1 before t2 / t1 during t2 / t2 during t1
ed = time.time()
print("functional_mining time is", ed - st, "s")
# print(possible_subgraph)
return functional_constraint
def inverse_functional_mining(graph):
# possible_subgraph=0
# a relation is temporally functional if its value's valid time has no overlaps
# find which relation is functional
# what a functional constraint is like?
# how to compute confidence? present strategy is consistent subsets/total subsets
print("inverse_functional_mining......")
st=time.time()
inverse_functional_constraint = []
index_dict = {}
for i in range(len(graph.temporalRelationList)):
index_dict[graph.temporalRelationList[i]] = i
IF_relations = graph.temporalRelationList.copy()
IF_relations_statistics = []
for r in IF_relations:
IF_relations_statistics.append([r, 0, 0])
# relation consistent_subset total_subset
# [[P1,0,0]]
vertex_count=0
pre=time.time()
for i in graph.eVertexList:
vertex_count+=1
if vertex_count%1000000==0:
ed=time.time()
print("have traversed nodes:",vertex_count)
print("time cost:",ed-pre,"s")
v = graph.eVertexList[i]
if len(v.bePointedTo)<2:
continue
if v.isLiteral==True:
continue
all_relation_pairs={}
if len(v.bePointedTo)>1000:
continue
for s in v.bePointedTo:
i1 = s.getStartTime()
i2 = s.getEndTime()
# we only care temporal facts
if i1 != -1 or i2 != -1:
relation = s.getId()
index = index_dict[relation]
all_relation_pairs.setdefault(index, []).append(s)
# [[P54,e,e,e],[P286,e,e,e]]
for j in all_relation_pairs.keys():
# if len(all_relation_pairs[j])==1:
# continue
# IF_relations_statistics[j][2] += 1
consistent = True
negative = False
# possible_subgraph += len(all_relation_pairs[j]) * (len(all_relation_pairs[j]) - 1) / 2
for k in range(len(all_relation_pairs[j])):
vertex1 = all_relation_pairs[j][k]
flag=True
for l in range(k + 1, len(all_relation_pairs[j])):
# possible_subgraph +=1
vertex2 = all_relation_pairs[j][l]
start1 = vertex1.getStartTime()
end1 = vertex1.getEndTime()
start2 = vertex2.getStartTime()
end2 = vertex2.getEndTime()
# if Interval_Relations.disjoint(start1, end1, start2, end2) == -1:
# consistent = False
# flag=False
# break
# confidence = positive/positive+negative
result=Interval_Relations.disjoint(start1, end1, start2, end2)
if result == -1:
consistent = False
flag = False
negative = True
break
if result == 0:
consistent = False
if flag==False:
break
if consistent == True:
# consistent subsets+=1
IF_relations_statistics[j][1] += 1
# confidence = positive/positive+negative
IF_relations_statistics[j][2] += 1
elif negative == True:
IF_relations_statistics[j][2] += 1
for f in IF_relations_statistics:
total_subsets = f[2]
consistent_subsets = f[1]
relation = f[0]
if total_subsets == 0:
confidence = 0
else:
confidence = consistent_subsets * 1.0 / total_subsets
# print(relation, consistent_subsets, total_subsets, confidence)
if confidence > candidate_threshold and consistent_subsets > support_threshold:
# if confidence > confidence_threshold and consistent_subsets > support_threshold:
constraint = "(a," + relation + ",b,t1,t2) & (c," + relation + ",b,t3,t4) => disjoint(t1,t2,t3,t4)|"+str(confidence)
# print(constraint)
inverse_functional_constraint.append(constraint)
# x relation1 y & x relation2 z t1(t1=开始结束时间取平均数) & y relation3 w t2 = > t2 before t1 / t1 before t2 / t1 during t2 / t2 during t1
ed=time.time()
print("inverse_functional_mining time is",ed-st,"s")
# print(possible_subgraph)
return inverse_functional_constraint
def Single_Entity_Temporal_Order(graph,pruned_relation):
# transitivity
# before include start finish overlap disjoint
print("Single Entity Temporal Order Mining......")
possible_subgraph = 0
st = time.time()
index_dict = {}
Single_Entity_Temporal_Order_Constraint = []
ZH_relations = []
ZH_relations_statistics=[]
cou=0
for i in range(len(graph.temporalRelationList)):
for j in range(i+1,len(graph.temporalRelationList)):
ZH_relation1 = graph.temporalRelationList[i]
ZH_relation2 = graph.temporalRelationList[j]
relation_pair1 = [ZH_relation1, ZH_relation2]
relation_pair2 = [ZH_relation2, ZH_relation1]
ZH_relations.append(relation_pair1)
ZH_relations_statistics.append([ZH_relation1,ZH_relation2,0,0,0,0,0,0,0,0])
key=ZH_relation1+"*"+ZH_relation2
index_dict[key]=cou
cou+=1
#before include start finish total
ZH_relations.append(relation_pair2)
ZH_relations_statistics.append([ZH_relation2, ZH_relation1, 0, 0, 0, 0, 0,0,0,0])
key = ZH_relation2 + "*" + ZH_relation1
index_dict[key] = cou
cou+=1
# [[P54,P569,before],[P54,P570,before]]
vertex_count = 0
pre = time.time()
# pool=ThreadPool(4)
# def single_subgraph_traverse(i):
#how to prune1?
#n * n complexity, we can prune at the first layer
# relation_temporal_order_score=[]
# for i in graph.temporalRelationList:
# #relation,has_order,entity_subgraph
# record=[i,0,0]
# relation_temporal_order_score.append(record)
# print(relation_temporal_order_score)
possible_subgraph_set=set()
pruned_subgraph_set=set()
relation_in_possible_set={}
# relation_in_pruned_set=set()
relation_in_howmany_entities={}
for r in graph.temporalRelationList:
relation_in_possible_set[r]=0
relation_in_howmany_entities[r]=0
# print(relation_in_possible_set)
# prune_begin_number = prune2_begin_percent * len(graph.eVertexList)
for i in graph.eVertexList:
relation_temporal_order_true=set()
relation_temporal_order_exist=set()
cou+=1
# print(cou)
vertex_count += 1
if vertex_count % 1000000 == 0:
ed = time.time()
print("have traversed nodes:", vertex_count)
print("time cost:", ed - pre, "s")
# prune_step2=False
# if prune_step2==False:
# if vertex_count > prune_begin_number:
# prune_step2=True
v = graph.eVertexList[i]
# print(v.getId())
if v.isLiteral==True:
continue
# return
if len(v.hasStatement) < 2:
continue
# return
else:
all_relation_pairs={}
# time0=time.time()
relation_set=set()
for j in range(len(v.hasStatement)):
s1 = v.hasStatement[j]
relation1 = s1.getId()
start1 = s1.getStartTime()
end1 = s1.getEndTime()
if start1 != -1 or end1 != -1:
#prune
# if vertex_count>prune_begin_number:
# continue
if relation1 in pruned_relation:
continue
relation_set.add(relation1)
for k in range(j+1,len(v.hasStatement)):
s2 = v.hasStatement[k]
if s1.getId().__eq__(s2.getId()):
continue
start2 = s2.getStartTime()
end2 = s2.getEndTime()
relation2 = s2.getId()
if start2 != -1 or end2 != -1:
if relation2 in pruned_relation:
continue
key1=relation1+"*"+relation2
key2=relation2+"*"+relation1
index1=index_dict[key1]
index2=index_dict[key2]
#prune
if key1 not in pruned_subgraph_set:
all_relation_pairs.setdefault(index1, []).append(s1)
all_relation_pairs.setdefault(index1, []).append(s2)
# if key1 not in possible_subgraph_set:
# possible_subgraph_set.add(key1)
# relation_in_possible_set[relation1]+=1
# relation_in_possible_set[relation2]+=1
if key2 not in pruned_subgraph_set:
all_relation_pairs.setdefault(index2, []).append(s2)
all_relation_pairs.setdefault(index2, []).append(s1)
# if key2 not in possible_subgraph_set:
# possible_subgraph_set.add(key2)
# relation_in_possible_set[relation1] += 1
# relation_in_possible_set[relation2] += 1
# time1=time.time()
# print("choose time is",time1-time0,"s")
for j in all_relation_pairs.keys():
before_consistent = True
include_consistent = True
start_consistent = True
finish_consistent = True
before_negative = False
include_negative = False
start_negative = False
finish_negative = False
flag1 = True
flag2 = True
flag3 = True
flag4 = True
# total subsets+=1
# 1
# ZH_relations_statistics[j][6]+=1
# step=2
for k in range(0,len(all_relation_pairs[j]), 2):
vertex1 = all_relation_pairs[j][k]
vertex2 = all_relation_pairs[j][k + 1]
start1 = vertex1.getStartTime()
end1 = vertex1.getEndTime()
start2 = vertex2.getStartTime()
end2 = vertex2.getEndTime()
# choose which interval relation is
possible_subgraph +=1
result1=Interval_Relations.before(start1, end1, start2, end2)
result2=Interval_Relations.include(start1, end1, start2, end2)
result3=Interval_Relations.start(start1, end1, start2, end2)
result4=Interval_Relations.finish(start1, end1, start2, end2)
if result1== -1:
before_consistent=False
before_negative=True
flag1=False
elif result1==0:
before_consistent=False
if result2 == -1:
include_consistent = False
include_negative = True
flag2 = False
elif result2 == 0:
include_consistent = False
if result3 == -1:
start_consistent = False
start_negative = True
flag3 = False
elif result3 == 0:
start_consistent = False
if result4 == -1:
finish_consistent = False
finish_negative = True
flag4 = False
elif result4 == 0:
finish_consistent = False
# 1
# if before_consistent==False and include_consistent==False and start_consistent==False and finish_consistent==False:
# break
if flag1==False and flag2==False and flag3==False and flag4==False:
break
if before_consistent==True:
#before_consistent_subsets += 1
ZH_relations_statistics[j][2]+=1
# confidence = positive/positive+negative
ZH_relations_statistics[j][6] += 1
elif before_negative == True:
ZH_relations_statistics[j][6] += 1
if include_consistent==True:
#include_consistent_subsets +=1
ZH_relations_statistics[j][3] += 1
# confidence = positive/positive+negative
ZH_relations_statistics[j][7] += 1
elif include_negative == True:
ZH_relations_statistics[j][7] += 1
if start_consistent==True:
#start_consistent_subsets +=1
ZH_relations_statistics[j][4] += 1
# confidence = positive/positive+negative
ZH_relations_statistics[j][8] += 1
elif start_negative == True:
ZH_relations_statistics[j][8] += 1
if finish_consistent==True:
#finish_consistent_subsets +=1
ZH_relations_statistics[j][5] += 1
# confidence = positive/positive+negative
ZH_relations_statistics[j][9] += 1
elif finish_negative == True:
ZH_relations_statistics[j][9] += 1
#prune
#Bernoulli law of large numbers
relation1 = ZH_relations_statistics[j][0]
relation2 = ZH_relations_statistics[j][1]
before_consistent_subsets = ZH_relations_statistics[j][2]
include_consistent_subsets = ZH_relations_statistics[j][3]
start_consistent_subsets = ZH_relations_statistics[j][4]
finish_consistent_subsets = ZH_relations_statistics[j][5]
before_total_subsets = ZH_relations_statistics[j][6]
include_total_subsets = ZH_relations_statistics[j][7]
start_total_subsets = ZH_relations_statistics[j][8]
finish_total_subsets = ZH_relations_statistics[j][9]
if before_total_subsets == 0:
before_confidence = 0
else:
before_confidence = before_consistent_subsets * 1.0 / before_total_subsets
if include_total_subsets == 0:
include_confidence = 0
else:
include_confidence = include_consistent_subsets * 1.0 / include_total_subsets
if start_total_subsets == 0:
start_confidence = 0
else:
start_confidence = start_consistent_subsets * 1.0 / start_total_subsets
if finish_total_subsets == 0:
finish_confidence = 0
else:
finish_confidence = finish_consistent_subsets * 1.0 / finish_total_subsets
if before_total_subsets >=pruned_instances or include_total_subsets>=pruned_instances or \
start_total_subsets>=pruned_instances or finish_total_subsets>=pruned_instances:
if before_confidence<=pruned_threshold and include_confidence<=pruned_threshold and \
start_confidence<=pruned_threshold and finish_confidence<=pruned_threshold:
# print("pruned")
pruned_subgraph = relation1 + "*" + relation2
pruned_subgraph_set.add(pruned_subgraph)
if pruned_subgraph in possible_subgraph_set:
relation_in_possible_set[relation1]-=1
relation_in_possible_set[relation2]-=1
support_percent=0.4
if before_confidence > candidate_threshold and before_consistent_subsets > support_percent*support_threshold:
key=relation1 + "*" + relation2
if key not in possible_subgraph_set:
possible_subgraph_set.add(key)
relation_in_possible_set[relation1] += 1
relation_in_possible_set[relation2] += 1
elif include_confidence > candidate_threshold and include_consistent_subsets > support_percent*support_threshold:
key=relation1 + "*" + relation2
if key not in possible_subgraph_set:
possible_subgraph_set.add(key)
relation_in_possible_set[relation1] += 1
relation_in_possible_set[relation2] += 1
elif start_confidence > candidate_threshold and start_consistent_subsets > support_percent*support_threshold:
key=relation1 + "*" + relation2
if key not in possible_subgraph_set:
possible_subgraph_set.add(key)
relation_in_possible_set[relation1] += 1
relation_in_possible_set[relation2] += 1
elif finish_confidence > candidate_threshold and finish_consistent_subsets > support_percent*support_threshold:
key=relation1 + "*" + relation2
if key not in possible_subgraph_set:
possible_subgraph_set.add(key)
relation_in_possible_set[relation1] += 1
relation_in_possible_set[relation2] += 1
#prune
for r in relation_set:
relation_in_howmany_entities[r]+=1
if relation_in_howmany_entities[r]>=relation_prune_threshold:
if relation_in_possible_set[r]==0:
pruned_relation.add(r)
# print(relation_in_possible_set)
for f in ZH_relations_statistics:
relation1=f[0]
relation2=f[1]
# 1
# before_total_subsets= f[6]
before_consistent_subsets=f[2]
include_consistent_subsets=f[3]
start_consistent_subsets = f[4]
finish_consistent_subsets = f[5]
before_total_subsets = f[6]
include_total_subsets = f[7]
start_total_subsets = f[8]
finish_total_subsets = f[9]
# 2
if before_total_subsets==0:
before_confidence = 0
else:
before_confidence = before_consistent_subsets * 1.0 / before_total_subsets
if include_total_subsets == 0:
include_confidence = 0
else:
include_confidence = include_consistent_subsets * 1.0 / include_total_subsets
if start_total_subsets == 0:
start_confidence = 0
else:
start_confidence = start_consistent_subsets * 1.0 / start_total_subsets
if finish_total_subsets == 0:
finish_confidence = 0
else:
finish_confidence = finish_consistent_subsets * 1.0 / finish_total_subsets
# if before_consistent_subsets != 0:
# print("before relation", relation1, relation2, before_consistent_subsets, before_total_subsets, before_confidence)
# if include_consistent_subsets != 0:
# print("include relation", relation1, relation2, include_consistent_subsets, include_total_subsets, include_confidence)
# if start_consistent_subsets != 0:
# print("start relation", relation1, relation2, start_consistent_subsets, start_total_subsets, start_confidence)
# if finish_consistent_subsets != 0:
# print("finish relation", relation1, relation2, finish_consistent_subsets, finish_total_subsets, finish_confidence)
# if before_confidence > confidence_threshold and before_consistent_subsets > support_threshold:
if before_confidence > candidate_threshold and before_consistent_subsets > support_threshold:
constraint = "(a," + relation1 + ",b,t1,t2) & (a," + relation2 + ",c,t3,t4) => before(t1,t2,t3,t4)|" + str(
before_confidence)
# print(constraint)
Single_Entity_Temporal_Order_Constraint.append(constraint)
# elif include_confidence > confidence_threshold and include_consistent_subsets > support_threshold:
elif include_confidence > candidate_threshold and include_consistent_subsets > support_threshold:
constraint = "(a," + relation1 + ",b,t1,t2) & (a," + relation2 + ",c,t3,t4) => include(t1,t2,t3,t4)|" + str(
include_confidence)
# print(constraint)
Single_Entity_Temporal_Order_Constraint.append(constraint)
elif start_confidence > candidate_threshold and start_consistent_subsets > support_threshold:
constraint = "(a," + relation1 + ",b,t1,t2) & (a," + relation2 + ",c,t3,t4) => start(t1,t2,t3,t4)|" + str(
start_confidence)
# print(constraint)
Single_Entity_Temporal_Order_Constraint.append(constraint)
elif finish_confidence > candidate_threshold and finish_consistent_subsets > support_threshold:
constraint = "(a," + relation1 + ",b,t1,t2) & (a," + relation2 + ",c,t3,t4) => finish(t1,t2,t3,t4)|" + str(
finish_confidence)
# print(constraint)
Single_Entity_Temporal_Order_Constraint.append(constraint)
# print(relation_in_howmany_entities)
ed=time.time()
print("Single Entity Temporal Order Mining time is",ed-st,"s")
# print("checked subgraph",possible_subgraph)
return Single_Entity_Temporal_Order_Constraint
def Mutiple_Entity_Temporal_Order(graph,pruned_relation):
print("Mutiple Entity Temporal Order Mining......")
st = time.time()
Mutiple_Entity_Temporal_Order_Constraint = []
index_dict = {}
OH_relations = []
OH_relations_statistics = []
cou = 0
possible_subgraph=0
for i in range(len(graph.temporalRelationList)):
OH_relation1 = graph.temporalRelationList[i]
for j in range(len(graph.relationList)):
OH_relation2 = graph.relationList[j]
if OH_relation2.__eq__(OH_relation1):
continue
for k in range(len(graph.temporalRelationList)):
OH_relation3 = graph.temporalRelationList[k]
if OH_relation3.__eq__(OH_relation2):
continue
relation_pair1 = [OH_relation1, OH_relation2,OH_relation3]
OH_relations.append(relation_pair1)
OH_relations_statistics.append([OH_relation1, OH_relation2,OH_relation3,0,0,0,0,0,0,0,0,0,0,0,0,0])
# before inverse_before include inverse_include start finish total
key = OH_relation1 + "*" + OH_relation2+"*"+OH_relation3
index_dict[key] = cou
cou += 1
possible_subgraph_set = set()
pruned_subgraph_set = set()
relation_in_possible_set = {}
# relation_in_pruned_set=set()
relation_in_howmany_entities = {}
relationList=set(graph.relationList)
temporalrelationList=set(graph.temporalRelationList)
nonTemporalList=relationList.difference(temporalrelationList)
for r in nonTemporalList:
relation_in_possible_set[r] = 0
relation_in_howmany_entities[r] = 0
vertex_count = 0
pre=time.time()
for i in graph.eVertexList:
# cou+=1
# print(cou)
vertex_count += 1
if vertex_count % 1000000 == 0:
ed = time.time()
print("have traversed nodes:", vertex_count)
print("time cost:", ed - pre, "s")
v = graph.eVertexList[i]
if v.isLiteral==True:
continue
if len(v.hasStatement) < 2:
continue
else:
all_relation_pairs={}
relation_set = set()
for j in range(len(v.hasStatement)):
s1 = v.hasStatement[j]
start1 = s1.getStartTime()
end1 = s1.getEndTime()
relation1=s1.getId()
if start1 != -1 or end1 != -1:
if relation1 in pruned_relation:
continue
for k in range(len(v.hasStatement)):
if j==k:
continue
s2 = v.hasStatement[k]
one_hop=s2.getId()
if relation1.__eq__(one_hop):
continue
if one_hop in pruned_relation:
continue
if one_hop in nonTemporalList:
relation_set.add(one_hop)
for l in range(len(s2.hasValue.hasStatement)):
s3=s2.hasValue.hasStatement[l]
relation2=s3.getId()
if one_hop.__eq__(relation2):
continue
if relation2 in pruned_relation:
continue
start2 = s3.getStartTime()
end2 = s3.getEndTime()
if start2 != -1 or end2 != -1:
key1 = relation1 + "*" + one_hop+"*"+relation2
index1 = index_dict[key1]
if key1 not in pruned_subgraph_set:
all_relation_pairs.setdefault(index1, []).append(s1)
all_relation_pairs.setdefault(index1, []).append(s3)
for j in all_relation_pairs.keys():
before_consistent = True
inverse_before_consistent=True
include_consistent = True
inverse_include_consistent=True
start_consistent = True
finish_consistent = True
before_negative = False
inverse_before_negative = False
include_negative = False
inverse_include_negative = False
start_negative = False
finish_negative = False
flag1 = True
flag2 = True
flag3 = True
flag4 = True
flag5 = True
flag6 = True
# total subsets+=1
# OH_relations_statistics[j][9] += 1
# step=2
for k in range(0, len(all_relation_pairs[j]), 2):
# print(len(all_relation_pairs[j]))
vertex1 = all_relation_pairs[j][k]
vertex2 = all_relation_pairs[j][k + 1]
start1 = vertex1.getStartTime()
end1 = vertex1.getEndTime()
start2 = vertex2.getStartTime()
end2 = vertex2.getEndTime()
possible_subgraph += 1
# choose which interval relation is
result1=Interval_Relations.before(start1, end1, start2, end2)
result2 = Interval_Relations.before(start2, end2, start1, end1)
result3 = Interval_Relations.include(start1, end1, start2, end2)
result4 = Interval_Relations.include(start2, end2, start1, end1)
result5 = Interval_Relations.start(start1, end1, start2, end2)
result6 = Interval_Relations.finish(start1, end1, start2, end2)
if result1== -1:
before_consistent = False
before_negative=True
flag1=False
elif result1==0:
before_consistent=False
if result2== -1:
inverse_before_consistent = False
inverse_before_negative = True
flag2 = False
elif result2 == 0:
inverse_before_consistent = False
if result3== -1:
include_consistent = False
include_negative = True
flag3 = False
elif result3 == 0:
include_consistent = False
if result4== -1:
inverse_include_consistent = False
inverse_include_negative = True
flag4 = False
elif result4 == 0:
inverse_include_consistent = False
if result5== -1:
start_consistent = False
start_negative = True
flag5 = False
elif result5 == 0:
start_consistent = False
if result6== -1:
finish_consistent = False
finish_negative = True
flag6 = False
elif result6 == 0:
finish_consistent = False
# if before_consistent==False and inverse_before_consistent==False and include_consistent==False \
# and inverse_include_consistent==False and start_consistent==False and finish_consistent==False:
# break
if flag1==False and flag2==False and flag3==False and flag4==False and flag5==False and flag6==False:
break
if before_consistent == True:
# before_consistent_subsets += 1
OH_relations_statistics[j][3] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][9] += 1
elif before_negative == True:
OH_relations_statistics[j][9] += 1
if inverse_before_consistent == True:
# inverse_before_consistent_subsets +=1
OH_relations_statistics[j][4] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][10] += 1
elif inverse_before_negative == True:
OH_relations_statistics[j][10] += 1
if include_consistent == True:
# include_consistent_subsets +=1
OH_relations_statistics[j][5] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][11] += 1
elif include_negative == True:
OH_relations_statistics[j][11] += 1
if inverse_include_consistent == True:
# inverse_include_consistent_subsets +=1
OH_relations_statistics[j][6] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][12] += 1
elif inverse_include_negative == True:
OH_relations_statistics[j][12] += 1
if start_consistent == True:
# start_consistent_subsets +=1
OH_relations_statistics[j][7] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][13] += 1
elif start_negative == True:
OH_relations_statistics[j][13] += 1
if finish_consistent == True:
# finish_consistent_subsets +=1
OH_relations_statistics[j][8] += 1
# confidence = positive/positive+negative
OH_relations_statistics[j][14] += 1
elif finish_negative == True:
OH_relations_statistics[j][14] += 1
relation1 = OH_relations_statistics[j][0]
one_hop = OH_relations_statistics[j][1]
relation2 = OH_relations_statistics[j][2]
# 1
# total_subsets = f[9]
before_consistent_subsets = OH_relations_statistics[j][3]
inverse_before_consistent_subsets = OH_relations_statistics[j][4]
include_consistent_subsets = OH_relations_statistics[j][5]
inverse_include_consistent_subsets = OH_relations_statistics[j][6]
start_consistent_subsets = OH_relations_statistics[j][7]
finish_consistent_subsets = OH_relations_statistics[j][8]
before_total_subsets = OH_relations_statistics[j][9]
inverse_before_total_subsets = OH_relations_statistics[j][10]
include_total_subsets = OH_relations_statistics[j][11]
inverse_include_total_subsets = OH_relations_statistics[j][12]
start_total_subsets = OH_relations_statistics[j][13]
finish_total_subsets = OH_relations_statistics[j][14]
# 2
if before_total_subsets == 0:
before_confidence = 0
else:
before_confidence = before_consistent_subsets * 1.0 / before_total_subsets
if inverse_before_total_subsets == 0:
inverse_before_confidence = 0
else:
inverse_before_confidence = inverse_before_consistent_subsets * 1.0 / inverse_before_total_subsets
if include_total_subsets == 0:
include_confidence = 0
else:
include_confidence = include_consistent_subsets * 1.0 / include_total_subsets
if inverse_include_total_subsets == 0:
inverse_include_confidence = 0
else:
inverse_include_confidence = inverse_include_consistent_subsets * 1.0 / inverse_include_total_subsets
if start_total_subsets == 0:
start_confidence = 0
else:
start_confidence = start_consistent_subsets * 1.0 / start_total_subsets
if finish_total_subsets == 0:
finish_confidence = 0
else:
finish_confidence = finish_consistent_subsets * 1.0 / finish_total_subsets
if before_total_subsets >=pruned_instances or include_total_subsets>=pruned_instances or \