-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
73 lines (56 loc) · 1.89 KB
/
main.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
### Lecture 2. Inversion of Control
### 01-3. Math Knowledge Base
import sys
import random
from abc import *
from enum import Enum
from services.command import *
from services.knowledge_base.math_object import *
from services.knowledge_base.knowledge_base import *
from services.knowledge_base.notion import *
from impl.commands import *
from impl.knowledge_base import *
def parse_user_input(user_input):
tokens = user_input.strip().split(" ")
if len(tokens) == 0:
return (None, None, "")
norm_cmd = tokens[0].lower()
if norm_cmd in supported_commands:
cmd = supported_commands[norm_cmd]
return (cmd, tokens[1:10], "")
return (None, None, "Command not supported: ")
def eval_command(cmd : IUserCommand, args, st, base : IKnowledgeBase):
if cmd.get_args_count() != len(args):
return (False, st, "Invalid number of args. Expected: "
+ str(cmd.get_args_count()) + ", " + "got: " + str(len(args)))
return cmd.evaluate(st, args, base)
def make_commands_dict(cmd_lst):
cmd_dict = dict()
for cmd in cmd_lst:
cmd_dict[cmd.get_command_tag().lower().strip()] = cmd
return cmd_dict
supported_commands = make_commands_dict(
[ Finish()
, Describe()
, DescribeShort()
])
if __name__ == "__main__":
print("Math Research Assistant")
base = KnowledgeBase(make_objects_dict())
finished = False
state = ""
message = ""
args = []
cmd = None
try:
while not(finished):
user_input = input("$> ")
cmd, args, message = parse_user_input(user_input)
if cmd == None:
print(message)
continue
(finished, state, message) = eval_command(cmd, args, state, base)
print("New state: " + str(state))
if message != "": print(message)
except:
print("Unexpected error:", sys.exc_info()[0])