Skip to content

Commit 9d0f326

Browse files
committed
forward chaining algorithm complete
1 parent 98a810e commit 9d0f326

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

propositional_logic_inference_algorithms/forward_chaining

+51-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import re
33

44
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
55
'''
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
69
'''
10+
711
inferred = {}
812

913
for i in range(len(knowledge_base)):
@@ -18,6 +22,9 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
1822
def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
1923

2024
'''
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
2128
'''
2229

2330
count = {}
@@ -32,9 +39,13 @@ def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
3239

3340

3441
def get_known_facts(knowledge_base: list[str]) -> list[str]:
35-
3642
'''
43+
Get the known facts in KB
44+
:param knowledge_base: a list of string of definite clauses
45+
:returns: list of facts
46+
3747
'''
48+
3849
facts = []
3950
for clause in knowledge_base:
4051
if len(clause) == 1:
@@ -45,7 +56,17 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
4556

4657

4758

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+
'''
4970

5071
count = number_of_symbols_in_premise(knowledge_base)
5172
inferred = find_symbols_in_kb(knowledge_base)
@@ -68,9 +89,35 @@ def forward_chianing(knowledge_base: list[str], query:str) -> bool:
6889

6990

7091

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+
71115
if __name__ == "__main__":
116+
72117

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
74119

75-
result = forward_chianing(kb, "t")
120+
doctest.testmod(verbose=True)
121+
122+
result = forward_chaining(KB, QUERY)
76123
print(result)

0 commit comments

Comments
 (0)