Skip to content

Commit 715e3e3

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 205fb53 commit 715e3e3

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
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
@@ -29,31 +30,30 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
2930

3031

3132
def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
32-
33-
'''
33+
"""
3434
Count the number of prposiotion symbols in each premise of KB clause
3535
:param knowledge_base: a list of string of definite clauses
3636
:returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise
37-
'''
37+
"""
3838

3939
count = {}
4040
for clause in knowledge_base:
41-
if(len(clause) != 1):
41+
if len(clause) != 1:
4242
index = clause.find("=>")
4343
premise = clause[:index]
44-
letters = ''.join(e for e in premise if e.isalpha())
44+
letters = "".join(e for e in premise if e.isalpha())
4545
count[premise] = len(letters)
4646

4747
return count
4848

4949

5050
def get_known_facts(knowledge_base: list[str]) -> list[str]:
51-
'''
51+
"""
5252
Get the known facts in KB
5353
:param knowledge_base: a list of string of definite clauses
5454
:returns: list of facts
5555
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 that you are checking if it is entailed by the KB
7270
:returns: If the query entailed by the KB or not?
@@ -75,15 +73,16 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
7573
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C")
7674
False
7775
78-
'''
76+
"""
7977

8078
count = number_of_symbols_in_premise(knowledge_base)
8179
inferred = find_symbols_in_kb(knowledge_base)
8280
queue = get_known_facts(knowledge_base)
8381

84-
while(len(queue) > 0):
82+
while len(queue) > 0:
8583
p = queue.pop()
86-
if p == query: return True
84+
if p == query:
85+
return True
8786
if not inferred[p]:
8887
inferred[p] = True
8988
for clause in knowledge_base:
@@ -97,39 +96,26 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
9796
return False
9897

9998

99+
KB = ["P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B"]
100100

101-
102-
KB = [
103-
"P => Q",
104-
"L & M => P",
105-
"B&L=> M",
106-
"A&P=>L",
107-
"A&B=>L",
108-
"A",
109-
"B" ]
110-
111-
'''
101+
"""
112102
1)- KB must be written in horn form.
113-
2)- It must be written as an implcaion whose
103+
2)- It must be written as an implcaion whose
114104
2)- It must be written as an implcaion whose
115105
its premise(head) must be conjunction of positive literals and its conclusion(body)
116106
3)- It must contains facts about the world which are written as a single proposition symbol
117-
'''
107+
"""
118108
QUERY = "Q"
119109

120-
'''
110+
"""
121111
Query is a signe proposition symbol that you check if it is entailed by the KB
122112
123-
'''
113+
"""
124114

125115
if __name__ == "__main__":
126-
127-
128-
129116
import doctest
130117

131118
doctest.testmod(verbose=True)
132-
133119

134120
result = forward_chaining(KB, QUERY)
135-
print(result)
121+
print(result)

0 commit comments

Comments
 (0)