-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_base.py
More file actions
52 lines (46 loc) · 1.73 KB
/
knowledge_base.py
File metadata and controls
52 lines (46 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
from collections import defaultdict, deque
import re
from forward_chaining import ForwardChaining
from backwards_chaining import BackwardChaining
from truth_table import TruthTable
class KnowledgeBase:
def __init__(self, filename):
self.kb, self.query = self.parse_file(filename)
def parse_file(self, filename):
try:
with open(filename, 'r') as file:
lines = file.read().splitlines()
tell_index = lines.index("TELL")
ask_index = lines.index("ASK")
kb_str = "".join(lines[tell_index + 1:ask_index]).strip()
query = lines[ask_index + 1].strip()
return self.parse_kb(kb_str), query
except (FileNotFoundError, ValueError):
print("Usage: python iengine.py <input_file> <method>")
sys.exit(1)
@staticmethod
def parse_kb(kb_str):
clauses = kb_str.strip().split(";")
kb = []
for clause in clauses:
clause = clause.strip()
if not clause:
continue
if "=>" in clause:
premise, conclusion = clause.split("=>")
premises = tuple(p.strip() for p in premise.split("&"))
kb.append((premises, conclusion.strip()))
else:
kb.append(((), clause.strip()))
return kb
def forward_chaining(self):
fc = ForwardChaining(self.kb, self.query)
return fc.run()
def backward_chaining(self):
bc = BackwardChaining(self.kb, self.query)
return bc.execute() # Change from run() to execute()
def truth_table(self):
tt = TruthTable(self.kb, self.query)
tt.generate_table()
return tt.result