1
- '''
1
+ """
2
2
The forward-chaining algorithm PL-FC-ENTAILS? (KB, q) determines if a single proposition
3
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.
4
+ known facts (positive literals) in the knowledge base.
5
5
known facts (positive literals) in the knowledge base.
6
6
7
7
Reference: https://dl.ebooksworld.ir/books/Artificial.Intelligence.A.Modern.Approach.4th.Edition.Peter.Norvig.%20Stuart.Russell.Pearson.9780134610993.EBooksWorld.ir.pdf
8
8
9
- '''
9
+ """
10
10
11
11
import re
12
12
13
+
13
14
def find_symbols_in_kb (knowledge_base : list [str ]) -> dict :
14
- '''
15
+ """
15
16
Find all unique symbols in the Knowledge_base
16
17
:param knowledge_base: a list of string of definite clauses
17
18
:returns: a dictionary with symbols as the keys their values are False
18
- '''
19
+ """
19
20
20
21
inferred = {}
21
22
22
23
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 ])
24
25
for symbol in symbols :
25
26
if symbol not in inferred :
26
27
inferred [symbol ] = False
@@ -29,31 +30,30 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
29
30
30
31
31
32
def number_of_symbols_in_premise (knowledge_base : list [str ]) -> dict :
32
-
33
- '''
33
+ """
34
34
Count the number of prposiotion symbols in each premise of KB clause
35
35
:param knowledge_base: a list of string of definite clauses
36
36
:returns: a dictionary with key as the premise of KB clause and value of count of symbols in the premise
37
- '''
37
+ """
38
38
39
39
count = {}
40
40
for clause in knowledge_base :
41
- if ( len (clause ) != 1 ) :
41
+ if len (clause ) != 1 :
42
42
index = clause .find ("=>" )
43
43
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 ())
45
45
count [premise ] = len (letters )
46
46
47
47
return count
48
48
49
49
50
50
def get_known_facts (knowledge_base : list [str ]) -> list [str ]:
51
- '''
51
+ """
52
52
Get the known facts in KB
53
53
:param knowledge_base: a list of string of definite clauses
54
54
:returns: list of facts
55
55
56
- '''
56
+ """
57
57
58
58
facts = []
59
59
for clause in knowledge_base :
@@ -63,10 +63,8 @@ def get_known_facts(knowledge_base: list[str]) -> list[str]:
63
63
return facts
64
64
65
65
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
70
68
:param knowledge_base: a list of string of definite clauses
71
69
:param query: a single proposition symbol that you are checking if it is entailed by the KB
72
70
:returns: If the query entailed by the KB or not?
@@ -75,15 +73,16 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
75
73
>>> forward_chaining([ "P => Q", "L & M => P", "B&L=> M", "A&P=>L", "A&B=>L", "A", "B" ], "C")
76
74
False
77
75
78
- '''
76
+ """
79
77
80
78
count = number_of_symbols_in_premise (knowledge_base )
81
79
inferred = find_symbols_in_kb (knowledge_base )
82
80
queue = get_known_facts (knowledge_base )
83
81
84
- while ( len (queue ) > 0 ) :
82
+ while len (queue ) > 0 :
85
83
p = queue .pop ()
86
- if p == query : return True
84
+ if p == query :
85
+ return True
87
86
if not inferred [p ]:
88
87
inferred [p ] = True
89
88
for clause in knowledge_base :
@@ -97,39 +96,26 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
97
96
return False
98
97
99
98
99
+ KB = ["P => Q" , "L & M => P" , "B&L=> M" , "A&P=>L" , "A&B=>L" , "A" , "B" ]
100
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
- '''
101
+ """
112
102
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
114
104
2)- It must be written as an implcaion whose
115
105
its premise(head) must be conjunction of positive literals and its conclusion(body)
116
106
3)- It must contains facts about the world which are written as a single proposition symbol
117
- '''
107
+ """
118
108
QUERY = "Q"
119
109
120
- '''
110
+ """
121
111
Query is a signe proposition symbol that you check if it is entailed by the KB
122
112
123
- '''
113
+ """
124
114
125
115
if __name__ == "__main__" :
126
-
127
-
128
-
129
116
import doctest
130
117
131
118
doctest .testmod (verbose = True )
132
-
133
119
134
120
result = forward_chaining (KB , QUERY )
135
- print (result )
121
+ print (result )
0 commit comments