Skip to content

Commit 205fb53

Browse files
committed
fixed pushing issue
1 parent 664653e commit 205fb53

File tree

1 file changed

+84
-30
lines changed

1 file changed

+84
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
import re
1+
'''
2+
The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition
3+
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.
5+
known facts (positive literals) in the knowledge base.
6+
7+
Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf
8+
9+
'''
210

11+
import re
312

413
def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
5-
""" """
14+
'''
15+
Find all unique symbols in the Knowledge_base
16+
:param knowledge_base: a list of string of definite clauses
17+
:returns: a dictionary with symbols as the keys their values are False
18+
'''
19+
620
inferred = {}
721

822
for i in range(len(knowledge_base)):
9-
symbols = re.findall(r"[a-zA-Z]", knowledge_base[i])
23+
symbols = re.findall(r'[a-zA-Z]', knowledge_base[i])
1024
for symbol in symbols:
1125
if symbol not in inferred:
1226
inferred[symbol] = False
@@ -15,21 +29,32 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
1529

1630

1731
def number_of_symbols_in_premise(knowledge_base: list[str]) -> dict:
18-
""" """
32+
33+
'''
34+
Count the number of prposiotion symbols in each premise of KB clause
35+
:param knowledge_base: a list of string of definite clauses
36+
:returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise
37+
'''
1938

2039
count = {}
2140
for clause in knowledge_base:
22-
if len(clause) != 1:
41+
if(len(clause) != 1):
2342
index = clause.find("=>")
2443
premise = clause[:index]
25-
letters = "".join(e for e in premise if e.isalpha())
44+
letters = ''.join(e for e in premise if e.isalpha())
2645
count[premise] = len(letters)
2746

2847
return count
2948

3049

3150
def get_known_facts(knowledge_base: list[str]) -> list[str]:
32-
""" """
51+
'''
52+
Get the known facts in KB
53+
:param knowledge_base: a list of string of definite clauses
54+
:returns: list of facts
55+
56+
'''
57+
3358
facts = []
3459
for clause in knowledge_base:
3560
if len(clause) == 1:
@@ -38,16 +63,28 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
3863
return facts
3964

4065

41-
def forward_chianing(knowledge_base: list[str], query: str) -> bool:
66+
67+
68+
def forward_chaining(knowledge_base: list[str], query:str) -> bool:
69+
''' Forward chaining on Knowledge Base(KB) of definite clauses
70+
:param knowledge_base: a list of string of definite clauses
71+
:param query: a single proposition symbol that you are checking if it is entailed by the KB
72+
:returns: If the query entailed by the KB or not?
73+
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "Q")
74+
True
75+
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C")
76+
False
77+
78+
'''
79+
4280
count = number_of_symbols_in_premise(knowledge_base)
4381
inferred = find_symbols_in_kb(knowledge_base)
4482
queue = get_known_facts(knowledge_base)
4583

46-
while len(queue) > 0:
84+
while(len(queue) > 0):
4785
p = queue.pop()
48-
if p == query:
49-
return True
50-
if inferred[p] == False:
86+
if p == query: return True
87+
if not inferred[p]:
5188
inferred[p] = True
5289
for clause in knowledge_base:
5390
index = clause.find("=>")
@@ -60,22 +97,39 @@ def forward_chianing(knowledge_base: list[str], query: str) -> bool:
6097
return False
6198

6299

100+
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+
'''
112+
1)- KB must be written in horn form.
113+
2)- It must be written as an implcaion whose
114+
2)- It must be written as an implcaion whose
115+
its premise(head) must be conjunction of positive literals and its conclusion(body)
116+
3)- It must contains facts about the world which are written as a single proposition symbol
117+
'''
118+
QUERY = "Q"
119+
120+
'''
121+
Query is a signe proposition symbol that you check if it is entailed by the KB
122+
123+
'''
124+
63125
if __name__ == "__main__":
64-
kb = [
65-
"p => q",
66-
"q => r",
67-
"r => p",
68-
"s => t",
69-
"u => v",
70-
"v => w",
71-
"w => u",
72-
"a",
73-
"b",
74-
"c",
75-
"w",
76-
"u&q=>r",
77-
"q",
78-
]
79-
80-
result = forward_chianing(kb, "p")
81-
print(result)
126+
127+
128+
129+
import doctest
130+
131+
doctest.testmod(verbose=True)
132+
133+
134+
result = forward_chaining(KB, QUERY)
135+
print(result)

0 commit comments

Comments
 (0)