@@ -3,7 +3,11 @@ import re
3
3
4
4
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
5
5
'''
6
+ Find all unique symbols in the Knowledge_base
7
+ :param knowledge_base: a list of string of definite clauses
8
+ :returns: a dictionary with symbols as the keys their values are False
6
9
'''
10
+
7
11
inferred = {}
8
12
9
13
for i in range(len(knowledge_base)):
@@ -18,6 +22,9 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
18
22
def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
19
23
20
24
'''
25
+ Count the number of prposiotion symbols in each premise of KB clause
26
+ :param knowledge_base: a list of string of definite clauses
27
+ :returns: a dictionary with key as the premise of KB clause and vlaue of count of symbols in the premise
21
28
'''
22
29
23
30
count = {}
@@ -32,9 +39,13 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
32
39
33
40
34
41
def get_known_facts(knowledge_base: list[str]) -> list[str]:
35
-
36
42
'''
43
+ Get the known facts in KB
44
+ :param knowledge_base: a list of string of definite clauses
45
+ :returns: list of facts
46
+
37
47
'''
48
+
38
49
facts = []
39
50
for clause in knowledge_base:
40
51
if len(clause) == 1:
@@ -45,7 +56,17 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
45
56
46
57
47
58
48
- def forward_chianing(knowledge_base: list[str], query:str) -> bool:
59
+ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
60
+ ''' Forward chaining on Knowledge Base(KB) of definite clauses
61
+ :param knowledge_base: a list of string of definite clauses
62
+ :param query: a single proposition sysmbol that you are checking if it is entailed by the KB
63
+ :returns: If the query entailed by the KB or not?
64
+ >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q")
65
+ True
66
+ >>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C")
67
+ False
68
+
69
+ '''
49
70
50
71
count = number_of_symbols_in_premise(knowledge_base)
51
72
inferred = find_symbols_in_kb(knowledge_base)
@@ -68,9 +89,35 @@ def forward_chianing(knowledge_base: list[str], query:str) -> bool:
68
89
69
90
70
91
92
+
93
+ KB = [
94
+ "P => Q",
95
+ "L & M => P",
96
+ "B&L=> M",
97
+ "A&P=>L",
98
+ "A&B=>L",
99
+ "A",
100
+ "B" ]
101
+
102
+ '''
103
+ 1)- KB must be written in horn form.
104
+ 2)- It must be written as an implcaion whoose
105
+ its premise(head) must be conjuction of positive literals and its conclusion(body)
106
+ 3)- It must contains facts about the world which are written as a single proposition symbol
107
+ '''
108
+
109
+ QUERY = "Q"
110
+
111
+ '''
112
+ Query is a signe proposition symbol that you check if it is entailed by the KB
113
+ '''
114
+
71
115
if __name__ == "__main__":
116
+
72
117
73
- kb = ["p => q", "q => r", "r => p", "s => t", "u => v", "v => w", "w => u", "a", "b", "c" , "w" , "u&q=>r", "q"]
118
+ import doctest
74
119
75
- result = forward_chianing(kb, "t")
120
+ doctest.testmod(verbose=True)
121
+
122
+ result = forward_chaining(KB, QUERY)
76
123
print(result)
0 commit comments