Skip to content

Commit 6f6113d

Browse files
committed
Merge branch 'forward_chaining_implementation' of https://github.com/KarimAly12/Python into forward_chaining_implementation
2 parents 18dfd30 + 715e3e3 commit 6f6113d

File tree

1 file changed

+29
-43
lines changed

1 file changed

+29
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
'''
1+
"""
22
The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition
33
symbol q—the query—is entailed by a knowledge base of definite clauses. It begins from
4-
known facts (positive literals) in the knowledge base.
4+
known facts (positive literals) in the knowledge base.
55
known facts (positive literals) in the knowledge base.
66
77
Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf
88
9-
'''
9+
"""
1010

1111
import re
1212

13+
1314
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
14-
'''
15+
"""
1516
Find all unique symbols in the Knowledge_base.
1617
:param knowledge_base: a list of string of definite clauses
1718
:returns: a dictionary with symbols as the keys their values are False
18-
'''
19+
"""
1920

2021
inferred = {}
2122

2223
for i in range(len(knowledge_base)):
23-
symbols = re.findall(r'[a-zA-Z]', knowledge_base[i])
24+
symbols = re.findall(r"[a-zA-Z]", knowledge_base[i])
2425
for symbol in symbols:
2526
if symbol not in inferred:
2627
inferred[symbol] = False
@@ -30,30 +31,29 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
3031

3132
def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
3233

33-
'''
34+
"""
3435
Count the number of prposiotion symbols in each premise of KB clause.
3536
:param knowledge_base: a list of string of definite clauses
3637
:returns: a dict with keys as the premise and value is count of symbols in premise
37-
'''
38+
"""
3839

3940
count = {}
4041
for clause in knowledge_base:
41-
if(len(clause) != 1):
42+
if len(clause) != 1:
4243
index = clause.find("=>")
4344
premise = clause[:index]
44-
letters = ''.join(e for e in premise if e.isalpha())
45+
letters = "".join(e for e in premise if e.isalpha())
4546
count[premise] = len(letters)
4647

4748
return count
4849

4950

5051
def get_known_facts(knowledge_base: list[str]) -> list[str]:
51-
'''
52-
Get the known facts in KB.
52+
"""
53+
Get the known facts in KB
5354
:param knowledge_base: a list of string of definite clauses
5455
:returns: list of facts
55-
56-
'''
56+
"""
5757

5858
facts = []
5959
for clause in knowledge_base:
@@ -63,10 +63,8 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
6363
return facts
6464

6565

66-
67-
68-
def forward_chaining(knowledge_base: list[str], query:str) -> bool:
69-
''' Forward chaining on Knowledge Base(KB) of definite clauses.
66+
def forward_chaining(knowledge_base: list[str], query: str) -> bool:
67+
"""Forward chaining on Knowledge Base(KB) of definite clauses
7068
:param knowledge_base: a list of string of definite clauses
7169
:param query: a single proposition symbol
7270
:returns: If the query entailed by the KB or not?
@@ -79,15 +77,16 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
7977
>>> forward_chaining(input_kb, "C")
8078
False
8179
82-
'''
80+
"""
8381

8482
count = number_of_symbols_in_premise(knowledge_base)
8583
inferred = find_symbols_in_kb(knowledge_base)
8684
queue = get_known_facts(knowledge_base)
8785

88-
while(len(queue) > 0):
86+
while len(queue) > 0:
8987
p = queue.pop()
90-
if p == query: return True
88+
if p == query:
89+
return True
9190
if not inferred[p]:
9291
inferred[p] = True
9392
for clause in knowledge_base:
@@ -101,38 +100,25 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
101100
return False
102101

103102

103+
KB = ["P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B"]
104104

105-
106-
KB = [
107-
"P => Q",
108-
"L & M => P",
109-
"B&L=> M",
110-
"A&P=>L",
111-
"A&B=>L",
112-
"A",
113-
"B" ]
114-
115-
'''
105+
"""
116106
1)- KB must be written in horn form.
117107
2)- It must be written as an implcaion whose
118-
its premise(head) must be conjunction of positive literals and its conclusion(body).
119-
3)- Contains facts about the world as single proposition symbol.
120-
'''
108+
its premise(head) must be conjunction of positive literals and its conclusion(body)
109+
3)- It must contains facts about the world written as a single proposition symbol
110+
"""
121111
QUERY = "Q"
122112

123-
'''
124-
Query is a signe proposition symbol that you check if it is entailed by the KB.
113+
"""
114+
Query is a signe proposition symbol that you check if it is entailed by the KB
125115
126-
'''
116+
"""
127117

128118
if __name__ == "__main__":
129-
130-
131-
132119
import doctest
133120

134121
doctest.testmod(verbose=True)
135-
136122

137123
result = forward_chaining(KB, QUERY)
138-
print(result)
124+
print(result)

0 commit comments

Comments
 (0)