-
Notifications
You must be signed in to change notification settings - Fork 3
/
igor-lib.js
93 lines (86 loc) · 2 KB
/
igor-lib.js
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
// Parse webkit speech output for recognized Igor commands
function parse_speech(message)
{
debug("Message = " + message);
command = match_command(message);
if (command)
{
run_command(command, message);
}
}
// Run an Igor command, given what was said to invoke it
function run_command(command, message)
{
debug("Making the call to " + command.handler);
if (is_integration_loaded(command.from))
{
eval(command.handler)(message);
}
else
{
load_integration(command.from, function () {
eval(command.handler)(message);
});
}
}
// Determine whether an utterance matches any Igor commands
function match_command(utterance)
{
for (c in igor_commands)
{
command = igor_commands[c];
if (command.matcher(utterance, command.keywords))
{
debug("Matched " + command.handler + " from " + command.from);
return command;
}
}
}
// Lazy-load javascript from the integrations/ directory for use
function load_integration(path, callback)
{
if (!is_integration_loaded(path))
{
loaded_integrations.push(path);
loadScript(path, callback);
debug("Now have " + loaded_integrations.length + " integrations loaded.");
}
}
// Determines whether an integration has already been loaded
function is_integration_loaded(path)
{
for (i = 0; i < loaded_integrations.length; i++)
{
if (loaded_integrations[i] == path)
{
debug("Tried to load " + path + " but it was already loaded.");
return true;
}
}
return false;
}
// Returns true if all words in needles exist in haystack
function match_all(haystack, needles)
{
for (i in needles)
{
pedantic("Trying to match " + needles[i] + " in " + haystack);
if (haystack.toLowerCase().indexOf(needles[i].toLowerCase()) < 0)
{
return false;
}
}
return true;
}
// Returns true if any word in needles exists in haystack
function match_any(haystack, needles)
{
for (i in needles)
{
if (haystack.indexOf(needles[i]) > -1)
{
return true;
}
}
return false;
}