Skip to content

Commit d947265

Browse files
committed
Added a reference and fixed some typos
1 parent 08e84bd commit d947265

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

propositional_logic_inference_algorithms/forward_chaining

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
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
28

9+
'''
310

411
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
512
'''
@@ -24,7 +31,7 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
2431
'''
2532
Count the number of prposiotion symbols in each premise of KB clause
2633
: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
2835
'''
2936

3037
count = {}
@@ -59,7 +66,7 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
5966
def forward_chaining(knowledge_base: list[str], query:str) -> bool:
6067
''' Forward chaining on Knowledge Base(KB) of definite clauses
6168
: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
6370
:returns: If the query entailed by the KB or not?
6471
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q")
6572
True
@@ -90,19 +97,19 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
9097

9198

9299

93-
KB = [
94-
"P => Q",
95-
"L & M => P",
100+
KB = [
101+
"P => Q",
102+
"L & M => P",
96103
"B&L=> M",
97-
"A&P=>L",
98-
"A&B=>L",
99-
"A",
104+
"A&P=>L",
105+
"A&B=>L",
106+
"A",
100107
"B" ]
101108

102109
'''
103110
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)
106113
3)- It must contains facts about the world which are written as a single proposition symbol
107114
'''
108115

@@ -114,10 +121,10 @@ Query is a signe proposition symbol that you check if it is entailed by the KB
114121

115122
if __name__ == "__main__":
116123

117-
124+
118125
import doctest
119126

120127
doctest.testmod(verbose=True)
121-
128+
122129
result = forward_chaining(KB, QUERY)
123130
print(result)

0 commit comments

Comments
 (0)