-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.py
2177 lines (1536 loc) · 65.8 KB
/
actions.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
from parse_en import *
from nl_to_fol import *
from phidias.Types import *
import configparser
from datetime import datetime
from owlready2 import *
import re
config = configparser.ConfigParser()
config.read('config.ini')
cnt = itertools.count(1)
dav = itertools.count(1)
VERBOSE = config.getboolean('PARSING', 'VERBOSE')
LANGUAGE = config.get('PARSING', 'LANGUAGE')
ASSIGN_RULES_LEMMAS = config.get('PARSING', 'ASSIGN_RULES_LEMMAS').split(", ")
ASSIGN_RULES_POS = config.get('PARSING', 'ASSIGN_RULES_POS').split(", ")
AXIOMS_WORDS = config.get('PARSING', 'AXIOMS_WORDS').split(", ")
LEMMATIZATION = config.getboolean('PARSING', 'LEMMATIZATION')
WAIT_TIME = config.getint('AGENT', 'WAIT_TIME')
LOG_ACTIVE = config.getboolean('AGENT', 'LOG_ACTIVE')
FILE_NAME = config.get('AGENT', 'FILE_NAME')
# REASONING Section
REASONING_ACTIVE = config.getboolean('REASONING', 'ACTIVE')
REASONER = config.get('REASONING', 'REASONER')
PREFIXES = config.get('REASONING', 'PREFIXES').split(",")
PREFIX = " ".join(PREFIXES)
PREFIX = PREFIX + f"PREFIX lodo: <http://test.org/{FILE_NAME}#> "
INCLUDE_ACT_POS = config.getboolean('POS', 'INCLUDE_ACT_POS')
INCLUDE_NOUNS_POS = config.getboolean('POS', 'INCLUDE_NOUNS_POS')
INCLUDE_ADJ_POS = config.getboolean('POS', 'INCLUDE_ADJ_POS')
INCLUDE_PRP_POS = config.getboolean('POS', 'INCLUDE_PRP_POS')
INCLUDE_ADV_POS = config.getboolean('POS', 'INCLUDE_ADV_POS')
OBJ_JJ_TO_NOUN = config.getboolean('POS', 'OBJ_JJ_TO_NOUN')
SEP = config.get('POS', 'SEP')
parser = Parse(VERBOSE)
m = ManageFols(VERBOSE, LANGUAGE)
owl_obj_dict = {}
try:
my_onto = get_ontology(FILE_NAME).load()
print("\nLoading existing "+FILE_NAME+" file...")
except IOError:
my_onto = get_ontology("http://test.org/"+FILE_NAME)
print("\nCreating new "+FILE_NAME+" file...")
print("\nPlease Re-Run Qu-LIO-XR.")
my_onto.save(file=FILE_NAME, format="rdfxml")
exit()
with my_onto:
class Id(Thing):
pass
class Verb(Thing):
pass
class Transitive(Verb):
pass
class Intransitive(Verb):
pass
class Adjective(Thing):
pass
class Adverb(Thing):
pass
class Entity(Thing):
pass
class Preposition(Thing):
pass
class hasAdj(ObjectProperty):
pass
class hasAdv(ObjectProperty):
pass
class hasObject(ObjectProperty):
pass
class hasSubject(ObjectProperty):
pass
class hasPrep(ObjectProperty):
pass
class hasId(ObjectProperty):
pass
class hasDate(DataProperty):
pass
class hasPlace(DataProperty):
pass
class hasValue(DataProperty):
range = [int]
# Ontology creation procedures
class create_onto(Procedure): pass
class process_rule(Procedure): pass
class process_onto(Procedure): pass
class create_adj(Procedure): pass
class create_adv(Procedure): pass
class create_verb(Procedure): pass
class create_assrule(Procedure): pass
class create_gnd_prep(Procedure): pass
class create_prep(Procedure): pass
class aggr_ent(Procedure): pass
class create_body(Procedure): pass
class create_head(Procedure): pass
class finalize_onto(Procedure): pass
class create_ner(Procedure): pass
class valorize(Procedure): pass
class start(Procedure): pass
# initialize Clauses Kb
# mode reactors
class LISTEN(Belief): pass
class REASON(Belief): pass
class IS_RULE(Belief): pass
class WAIT(Belief): pass
class ANSWER(Reactor): pass
# domotic reactive routines
class r1(Procedure): pass
class r2(Procedure): pass
# domotic direct commands
class d1(Procedure): pass
class d2(Procedure): pass
# domotic sensor simulatons
class s1(Procedure): pass
class s2(Procedure): pass
# Fol reasoning utterances
class c1(Procedure): pass
class c2(Procedure): pass
class c3(Procedure): pass
class c4(Procedure): pass
class c5(Procedure): pass
class c6(Procedure): pass
# normal requests beliefs
class GROUND(Belief): pass
class PRE_MOD(Belief): pass
class MOD(Belief): pass
class PRE_INTENT(Belief): pass
class INTENT(Reactor): pass
# action
class ACTION(Belief): pass
# preposition
class PREP(Belief): pass
# ground
class GND(Belief): pass
# adverb
class ADV(Belief): pass
# adjective
class ADJ(Belief): pass
# id individual
class ID(Belief): pass
# rule accumulator
class RULE(Belief): pass
# subject accumulator
class SUBJ(Belief): pass
# subject accumulator
class VALUE(Belief): pass
# parse rule beliefs
class DEP(Belief): pass
class MST_ACT(Belief): pass
class MST_VAR(Belief): pass
class MST_PREP(Belief): pass
class MST_BIND(Belief): pass
class MST_COMP(Belief): pass
class MST_COND(Belief): pass
class parse_deps(Procedure): pass
class feed_mst(Procedure): pass
class PROCESS_STORED_MST(Reactor): pass
class NER(Belief): pass
class feed_sparql(Procedure): pass
class finalize_sparql(Procedure): pass
class proc_logform(Procedure): pass
class build_logform(Procedure): pass
# Nominal query sparql belief
class SPARQL(Reactor): pass
# Pre-nominal query sparql belief
class PRE_SPARQL(Belief): pass
# Explorative query sparql belief
class EXPLO_SPARQL(Reactor): pass
# Belief from KG to build Logical forms (LF)
class VF(Belief): pass
class LF(Belief): pass
class LF_ORIGIN(Belief): pass
class PRE_LF(Belief): pass
class LF_ADV(Belief): pass
class LF_ADJ(Belief): pass
class LF_PREP(Belief): pass
class ALL(Belief): pass
class PREXR(Reactor): pass
class MODE(Belief): pass
class reset_ct(Action):
"""Reset execution time"""
def execute(self):
parser.set_start_time()
class show_ct(Action):
"""Show execution time"""
def execute(self):
ct = parser.get_comp_time()
print("\nExecution time: ", ct)
if LOG_ACTIVE:
with open("log.txt", "a") as myfile:
myfile.write("\nExecution time: "+str(ct))
class preprocess_onto(Action):
"""Producting beliefs to feed the Ontology Builder"""
def execute(self, *args):
print("\n--------- NEW ONTOLOGY ---------\n ")
deps = parser.get_last_deps()
ners = parser.get_last_ner()
print("NER: ", ners)
for ner in ners:
n = ner.split(", ")
self.assert_belief(NER(n[0][1:], n[1][:-1]))
for i in range(len(deps)):
governor = self.get_lemma(deps[i][1]).capitalize() + ":" + self.get_pos(deps[i][1])
dependent = self.get_lemma(deps[i][2]).capitalize() + ":" + self.get_pos(deps[i][2])
deps[i] = [deps[i][0], governor, dependent]
print("\n" + str(deps))
MST = parser.get_last_MST()
print("\nMST: \n" + str(MST))
print("\nGMC_SUPP: \n" + str(parser.GMC_SUPP))
print("\nGMC_SUPP_REV: \n" + str(parser.GMC_SUPP_REV))
print("\nLCD: \n" + str(parser.LCD))
# MST varlist correction on cases of adj-obj
if OBJ_JJ_TO_NOUN is True:
for v in MST[1]:
if self.get_pos(v[1]) in ['JJ', 'JJR', 'JJS']:
old_value = v[1]
new_value = self.get_lemma(v[1]) + ":NNP"
v[1] = new_value
new_value_clean = parser.get_lemma(new_value.lower())[:-2]
print("\nadj-obj correction...", new_value_clean)
# checking if the lemma has a disambiguation
if new_value_clean in parser.GMC_SUPP_REV:
parser.LCD[parser.GMC_SUPP_REV[new_value_clean]] = new_value_clean
# binds correction
for b in MST[3]:
if b[0] == old_value:
b[0] = new_value
vect_LR_fol = m.build_LR_fol(MST, 'e')
if len(vect_LR_fol) == 0:
print("\n --- IMPROPER VERBAL PHRASE COSTITUTION ---")
self.assert_belief(ANSWER("Improper verbal phrase"))
return
CHECK_IMPLICATION = m.check_implication(vect_LR_fol)
dclause = vect_LR_fol[:]
if CHECK_IMPLICATION:
dclause[1] = ["==>"]
self.assert_belief(RULE("->"))
print("\nvect_LR_fol:\n" + str(dclause))
ent_root = self.get_ent_ROOT(deps)
# IMPLICATION CASES
if dclause[1][0] == "==>":
print("\nPROCESSING LEFT HAND-SIDE...")
self.process_fol(dclause[0], "LEFT", ent_root)
print("\nPROCESSING RIGHT HAND-SIDE...")
self.process_fol(dclause[2], "RIGHT", ent_root)
# FLAT CASES
else:
self.process_fol(dclause, "FLAT", ent_root)
def get_ent_ROOT(self, deps):
for d in deps:
if d[0] == "ROOT":
return d[1]
def get_dav_rule(self, fol, ent_root):
for f in fol:
if f[0] == ent_root:
return f[1]
return False
def check_neg(self, word, language):
pos = wordnet.ADV
syns = wordnet.synsets(word, pos=pos, lang=language)
for synset in syns:
if str(synset.name()) in ['no.r.01', 'no.r.02', 'no.r.03', 'not.r.01']:
return True
return False
def get_nocount_lemma(self, lemma):
lemma_nocount = ""
total_lemma = lemma.split("_")
for i in range(len(total_lemma)):
if i == 0:
lemma_nocount = total_lemma[i].split(':')[0][:-2] + ":" + total_lemma[i].split(':')[1]
else:
lemma_nocount = total_lemma[i].split(':')[0][:-2] + ":" + total_lemma[i].split(':')[1] + "_" + lemma_nocount
return lemma_nocount
def process_fol(self, vect_fol, id, ent_root):
# prepositions
for v in vect_fol:
if len(v) == 3:
label = self.get_nocount_lemma(v[0])
if id == "LEFT":
if INCLUDE_PRP_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(PREP(str(id), v[1], lemma, v[2]))
print("PREP(" + str(id) + ", " + v[1] + ", " + lemma + ", " + v[2] + ")")
else:
if INCLUDE_PRP_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(PREP(str(id), v[1], lemma, v[2]))
print("PREP(" + str(id) + ", " + v[1] + ", " + lemma + ", " + v[2] + ")")
# actions
for v in vect_fol:
if len(v) == 4:
label = self.get_nocount_lemma(v[0])
if INCLUDE_ACT_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
if id == "FLAT" and ent_root == v[0]:
self.assert_belief(ACTION("ROOT", str(id), lemma, v[1], v[2], v[3]))
print("ACTION(ROOT, " + str(id) + ", " + lemma + ", " + v[1] + ", " + v[2] + ", " + v[3] + ")")
else:
self.assert_belief(ACTION(str(id), lemma, v[1], v[2], v[3]))
print("ACTION(" + str(id) + ", " + lemma + ", " + v[1] + ", " + v[2] + ", " + v[3] + ")")
# nouns
for v in vect_fol:
if len(v) == 2:
if self.get_pos(v[0]) in ['NNP', 'NNPS', 'PRP', 'NN', 'NNS', 'PRP', 'PRP$']:
label = self.get_nocount_lemma(v[0])
if INCLUDE_NOUNS_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(GND(str(id), v[1], lemma))
print("GND(" + str(id) + ", " + v[1] + ", " + lemma + ")")
# adjectives, adverbs
for v in vect_fol:
if self.get_pos(v[0]) in ['JJ', 'JJR', 'JJS']:
label = self.get_nocount_lemma(v[0])
if id == "LEFT":
if INCLUDE_ADJ_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(ADJ(str(id), v[1], lemma))
print("ADJ(" + str(id) + ", " + v[1] + ", " + lemma + ")")
else:
if INCLUDE_ADJ_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(ADJ(str(id), v[1], lemma))
print("ADJ(" + str(id) + ", " + v[1] + ", " + lemma + ")")
elif self.get_pos(v[0]) in ['RB', 'RBR', 'RBS', 'RP']:
label = self.get_nocount_lemma(v[0])
if id == "LEFT":
if INCLUDE_ADV_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(ADV(str(id), v[1], lemma))
print("ADV(" + str(id) + ", " + v[1] + ", " + lemma + ")")
else:
if INCLUDE_ADV_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(ADV(str(id), v[1], lemma))
print("ADV(" + str(id) + ", " + v[1] + ", " + lemma + ")")
# cardinal numbers
for v in vect_fol:
if len(v) == 2:
if self.get_pos(v[0]) == "CD":
label = self.get_nocount_lemma(v[0])
if INCLUDE_NOUNS_POS:
lemma = label
else:
lemma = parser.get_lemma(label)
self.assert_belief(VALUE(str(id), v[1], lemma))
print("VALUE(" + str(id) + ", " + v[1] + ", " + lemma + ")")
def get_pos(self, s):
first = s.split('_')[0]
s_list = first.split(':')
if len(s_list) > 1:
return s_list[1]
else:
return s_list[0]
def get_lemma(self, s):
s_list = s.split(':')
return s_list[0]
class create_sparql(Action):
"""create sparql query from MST"""
def execute(self, *args):
print("\n--------- MST ---------\n ")
MST = parser.get_last_MST()
print("\nMST: \n" + str(MST))
print("\nGMC_SUPP: \n" + str(parser.GMC_SUPP))
print("\nGMC_SUPP_REV: \n" + str(parser.GMC_SUPP_REV))
print("\nLCD: \n" + str(parser.LCD))
# ---------------------- Ontology creation Section
class WFR(ActiveBelief):
"""check if R is a Well Formed Rule"""
def evaluate(self, arg):
rule = str(arg).split("'")[3]
if rule[0] != "-" and rule[-1] != ">":
return True
else:
return False
class declareRule(Action):
"""assert an SWRL rule"""
def execute(self, arg1):
rule_str = str(arg1).split("'")[3]
print("FINALE: ", rule_str)
with my_onto:
rule = Imp()
rule.set_as_rule(rule_str)
class fillActRule(Action):
"""fills a rule with a verbal action"""
def execute(self, arg1, arg2, arg3, arg4, arg5):
rule = str(arg1).split("'")[3]
verb = str(arg2).split("'")[3].replace(":", SEP)
dav = str(arg3).split("'")[3]
subj = str(arg4).split("'")[3]
obj = str(arg5).split("'")[3]
# creating subclass of Verb
types.new_class(verb, (Transitive,))
if rule[0] == "-":
rule = "hasSubject(?"+dav+", ?"+subj+"), hasObject(?"+dav+", ?"+obj+"), "+verb+"(?"+dav+") "+rule
else:
rule = "hasSubject(?"+dav+", ?"+subj+"), hasObject(?"+dav+", ?"+obj+"), "+verb+"(?"+dav+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillPassActRule(Action):
"""fills a rule with a passive verbal action"""
def execute(self, arg1, arg2, arg3, arg4):
rule = str(arg1).split("'")[3]
verb = str(arg2).split("'")[3].replace(":", SEP)
dav = str(arg3).split("'")[3]
obj = str(arg4).split("'")[3].replace(":", SEP)
# creating subclass of Verb
types.new_class(verb, (Intransitive,))
if rule[0] == "-":
rule = "hasObject(?"+dav+", ?"+obj+"), "+verb+"(?"+dav+") "+rule
else:
rule = "hasObject(?"+dav+", ?"+obj+"), "+verb+"(?"+dav+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillIntraActRule(Action):
"""fills a rule with an intransitive verbal action"""
def execute(self, arg1, arg2, arg3, arg4):
rule = str(arg1).split("'")[3]
verb = str(arg2).split("'")[3].replace(":", SEP)
dav = str(arg3).split("'")[3]
subj = str(arg4).split("'")[3].replace(":", SEP)
# creating subclass of Verb
types.new_class(verb, (Intransitive,))
if rule[0] == "-":
rule = "hasSubject(?"+dav+", ?"+subj+"), "+verb+"(?"+dav+") "+rule
else:
rule = "hasSubject(?"+dav+", ?"+subj+"), "+verb+"(?"+dav+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillGndRule(Action):
"""fills a rule with a ground"""
def execute(self, arg0, arg1, arg2, arg3):
hand_side = str(arg0).split("'")[1]
rule = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
value = str(arg3).split("'")[3].replace(":", SEP)
# creating subclass of Entity
types.new_class(value, (Entity,))
if hand_side == "LEFT":
if rule[0] == "-":
rule = value+"(?"+var+") "+rule
else:
rule = value +"(?"+var+"), "+rule
else:
if rule[-1] == ">":
rule = rule+" "+value+"(?"+var+")"
else:
rule = rule+", "+value+"(?"+var+")"
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillHeadAdjRule(Action):
"""fills a rule with an adjective"""
def execute(self, arg1, arg2, arg3):
rule = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
adj_str = str(arg3).split("'")[3].replace(":", SEP)
# creating subclass of Adjective
types.new_class(adj_str, (Adjective,))
rule = rule+" "+adj_str+"(?"+var+")"
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillAdjRule(Action):
"""fills a rule with an adjective"""
def execute(self, arg1, arg2, arg3):
rule = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
adj_str = str(arg3).split("'")[3].replace(":", SEP)
# creating subclass of Adjective
types.new_class(adj_str, (Adjective,))
new_var = "x" + str(next(cnt))
if rule[0] == "-":
rule = "hasAdj(?"+var+", ?"+new_var+"), "+adj_str+"(?"+new_var+") "+rule
else:
rule = "hasAdj(?"+var+", ?"+new_var+"), "+adj_str+"(?"+new_var+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillAdvRule(Action):
"""fills a rule with an adverb"""
def execute(self, arg1, arg2, arg3):
rule = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
adv_str = str(arg3).split("'")[3].replace(":", SEP)
# creating subclass of Adverb
types.new_class(adv_str, (Adverb,))
new_var = "x" + str(next(cnt))
if rule[0] == "-":
rule = "hasAdv(?"+var+", ?"+new_var+"), "+adv_str+"(?"+new_var+") "+rule
else:
rule = "hasAdv(?"+var+", ?"+new_var+"), "+adv_str+"(?"+new_var+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillPrepRule(Action):
"""fills a rule with a preposition"""
def execute(self, arg0, arg1, arg2, arg3, arg4):
hand_side = str(arg0).split("'")[1]
rule = str(arg1).split("'")[3]
var_master = str(arg2).split("'")[3]
value = str(arg3).split("'")[3].replace(":", SEP)
var_slave = str(arg4).split("'")[3]
# creating subclass of preposition
types.new_class(value, (Preposition,))
new_index_var = str(next(cnt))
if hand_side == "LEFT":
if rule[0] == "-":
rule = "hasPrep(?"+var_master+", ?x"+new_index_var+"), "+value+"(?x"+new_index_var+"), hasObject(?x"+new_index_var+", ?"+var_slave+") "+rule
else:
rule = "hasPrep(?"+var_master+", ?x"+new_index_var+"), "+value+"(?x"+new_index_var+"), hasObject(?x"+new_index_var+", ?"+var_slave+"), " + rule
else:
if rule[-1] == ">":
rule = rule+" hasPrep(?"+var_master+", ?x"+new_index_var+"), "+value+"(?x"+new_index_var+"), hasObject(?x"+new_index_var+", ?"+var_slave+")"
else:
rule = rule+", hasPrep(?"+var_master+", ?x"+new_index_var+"), "+value+"(?x"+new_index_var+"), hasObject(?x"+new_index_var+", ?"+var_slave+")"
print("rule: ", rule)
self.assert_belief(RULE(rule))
class fillOpRule(Action):
"""fills with comparison operators"""
def execute(self, arg1, arg2, arg3):
rule = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
val_str = str(arg3).split("'")[3]
new_index_var = str(next(cnt))
if rule[0] == "-":
rule = "hasValue(?"+var+", ?x"+new_index_var+"), greaterThan(?x"+new_index_var+", "+val_str+") "+rule
else:
rule = "hasValue(?"+var+", ?x"+new_index_var+"), greaterThan(?x"+new_index_var+", "+val_str+"), "+rule
print("rule: ", rule)
self.assert_belief(RULE(rule))
class aggrEntity(Action):
"""aggregate two entity beliefs in one"""
def execute(self, arg1, arg2, arg3, arg4):
id = str(arg1).split("'")[3]
var = str(arg2).split("'")[3]
label1 = str(arg3).split("'")[3]
label2 = str(arg4).split("'")[3]
conc_label = label2 + "_" + label1
self.assert_belief(GND(id, var, conc_label))
class applyAdv(Action):
"""create an entity and apply an adj to it"""
def execute(self, arg1, arg2, arg3):
id_str = str(arg1).split("'")[3]
verb_str = str(arg2).split("'")[3].replace(":", SEP)
adv_str = str(arg3).split("'")[3].replace(":", SEP)
# creating subclass adjective
adv = types.new_class(adv_str, (Adverb,))
# adverb individual
new_adv_ind = adv(parser.clean_from_POS(adv_str)+"."+id_str)
# creating subclass entity
new_sub = types.new_class(verb_str, (Verb,))
# creating entity individual
new_ind = new_sub(parser.clean_from_POS(verb_str)+"."+id_str)
# individual entity - hasAdv - adverb individual
new_ind.hasAdv.append(new_adv_ind)
class createAdj(Action):
"""create an entity and apply an adj to it"""
def execute(self, arg0, arg1, arg2):
id_str = str(arg0).split("'")[3]
ent_str = str(arg1).split("'")[3].replace(":", SEP)
adj_str = str(arg2).split("'")[3].replace(":", SEP)
# creating subclass adjective
adv = types.new_class(adj_str, (Adjective,))
# adverb individual
new_adj_ind = adv(parser.clean_from_POS(adj_str)+"."+id_str)
# creating subclass entity
ent_sub = types.new_class(ent_str, (Entity,))
# creating entity individual
new_ind = ent_sub(parser.clean_from_POS(ent_str)+"."+id_str)
# individual entity - hasAdv - adverb individual
new_ind.hasAdj.append(new_adj_ind)
class createSubCustVerb(Action):
"""Creating a subclass of the class Verb"""
def execute(self, arg1, arg2, arg3, arg4):
id_str = str(arg1).split("'")[3]
verb_str = str(arg2).split("'")[3].replace(":", SEP)
subj_str = str(arg3).split("'")[3].replace(":", SEP)
obj_str = str(arg4).split("'")[3].replace(":", SEP)
# subclasses
new_sub_verb = types.new_class(verb_str, (Transitive,))
new_sub_subj = types.new_class(subj_str, (Entity,))
new_sub_obj = types.new_class(obj_str, (Entity,))
# entities individual
new_ind_id = Id(id_str)
new_ind_verb = new_sub_verb(parser.clean_from_POS(verb_str)+"."+id_str)
new_ind_subj = new_sub_subj(parser.clean_from_POS(subj_str)+"."+id_str)
new_ind_obj = new_sub_obj(parser.clean_from_POS(obj_str)+"."+id_str)
# individual entity - hasSubject - subject individual
new_ind_verb.hasSubject = [new_ind_subj]
# individual entity - hasObject - Object individual
new_ind_verb.hasObject = [new_ind_obj]
# storing action's id
new_ind_verb.hasId = [new_ind_id]
class createSubVerb(Action):
"""Creating a subclass of the class Verb"""
def execute(self, arg1, arg2, arg3, arg4):
id_str = str(arg1).split("'")[3]
verb_str = str(arg2).split("'")[3].replace(":", SEP)
subj_str = str(arg3).split("'")[3].replace(":", SEP)
obj_str = str(arg4).split("'")[3].replace(":", SEP)
# subclasses
new_sub_verb = types.new_class(verb_str, (Transitive,))
new_sub_subj = types.new_class(subj_str, (Entity,))
new_sub_obj = types.new_class(obj_str, (Entity,))
# entities individual
new_ind_id = Id(id_str)
new_ind_verb = new_sub_verb(parser.clean_from_POS(verb_str)+"."+id_str)
new_ind_subj = new_sub_subj(parser.clean_from_POS(subj_str)+"."+id_str)
new_ind_obj = new_sub_obj(parser.clean_from_POS(obj_str)+"."+id_str)
# individual entity - hasSubject - subject individual
new_ind_verb.hasSubject = [new_ind_subj]
# individual entity - hasObject - Object individual
new_ind_verb.hasObject = [new_ind_obj]
# storing action's id
new_ind_verb.hasId = [new_ind_id]
class createEmbVerbs(Action):
"""Creating subclasses of the class Verb/Entity + embedded individuals"""
def execute(self, arg1, arg2, arg3, arg4, arg5, arg6):
id_str = str(arg1).split("'")[3]
main_verb_str = str(arg2).split("'")[3].replace(":", SEP)
main_subj_str = str(arg3).split("'")[3].replace(":", SEP)
emb_verb_str = str(arg4).split("'")[3].replace(":", SEP)
emb_subj_str = str(arg5).split("'")[3].replace(":", SEP)
emb_obj_str = str(arg6).split("'")[3].replace(":", SEP)
# subclasses
main_sub_verb = types.new_class(main_verb_str, (Transitive,))
main_sub_subj = types.new_class(main_subj_str, (Entity,))
emb_sub_verb = types.new_class(emb_verb_str, (Transitive,))
emb_sub_subj = types.new_class(emb_subj_str, (Entity,))
emb_sub_obj = types.new_class(emb_obj_str, (Entity,))
# individuals
new_ind_id = Id(id_str)
new_ind_main_verb = main_sub_verb(parser.clean_from_POS(main_verb_str)+"."+id_str)
new_ind_main_subj = main_sub_subj(parser.clean_from_POS(main_subj_str)+"."+id_str)
new_ind_emb_verb = emb_sub_verb(parser.clean_from_POS(emb_verb_str)+"."+id_str)
new_ind_emb_subj = emb_sub_subj(parser.clean_from_POS(emb_subj_str)+"."+id_str)
new_ind_emb_obj = emb_sub_obj(parser.clean_from_POS(emb_obj_str)+"."+id_str)
# main
new_ind_main_verb.hasSubject = [new_ind_main_subj]
new_ind_main_verb.hasObject = [new_ind_emb_verb]
# embedded
new_ind_emb_verb.hasSubject = [new_ind_emb_subj]
new_ind_emb_verb.hasObject = [new_ind_emb_obj]
# storing action's id
new_ind_main_verb.hasId = [new_ind_id]
new_ind_emb_verb.hasId = [new_ind_id]
class createAssRule(Action):
"""Creating new assignment rule between entities"""
def execute(self, arg1, arg2):
ent1 = str(arg1).split("'")[3].replace(":", SEP)
ent2 = str(arg2).split("'")[3].replace(":", SEP)
types.new_class(ent1, (Entity,))
types.new_class(ent2, (Entity,))
rule_str = ent1+"(?x) -> "+ent2+"(?x)"
rule_adj_legacy = ent1+"(?x2), "+ent2+"(?x1), hasAdj(?x1, ?x3), Adjective(?x3) -> hasAdj(?x2, ?x3)"
print("New assignment rule: ", rule_str)
print("New legacy rule: ", rule_adj_legacy)
with my_onto:
rule1 = Imp()
rule1.set_as_rule(rule_str)
rule2 = Imp()
rule2.set_as_rule(rule_adj_legacy)
class createPassSubVerb(Action):
"""Creating a subclass of the class Verb (passive)"""
def execute(self, arg1, arg2, arg3):
id_str = str(arg1).split("'")[3]
verb_str = str(arg2).split("'")[3].replace(":", SEP)
obj_str = str(arg3).split("'")[3].replace(":", SEP)
# subclasses
new_sub_verb = types.new_class(verb_str, (Verb,))
new_sub_obj = types.new_class(obj_str, (Entity,))
# entities individual
new_ind_id = Id(id_str)
new_ind_verb = new_sub_verb(parser.clean_from_POS(verb_str)+"."+id_str)
new_ind_obj = new_sub_obj(parser.clean_from_POS(obj_str)+"."+id_str)
# individual entity - hasObject - Object individual
new_ind_verb.hasObject = [new_ind_obj]
# storing action's id
new_ind_verb.hasId = [new_ind_id]
class createIntrSubVerb(Action):
"""Creating a subclass of the class Verb (Intransitive)"""
def execute(self, arg1, arg2, arg3):
id_str = str(arg1).split("'")[3]
verb_str = str(arg2).split("'")[3].replace(":", SEP)
subj_str = str(arg3).split("'")[3].replace(":", SEP)
# subclasses
new_sub_verb = types.new_class(verb_str, (Intransitive, ))
new_sub_subj = types.new_class(subj_str, (Entity,))
# entities individual
new_ind_id = Id(id_str)
new_ind_verb = new_sub_verb(parser.clean_from_POS(verb_str)+"."+id_str)
# new_ind_verb.is_a.append(Intransitive)
new_ind_subj = new_sub_subj(parser.clean_from_POS(subj_str)+"."+id_str)
# individual entity - hasSubject - subject individual
new_ind_verb.hasSubject = [new_ind_subj]
# storing action's id
new_ind_verb.hasId = [new_ind_id]
class createSubPrep(Action):
"""Creating a subclass of depending action preposition"""
def execute(self, arg0, arg1, arg2, arg3):
id_str = str(arg0).split("'")[3]
verb = str(arg1).split("'")[3].replace(":", SEP)
prep = str(arg2).split("'")[3].replace(":", SEP)
ent = str(arg3).split("'")[3].replace(":", SEP)
v = parser.clean_from_POS(verb) + "." + id_str
if v in owl_obj_dict:
print("Getting objects from dict....", owl_obj_dict[v])
# Getting object from dict
new_ind_verb = owl_obj_dict[v]
else:
print("Creating objects....")
# Creating subclass of Verb and individual
new_sub_verb = types.new_class(verb, (Transitive,))
new_ind_verb = new_sub_verb(v)
# Updating owl object dict
owl_obj_dict[verb] = new_sub_verb
owl_obj_dict[v] = new_ind_verb
# Creating subclass of Preposition and individual
new_sub_prep = types.new_class(prep, (Preposition,))
new_ind_prep = new_sub_prep(parser.clean_from_POS(prep) + "." + id_str)
# Creating subclass of Entity and individual
new_sub_ent = types.new_class(ent, (Entity,))
new_ind_ent = new_sub_ent(parser.clean_from_POS(ent) + "." + id_str)
# Creating objects properties
new_ind_verb.hasPrep.append(new_ind_prep)
new_ind_prep.hasObject.append(new_ind_ent)
class createSubPassPrep(Action):
"""Creating a subclass of depending passive action preposition"""
def execute(self, arg0, arg1, arg2, arg3):
id_str = str(arg0).split("'")[3]
verb = str(arg1).split("'")[3].replace(":", SEP)
prep = str(arg2).split("'")[3].replace(":", SEP)
ent = str(arg3).split("'")[3].replace(":", SEP)
v = parser.clean_from_POS(verb) + "." + id_str