-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprolog_api.py
More file actions
1001 lines (803 loc) · 36.5 KB
/
prolog_api.py
File metadata and controls
1001 lines (803 loc) · 36.5 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
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 enum import Enum
from pyswip import Prolog
import os
class Operator(Enum):
"""This class will be used to identify the state of the program."""
IDLE = 0
ASSERTION = 1
HYPERNYM = 2
ENTAILMENT = 3
SIMILARITY = 4
MERONYM_HOLONYM = 5
CAUSED = 6
ATTRIBUTE = 7
ANTONYM = 8
SA = 9
PARTICIPLE = 10
PERTAINS = 11
def __str__(self):
if self == Operator.ASSERTION:
return "Assertion"
elif self == Operator.HYPERNYM:
return "Hypernym"
elif self == Operator.ENTAILMENT:
return "Entailment"
elif self == Operator.SIMILARITY:
return "Similarity"
elif self == Operator.MERONYM_HOLONYM:
return "Meronym/Holonym"
elif self == Operator.CAUSED:
return "Caused"
elif self == Operator.ATTRIBUTE:
return "Attribute"
elif self == Operator.ANTONYM:
return "Antonym"
elif self == Operator.SA:
return "Adicional information"
elif self == Operator.PARTICIPLE:
return "Participle"
elif self == Operator.PERTAINS:
return "Pertains"
else:
return "Unknown"
class Consulter:
def __init__(self):
# Create the prolog object.
self.prolog = Prolog()
# Consult the prolog files.
self.load_consults()
self.result_string = ""
def load_consults(self):
"""Load every prolog file in prolog_files."""
for file in os.listdir("prolog_files"):
if file.endswith(".pl"):
self.prolog.consult("prolog_files/" + file)
def receive_query(self, operator: Operator, word_1: str, word_2: str):
"""Receive two words and the operator to apply, and set the query"""
self.operator = operator
self.word_1 = WordInfo(word_1)
self.word_2 = WordInfo(word_2)
def process_query(self):
"""Perform the operation in the query"""
# Check for the Assertion operator.
word_selector = 1 if self.word_1.word != "" else 2
both_words = self.word_1.word != "" and self.word_2.word != ""
# Get info of the word 1 and 2
self.fill_word_info(1)
self.fill_word_info(2)
# Check for the Assertion operator.
if self.operator == Operator.ASSERTION:
self.result_string = self.assertion()
# Check for the Similarity operator.
elif self.operator == Operator.SIMILARITY:
if both_words:
self.result_string = self.similarity_1_to_2()
else:
self.result_string = self.similarity_1_to_all(word_selector)
# Check for the Antonym operator.
elif self.operator == Operator.ANTONYM:
if both_words:
self.result_string = self.antonym_1_to_2()
else:
self.result_string = self.antonym_1_to_all(word_selector)
# Check for the Hypernym operator.
elif self.operator == Operator.HYPERNYM:
if both_words:
self.result_string = self.is_hypernym()
elif word_selector == 1:
self.result_string = self.hypernym_of()
else:
self.result_string = self.inverse_hypernym()
# Check for the Entailment operator.
elif self.operator == Operator.ENTAILMENT:
if both_words:
self.result_string = self.is_entailment()
elif word_selector == 1:
self.result_string = self.entailment_of()
else:
self.result_string = self.inverse_entailment()
# Check for meronym holonym
elif self.operator == Operator.MERONYM_HOLONYM:
operations = ["mm", "ms", "mp"]
if both_words:
self.result_string = self.is_meronym_holonym()
elif word_selector == 1:
for operation in operations:
self.result_string += self.mer_hol("Meronym", operation)
else:
for operation in operations:
self.result_string += self.mer_hol("Holonym", operation)
# Check for the Caused operator.
elif self.operator == Operator.CAUSED:
self.result_string = self.caused()
# Check for the Attribute operator.
elif self.operator == Operator.ATTRIBUTE:
if both_words:
self.result_string = self.is_attribute()
else:
self.result_string = self.attribute_of(word_selector)
# Check for the Adicional information operator.
elif self.operator == Operator.SA:
self.result_string = self.sa()
# Check for the Participle operator.
elif self.operator == Operator.PARTICIPLE:
if both_words:
self.result_string = self.is_participle()
else:
self.result_string = self.participle_of(word_selector)
# Check for the Pertains operator.
elif self.operator == Operator.PERTAINS:
self.result_string = self.pertains()
else:
self.result_string = "Please select an operator."
def assertion(self):
"""Search all possible meanings of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the word info through the g predicate.
for synset_id in self.word_1.synset_id_list:
g_result = self.make_consult(f"g({synset_id}, Gloss)")
self.word_1.gloss_list.append(g_result[0]["Gloss"])
# Create the result string.
result_string = ""
for i in range(len(self.word_1.synset_id_list)):
result_string += f"{self.word_1.word}:\n ({self.word_1.word_type_list[i]}):\n {self.word_1.gloss_list[i]}\n"
return result_string
def caused(self):
"""Use the cs predicate to search for the cause of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the cause of the word
cause_synset_list = []
for synset_id in self.word_1.synset_id_list:
cause_result = self.make_consult(f"cs({synset_id}, CauseID)")
cause_synset_list.append(cause_result)
# Get all the words in the synset
cause_words_list = []
gloss_list = []
for cause_synset in cause_synset_list:
for cause_word in cause_synset:
cause_words_list.append(self.get_all_words(cause_word["CauseID"]))
gloss_list.append(
self.make_consult(f"g({cause_word['CauseID']}, Gloss)")[0]["Gloss"]
)
# Create the result string.
start_string = f"Cause words of {self.word_1.word}:\n\n"
result_string = start_string
for i in range(len(cause_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in cause_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == start_string:
return "No cause words were found."
return result_string
def sa(self):
"""Use the sa predicate to search for words that adds adicional information to the word 1."""
# sa/4 receive the word synset id and word number, and return the synset id and word number of the words that adds adicional information.
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the words that adds adicional information to the word 1.
sa_synset_list = []
for i in range(len(self.word_1.synset_id_list)):
sa_result = self.make_consult(
f"sa({self.word_1.synset_id_list[i]}, {self.word_1.word_number_list[i]}, SynsetID, WordNumber)"
)
sa_synset_list.append(sa_result)
# Get all the words in the synset
sa_words_list = []
gloss_list = []
for sa_synset in sa_synset_list:
for sa_word in sa_synset:
sa_words_list.append(self.get_all_words(sa_word["SynsetID"]))
gloss_list.append(
self.make_consult(f"g({sa_word['SynsetID']}, Gloss)")[0]["Gloss"]
)
# Create the result string.
start_string = (
f"Words that adds adicional information to {self.word_1.word}:\n\n"
)
result_string = start_string
for i in range(len(sa_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in sa_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == start_string:
return "No words that adds adicional information were found."
return result_string
def pertains(self):
"""Using the per/4 predicate, search for the words that word 1 pertains to."""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the words that adds adicional information to the word 1.
per_synset_list = []
for i in range(len(self.word_1.synset_id_list)):
per_result = self.make_consult(
f"per({self.word_1.synset_id_list[i]}, {self.word_1.word_number_list[i]}, SynsetID, WordNumber)"
)
per_synset_list.append(per_result)
# Get all the words in the synset
per_words_list = []
gloss_list = []
for per_synset in per_synset_list:
for per_word in per_synset:
per_words_list.append(self.get_all_words(per_word["SynsetID"]))
gloss_list.append(
self.make_consult(f"g({per_word['SynsetID']}, Gloss)")[0]["Gloss"]
)
# Create the result string.
result_string = f"Words that {self.word_1.word} pertains to:\n\n"
for i in range(len(per_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in per_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Words that {self.word_1.word} pertains to:\n\n":
return f"No words that {self.word_1.word} pertains to were found."
return result_string
# region Participle
def participle_of(self, word_indicator: int = 1):
"""Using the ppl/4 predicate, search for the participle of the word 1 and 2.
Args:
word_indicator (int, optional): Selector of the word that will be used. Defaults to 1.
Returns:
_type_: String with the result.
"""
temp_word = self.word_1 if word_indicator == 1 else self.word_2
if not temp_word.exist:
return self.not_found(temp_word.word)
# Get all the participle of the word
participle_synset_list = []
for i in range(len(temp_word.synset_id_list)):
if word_indicator == 1:
participle_result = self.make_consult(
f"ppl({temp_word.synset_id_list[i]},{temp_word.word_number_list[i]}, SynsetID, WordNumber)"
)
else:
participle_result = self.make_consult(
f"ppl(SynsetID, WordNumber,{temp_word.synset_id_list[i]},{temp_word.word_number_list[i]})"
)
participle_synset_list.append(participle_result)
# Get the words indexing with the word number. This will consult s/6 with the synset id and the word number.
participle_words_list = []
for participle_synset in participle_synset_list:
for participle_word in participle_synset:
participle_words_list.append(
self.make_consult(
f"s({participle_word['SynsetID']}, {participle_word['WordNumber']}, Word, _, _, _)"
)[0]["Word"]
)
# Get the gloss of the participle synset.
gloss_list = []
for participle_synset in participle_synset_list:
for participle_word in participle_synset:
gloss_list.append(
self.make_consult(f"g({participle_word['SynsetID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"Participle words of {temp_word.word}:\n\n"
for i in range(len(participle_words_list)):
result_string += f"{gloss_list[i]}:\n "
result_string += f"\t{participle_words_list[i]}"
result_string += "\n\n"
if result_string == f"Participle words of {temp_word.word}:\n\n":
return "No participle words were found."
return result_string
def is_participle(self):
"""Search if the word 2 is participle of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Call ppl predicate with the word 1 and 2 for every synset id.
for synset_id_1 in self.word_1.synset_id_list:
for synset_id_2 in self.word_2.synset_id_list:
ppl_result = self.make_consult(
f"ppl({synset_id_1}, {synset_id_2}, _, _)"
)
if ppl_result != []:
return f"{self.word_2.word} is participle of {self.word_1.word}."
return f"{self.word_2.word} is not participle of {self.word_1.word}"
# endregion
# region Similarity
def similarity_1_to_all(self, word_indicator: int = 1):
"""Search all possible meanings of the word 1 and 2"""
self.fill_word_info(word_indicator)
temp_word = self.word_1 if word_indicator == 1 else self.word_2
if not temp_word.exist:
return self.not_found(temp_word.word)
# Get all the similar of the word
similar_synset_list = []
for synset_id in temp_word.synset_id_list:
similar_result = self.make_consult(f"sim({synset_id}, SimilarID)")
similar_synset_list.append(similar_result)
# Get all the words in the synset
similar_words_list = []
gloss_list = []
for similar_synset in similar_synset_list:
for similar_word in similar_synset:
similar_words_list.append(self.get_all_words(similar_word["SimilarID"]))
gloss_list.append(
self.make_consult(f"g({similar_word['SimilarID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"Similar words with {temp_word.word}:\n\n"
for i in range(len(similar_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in similar_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Similar words with {temp_word.word}:\n\n":
return "No similar words were found."
return result_string
def similarity_1_to_2(self):
"""Search if the two given words are similar"""
# Fill words info
# Check if the words exist
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the similar of the word 1
similar_synset_list = []
for synset_id in self.word_1.synset_id_list:
similar_result = self.make_consult(f"sim({synset_id}, SimilarID)")
similar_synset_list.append(similar_result)
# Get all the words in the synset
similar_words_list = []
gloss_list = []
for similar_synset in similar_synset_list:
for similar_word in similar_synset:
similar_words_list.append(self.get_all_words(similar_word["SimilarID"]))
gloss_list.append(
self.make_consult(f"g({similar_word['SimilarID']}, Gloss)")[0][
"Gloss"
]
)
# Check if the word 2 is in the similar words list
for i in range(len(similar_words_list)):
if self.word_2.word in similar_words_list[i]:
return f"{self.word_1.word} and {self.word_2.word} are similar.\n\n{gloss_list[i]}:\n\t{self.word_2.word}"
return f"{self.word_1.word} and {self.word_2.word} are not similar."
# endregion
# region Antonym
def antonym_1_to_all(self, word_indicator: int = 1):
"""Search all possible meanings of the word 1 and 2"""
self.fill_word_info(word_indicator)
temp_word = self.word_1 if word_indicator == 1 else self.word_2
if not temp_word.exist:
return self.not_found(temp_word.word)
# Get all the antonyms of the word
antonym_synset_list = []
for i in range(len(temp_word.synset_id_list)):
antonym_result = self.make_consult(
f"ant({temp_word.synset_id_list[i]},{temp_word.word_number_list[i]}, SynsetID, WordNumber)"
)
antonym_synset_list.append(antonym_result)
# Get the words indexing with the word number. This will consult s/6 with the synset id and the word number.
antonym_words_list = []
for antonym_synset in antonym_synset_list:
for antonym_word in antonym_synset:
antonym_words_list.append(
self.make_consult(
f"s({antonym_word['SynsetID']}, {antonym_word['WordNumber']}, Word, _, _, _)"
)[0]["Word"]
)
# Get the gloss of the antonym synset.
gloss_list = []
for antonym_synset in antonym_synset_list:
for antonym_word in antonym_synset:
gloss_list.append(
self.make_consult(f"g({antonym_word['SynsetID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"Antonym words with {temp_word.word}:\n\n"
for i in range(len(antonym_words_list)):
result_string += f"{gloss_list[i]}:\n "
result_string += f"\t{antonym_words_list[i]}"
result_string += "\n\n"
if result_string == f"Antonym words with {temp_word.word}:\n\n":
return "No antonym words were found."
return result_string
def antonym_1_to_2(self):
"""Check if word 2 is antonym of word 1"""
# Fill words info
# Check if the words exist
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the antonyms of the word 1
antonym_synset_list = []
for synset_id in self.word_1.synset_id_list:
antonym_result = self.make_consult(f"ant({synset_id}, AntonymID)")
antonym_synset_list.append(antonym_result)
# Get all the words in the synset
antonym_words_list = []
gloss_list = []
for antonym_synset in antonym_synset_list:
for antonym_word in antonym_synset:
antonym_words_list.append(self.get_all_words(antonym_word["AntonymID"]))
gloss_list.append(
self.make_consult(f"g({antonym_word['AntonymID']}, Gloss)")[0][
"Gloss"
]
)
# Check if the word 2 is in the antonym words list
for i in range(len(antonym_words_list)):
if self.word_2.word in antonym_words_list[i]:
return f"{self.word_2.word} is antonym of {self.word_1.word}.\n\n{gloss_list[i]}:\n\t{self.word_1.word}"
return f"{self.word_2.word} is not antonym of {self.word_1.word}"
# endregion
# region Hypernym
def hypernym_of(self):
"""Search all possible meanings of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the hypernym of the word
hypernym_synset_list = []
for synset_id in self.word_1.synset_id_list:
hypernym_result = self.make_consult(f"hyp({synset_id}, HypernymID)")
hypernym_synset_list.append(hypernym_result)
# Get all the words in the synset
hypernym_words_list = []
gloss_list = []
for hypernym_synset in hypernym_synset_list:
for hypernym_word in hypernym_synset:
hypernym_words_list.append(
self.get_all_words(hypernym_word["HypernymID"])
)
gloss_list.append(
self.make_consult(f"g({hypernym_word['HypernymID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"Hypernym words of {self.word_1.word}:\n\n"
for i in range(len(hypernym_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in hypernym_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Hypernym words of {self.word_1.word}:\n\n":
return "No hypernym words were found."
return result_string
def inverse_hypernym(self):
"""Search for all the words that the word 2 is hypernym of"""
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the inverse hypernym of the word
inverse_hypernym_synset_list = []
for synset_id in self.word_2.synset_id_list:
inverse_hypernym_result = self.make_consult(f"hyp(SynsetID, {synset_id})")
inverse_hypernym_synset_list.append(inverse_hypernym_result)
# Get all the words in the synset
inverse_hypernym_words_list = []
gloss_list = []
for inverse_hypernym_synset in inverse_hypernym_synset_list:
for inverse_hypernym_word in inverse_hypernym_synset:
inverse_hypernym_words_list.append(
self.get_all_words(inverse_hypernym_word["SynsetID"])
)
gloss_list.append(
self.make_consult(f"g({inverse_hypernym_word['SynsetID']}, Gloss)")[
0
]["Gloss"]
)
# Create the result string.
result_string = f"Inverse hypernym words of {self.word_2.word}:\n\n"
for i in range(len(inverse_hypernym_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in inverse_hypernym_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Inverse hypernym words of {self.word_2.word}:\n\n":
return "No inverse hypernym words were found."
return result_string
def is_hypernym(self):
"""Search if the word 2 is hypernym of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the hypernym of the word 1
hypernym_synset_list = []
for synset_id in self.word_1.synset_id_list:
hypernym_result = self.make_consult(f"hyp({synset_id}, HypernymID)")
hypernym_synset_list.append(hypernym_result)
# Get all the words in the synset
hypernym_words_list = []
gloss_list = []
for hypernym_synset in hypernym_synset_list:
for hypernym_word in hypernym_synset:
hypernym_words_list.append(
self.get_all_words(hypernym_word["HypernymID"])
)
gloss_list.append(
self.make_consult(f"g({hypernym_word['HypernymID']}, Gloss)")[0][
"Gloss"
]
)
# Check if the word 2 is in the hypernym words list
for i in range(len(hypernym_words_list)):
if self.word_2.word in hypernym_words_list[i]:
return f"{self.word_2.word} is hypernym of {self.word_1.word}.\n\n{gloss_list[i]}:\n\t{self.word_1.word}"
return f"{self.word_2.word} is not hypernym of {self.word_1.word}."
# endregion
# region Entailment
def entailment_of(self):
"""Search all possible meanings of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
# Get all the entailment of the word
entailment_synset_list = []
for synset_id in self.word_1.synset_id_list:
entailment_result = self.make_consult(f"ent({synset_id}, EntailmentID)")
entailment_synset_list.append(entailment_result)
# Get all the words in the synset
entailment_words_list = []
gloss_list = []
for entailment_synset in entailment_synset_list:
for entailment_word in entailment_synset:
entailment_words_list.append(
self.get_all_words(entailment_word["EntailmentID"])
)
gloss_list.append(
self.make_consult(f"g({entailment_word['EntailmentID']}, Gloss)")[
0
]["Gloss"]
)
# Create the result string.
result_string = f"Entailment words of {self.word_1.word}:\n\n"
for i in range(len(entailment_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in entailment_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Entailment words of {self.word_1.word}:\n\n":
return "No entailment words were found."
return result_string
def inverse_entailment(self):
"""Search for all the words that the word 2 is entailment of"""
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the inverse entailment of the word
inverse_entailment_synset_list = []
for synset_id in self.word_2.synset_id_list:
inverse_entailment_result = self.make_consult(f"ent(SynsetID, {synset_id})")
inverse_entailment_synset_list.append(inverse_entailment_result)
# Get all the words in the synset
inverse_entailment_words_list = []
gloss_list = []
for inverse_entailment_synset in inverse_entailment_synset_list:
for inverse_entailment_word in inverse_entailment_synset:
inverse_entailment_words_list.append(
self.get_all_words(inverse_entailment_word["SynsetID"])
)
gloss_list.append(
self.make_consult(
f"g({inverse_entailment_word['SynsetID']}, Gloss)"
)[0]["Gloss"]
)
# Create the result string.
result_string = f"Inverse entailment words of {self.word_2.word}:\n\n"
for i in range(len(inverse_entailment_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in inverse_entailment_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Inverse entailment words of {self.word_2.word}:\n\n":
return "No inverse entailment words were found."
return result_string
def is_entailment(self):
"""Search if the word 2 is entailment of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the entailment of the word 1
entailment_synset_list = []
for synset_id in self.word_1.synset_id_list:
entailment_result = self.make_consult(f"ent({synset_id}, EntailmentID)")
entailment_synset_list.append(entailment_result)
# Get all the words in the synset
entailment_words_list = []
gloss_list = []
for entailment_synset in entailment_synset_list:
for entailment_word in entailment_synset:
entailment_words_list.append(
self.get_all_words(entailment_word["EntailmentID"])
)
gloss_list.append(
self.make_consult(f"g({entailment_word['EntailmentID']}, Gloss)")[
0
]["Gloss"]
)
# Check if the word 2 is in the entailment words list
for i in range(len(entailment_words_list)):
if self.word_2.word in entailment_words_list[i]:
return f"{self.word_2.word} is entailment of {self.word_1.word}.\n\n{gloss_list[i]}:\n\t{self.word_1.word}"
return f"{self.word_2.word} is not entailment of {self.word_1.word}"
# endregion
# region Meronym Holonym
def mer_hol(self, function: str, operator: str):
"""Make the consult to the prolog file with the operator. The operator can be mm, ms or mp."""
if function == "Meronym":
if not self.word_1.exist:
return self.not_found(self.word_1.word)
elif function == "Holonym":
if not self.word_2.exist:
return self.not_found(self.word_2.word)
operation = (
"member"
if operator == "mm"
else "substance"
if operator == "ms"
else "part"
)
# Get all the meronym of the word
consult_synset_list = []
for synset_id in self.word_1.synset_id_list:
consult_result = self.make_consult(f"{operator}({synset_id}, Consult_ID)")
consult_synset_list.append(consult_result)
# Get all the words in the synset
operation_words_list = []
gloss_list = []
for operation_synset in consult_synset_list:
for operation_word in operation_synset:
operation_words_list.append(
self.get_all_words(operation_word["Consult_ID"])
)
gloss_list.append(
self.make_consult(f"g({operation_word['Consult_ID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"{function}-{operation} words of {self.word_1.word}:\n\n"
for i in range(len(operation_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in operation_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"{function}-{operation} words of {self.word_1.word}:\n\n":
return f"No {function}-{operation} words were found."
return result_string
def is_meronym(self):
"""Check if the word 2 is meronym of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Get all the meronym of the word 1
meronym_synset_list = []
for synset_id in self.word_1.synset_id_list:
meronym_result = self.make_consult(f"mm({synset_id}, MeronymID)")
meronym_synset_list.append(meronym_result)
# Get all the words in the synset
meronym_words_list = []
gloss_list = []
for meronym_synset in meronym_synset_list:
for meronym_word in meronym_synset:
meronym_words_list.append(self.get_all_words(meronym_word["MeronymID"]))
gloss_list.append(
self.make_consult(f"g({meronym_word['MeronymID']}, Gloss)")[0][
"Gloss"
]
)
# Check if the word 2 is in the meronym words list
for i in range(len(meronym_words_list)):
if self.word_2.word in meronym_words_list[i]:
return f"{self.word_2.word} is meronym of {self.word_1.word}.\n\n{gloss_list[i]}:\n\t{self.word_1.word}"
return f"{self.word_2.word} is not meronym of {self.word_1.word}"
# endregion
# region Attribute
def attribute_of(self, word_indicator: int = 1):
"""Search all possible meanings of the word 1 and 2"""
self.fill_word_info(word_indicator)
temp_word = self.word_1 if word_indicator == 1 else self.word_2
if not temp_word.exist:
return self.not_found(temp_word.word)
# Get all the attribute of the word
attribute_synset_list = []
for synset_id in temp_word.synset_id_list:
attribute_result = self.make_consult(f"at({synset_id}, AttributeID)")
attribute_synset_list.append(attribute_result)
# Get all the words in the synset
attribute_words_list = []
gloss_list = []
for attribute_synset in attribute_synset_list:
for attribute_word in attribute_synset:
attribute_words_list.append(
self.get_all_words(attribute_word["AttributeID"])
)
gloss_list.append(
self.make_consult(f"g({attribute_word['AttributeID']}, Gloss)")[0][
"Gloss"
]
)
# Create the result string.
result_string = f"Attribute words of {temp_word.word}:\n\n"
for i in range(len(attribute_words_list)):
result_string += f"{gloss_list[i]}:\n "
for word in attribute_words_list[i]:
result_string += f"\t{word}"
result_string += "\n\n"
if result_string == f"Attribute words of {temp_word.word}:\n\n":
return "No attribute words were found."
return result_string
def is_attribute(self):
"""Search if the word 2 is attribute of the word 1"""
if not self.word_1.exist:
return self.not_found(self.word_1.word)
if not self.word_2.exist:
return self.not_found(self.word_2.word)
# Call at predicate with the word 1 and 2 for every synset id.
for synset_id_1 in self.word_1.synset_id_list:
for synset_id_2 in self.word_2.synset_id_list:
at_result = self.make_consult(f"at({synset_id_1}, {synset_id_2})")
if at_result != []:
return f"{self.word_2.word} is attribute of {self.word_1.word}."
return f"{self.word_2.word} is not attribute of {self.word_1.word}"
# endregion
def fill_word_info(self, word_selector: int):
temp_word = self.word_1 if word_selector == 1 else self.word_2
# Get all the word info through the s predicate.
s_result = self.make_consult(
f"s(SynsetID, WordNumber,'{temp_word.word}', WordType, WordSense, TagCount)"
)
# Check if the word exists.
temp_word.exist = s_result != []
# Fill the word info with the results.
for result in s_result:
temp_word.synset_id_list.append(result["SynsetID"])
temp_word.word_number_list.append(result["WordNumber"])
temp_word.word_sense_list.append(result["WordSense"])
temp_word.word_type_list.append(
self.translate_word_type(result["WordType"])
)
temp_word.tag_count_list.append(result["TagCount"])
def get_all_words(self, S_ID: str):
"""Get all the words in a synset"""
words_in_synset = []
search_result = self.make_consult(f"s({S_ID}, _, Word, _, _, _)")
for result in search_result:
words_in_synset.append(result["Word"])
return words_in_synset
def make_consult(self, query: str) -> list:
"""Make a consult to the prolog file."""
return list(self.prolog.query(query))
def translate_word_type(self, word_type: str) -> str:
"""Translate the word type to a more readable format."""
if word_type == "n":
return "noun"
elif word_type == "v":
return "verb"
elif word_type == "a" or word_type == "s":
return "adjective"
elif word_type == "r":
return "adverb"
else:
return "unknown"
def not_found(self, word):
return f"The word {word} was not found in the database."
class WordInfo:
"""Class that contains the information of a word."""
def __init__(self, word: str):
self.word = word
self.synset_id_list = []
self.word_type_list = []
self.word_number_list = []
self.word_sense_list = []
self.gloss_list = []
self.tag_count_list = []
self.exist = False
def __str__(self):