forked from ChalmersGU-AI-course/shrdlite-course-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShrdlite.ts
115 lines (103 loc) · 4.09 KB
/
Shrdlite.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
///<reference path="World.ts"/>
///<reference path="Parser.ts"/>
///<reference path="Interpreter.ts"/>
///<reference path="Planner.ts"/>
module Shrdlite {
export function interactive(world : World) : void {
function endlessLoop(utterance : string = "") : void {
var inputPrompt = "What can I do for you today? ";
var nextInput = () => world.readUserInput(inputPrompt, endlessLoop);
if (utterance.trim()) {
var plan : string[] = splitStringIntoPlan(utterance);
if (!plan) {
plan = parseUtteranceIntoPlan(world, utterance);
}
if (plan) {
world.printDebugInfo("Plan: " + plan.join(", "));
world.performPlan(plan, nextInput);
return;
}
}
nextInput();
}
world.printWorld(endlessLoop);
}
// Generic function that takes an utterance and returns a plan:
// - first it parses the utterance
// - then it interprets the parse(s)
// - then it creates plan(s) for the interpretation(s)
export function parseUtteranceIntoPlan(world : World, utterance : string) : string[] {
world.printDebugInfo('Parsing utterance: "' + utterance + '"');
try {
var parses : Parser.Result[] = Parser.parse(utterance);
} catch(err) {
if (err instanceof Parser.Error) {
world.printError("Parsing error", err.message);
return;
} else {
throw err;
}
}
world.printDebugInfo("Found " + parses.length + " parses");
parses.forEach((res, n) => {
world.printDebugInfo(" (" + n + ") " + Parser.parseToString(res));
});
try {
var interpretations : Interpreter.Result[] = [];
if(world.previousRes.length == 0) {
interpretations = Interpreter.interpret(parses, world.currentState);
} else {
interpretations = [world.previousRes[parseInt(parses[0].prs.letter) - 1]];
world.previousRes = [];
}
} catch(err) {
if (err instanceof Interpreter.Error) {
world.printError("Interpretation error", err.message);
return;
}else if(err instanceof Interpreter.Clarification) {
world.previousRes = err.data;
world.printError(err.message);
return;
} else {
throw err;
}
}
world.printDebugInfo("Found " + interpretations.length + " interpretations");
interpretations.forEach((res, n) => {
world.printDebugInfo(" (" + n + ") " + Interpreter.interpretationToString(res));
});
try {
var plans : Planner.Result[] = Planner.plan(interpretations, world.currentState);
} catch(err) {
if (err instanceof Planner.Error) {
world.printError("Planning error", err.message);
return;
} else if (err instanceof AStar.Error) {
world.printError("Planning error", err.message);
return;
} else {
throw err;
}
}
world.printDebugInfo("Found " + plans.length + " plans");
plans.forEach((res, n) => {
world.printDebugInfo(" (" + n + ") " + Planner.planToString(res));
});
var plan : string[] = plans[0].plan;
world.printDebugInfo("Final plan: " + plan.join(", "));
return plan;
}
// This is a convenience function that recognizes strings
// of the form "p r r d l p r d"
export function splitStringIntoPlan(planstring : string) : string[] {
var plan : string[] = planstring.trim().split(/\s+/);
var actions = {p:"pick", d:"drop", l:"left", r:"right"};
for (var i = plan.length-1; i >= 0; i--) {
if (!actions[plan[i]]) {
return;
}
plan.splice(i, 0, actions[plan[i]]);
}
return plan;
}
}