-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkrolog_parser.py
More file actions
43 lines (33 loc) · 1.14 KB
/
krolog_parser.py
File metadata and controls
43 lines (33 loc) · 1.14 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
from krolog_tools import Fact, Rule
def parse_fact(text: str) -> Fact:
text = text.lstrip(".")
if ("(" in text):
args = text[text.index("(")+1:-1].split(",")
name = text[:text.index("(")]
else:
args = []
name = text
args = list(map(lambda x: x.strip(), args))
return(Fact(name, args))
def parse_rule(text: str) -> Rule:
text = text.lstrip("!")
parts = text.split("<=")
truth = parse_fact(parts[0].strip())
args = parts[1].split("&")
when = [parse_fact(x.strip()) for x in args]
return Rule(truth, when)
def parse(file: str) -> tuple[list[Fact], list[Rule], list[Fact]]:
facts: list[Fact] = []
rules: list[Rule] = []
questions: list[Fact] = []
with open(file, "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if (line.startswith(".")): # Fact
facts.append(parse_fact(line))
if (line.startswith("!")): # Rule
rules.append(parse_rule(line))
if (line.startswith("?")): # Question
questions.append(parse_fact(line.lstrip("?")))
return facts, rules, questions