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
@@ -30,30 +31,29 @@ def find_symbols_in_kb(knowledge_base: list[str]) -> dict:
30
31
31
32
def number_of_symbols_in_premise (knowledge_base : list [str ]) -> dict :
32
33
33
- '''
34
+ """
34
35
Count the number of prposiotion symbols in each premise of KB clause.
35
36
:param knowledge_base: a list of string of definite clauses
36
37
:returns: a dict with keys as the premise and value is count of symbols in premise
37
- '''
38
+ """
38
39
39
40
count = {}
40
41
for clause in knowledge_base :
41
- if ( len (clause ) != 1 ) :
42
+ if len (clause ) != 1 :
42
43
index = clause .find ("=>" )
43
44
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 ())
45
46
count [premise ] = len (letters )
46
47
47
48
return count
48
49
49
50
50
51
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
53
54
:param knowledge_base: a list of string of definite clauses
54
55
:returns: list of facts
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
72
70
:returns: If the query entailed by the KB or not?
@@ -79,15 +77,16 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
79
77
>>> forward_chaining(input_kb, "C")
80
78
False
81
79
82
- '''
80
+ """
83
81
84
82
count = number_of_symbols_in_premise (knowledge_base )
85
83
inferred = find_symbols_in_kb (knowledge_base )
86
84
queue = get_known_facts (knowledge_base )
87
85
88
- while ( len (queue ) > 0 ) :
86
+ while len (queue ) > 0 :
89
87
p = queue .pop ()
90
- if p == query : return True
88
+ if p == query :
89
+ return True
91
90
if not inferred [p ]:
92
91
inferred [p ] = True
93
92
for clause in knowledge_base :
@@ -101,38 +100,25 @@ def forward_chaining(knowledge_base: list[str], query:str) -> bool:
101
100
return False
102
101
103
102
103
+ KB = ["P => Q" , "L & M => P" , "B&L=> M" , "A&P=>L" , "A&B=>L" , "A" , "B" ]
104
104
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
+ """
116
106
1)- KB must be written in horn form.
117
107
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
+ """
121
111
QUERY = "Q"
122
112
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
125
115
126
- '''
116
+ """
127
117
128
118
if __name__ == "__main__" :
129
-
130
-
131
-
132
119
import doctest
133
120
134
121
doctest .testmod (verbose = True )
135
-
136
122
137
123
result = forward_chaining (KB , QUERY )
138
- print (result )
124
+ print (result )
0 commit comments