1
1
import re
2
+ '''
3
+ The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition
4
+ symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from
5
+ known facts (positive literals) in the knowledge base.
6
+
7
+ Refrence: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf
2
8
9
+ '''
3
10
4
11
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
5
12
'''
@@ -24,7 +31,7 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
24
31
'''
25
32
Count the number of prposiotion symbols in each premise of KB clause
26
33
: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
34
+ :returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise
28
35
'''
29
36
30
37
count = {}
@@ -59,7 +66,7 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
59
66
def forward_chaining(knowledge_base: list[str], query:str) -> bool:
60
67
''' Forward chaining on Knowledge Base(KB) of definite clauses
61
68
: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
69
+ :param query: a single proposition symbol that you are checking if it is entailed by the KB
63
70
:returns: If the query entailed by the KB or not?
64
71
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q")
65
72
True
@@ -90,19 +97,19 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
90
97
91
98
92
99
93
- KB = [
94
- "P => Q",
95
- "L & M => P",
100
+ KB = [
101
+ "P => Q",
102
+ "L & M => P",
96
103
"B&L=> M",
97
- "A&P=>L",
98
- "A&B=>L",
99
- "A",
104
+ "A&P=>L",
105
+ "A&B=>L",
106
+ "A",
100
107
"B" ]
101
108
102
109
'''
103
110
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)
111
+ 2)- It must be written as an implcaion whose
112
+ its premise(head) must be conjunction of positive literals and its conclusion(body)
106
113
3)- It must contains facts about the world which are written as a single proposition symbol
107
114
'''
108
115
@@ -114,10 +121,10 @@ Query is a signe proposition symbol that you check if it is entailed by the KB
114
121
115
122
if __name__ == "__main__":
116
123
117
-
124
+
118
125
import doctest
119
126
120
127
doctest.testmod(verbose=True)
121
-
128
+
122
129
result = forward_chaining(KB, QUERY)
123
130
print(result)
0 commit comments