Skip to content

Commit 230d679

Browse files
committed
introduced utterance fingerprints
failed so far to set them added also depth marker
1 parent b54e2b6 commit 230d679

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

src/ai/susi/mind/SusiInference.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public JSONObject getDefinition() {
126126
private final static SusiProcedures memoryProcedures = new SusiProcedures();
127127
private final static SusiProcedures javascriptProcedures = new SusiProcedures();
128128
private final static SusiProcedures prologProcedures = new SusiProcedures();
129+
private final static Pattern CATCHALL_GROUP = Pattern.compile("(.*)");
129130
static {
130131
flowProcedures.put(Pattern.compile("SQUASH"), (flow, matcher) -> {
131132
// perform a full mindmeld
@@ -182,15 +183,15 @@ public JSONObject getDefinition() {
182183
});
183184
memoryProcedures.put(Pattern.compile("SET\\h+?([^=]*?)\\h+?=\\h+?([^=]*)\\h*?"), (flow, matcher) -> {
184185
String remember = flow.unify(matcher.group(1)), matching = flow.unify(matcher.group(2));
185-
return see(flow, "%1% AS " + remember, matching, Pattern.compile("(.*)"));
186+
return see(flow, "%1% AS " + remember, matching, CATCHALL_GROUP);
186187
});
187188
memoryProcedures.put(Pattern.compile("SET\\h+?([^=]*?)\\h+?=\\h+?([^=]*?)\\h+?MATCHING\\h+?(.*)\\h*?"), (flow, matcher) -> {
188189
String remember = flow.unify(matcher.group(1)), matching = flow.unify(matcher.group(2)), pattern = flow.unify(matcher.group(3));
189190
return see(flow, remember, matching, Pattern.compile(pattern));
190191
});
191192
memoryProcedures.put(Pattern.compile("CLEAR\\h+?(.*)\\h*?"), (flow, matcher) -> {
192193
String clear = matcher.group(1);
193-
return see(flow, "%1% AS " + flow.unify(clear), "", Pattern.compile("(.*)"));
194+
return see(flow, "%1% AS " + flow.unify(clear), "", CATCHALL_GROUP);
194195
});
195196
memoryProcedures.put(Pattern.compile("IF\\h+?([^=]*)\\h*?"), (flow, matcher) -> {
196197
String expect = flow.unify(matcher.group(1));
@@ -204,7 +205,7 @@ public JSONObject getDefinition() {
204205
return t;
205206
});
206207
memoryProcedures.put(Pattern.compile("NOT\\h*"), (flow, matcher) -> {
207-
//SusiThought t = see(flow, "%1% AS EXPECTED", "", Pattern.compile("(.*)"));
208+
//SusiThought t = see(flow, "%1% AS EXPECTED", "", CATCHALL_GROUP);
208209
// This is a NOT of nothing. This represents therefore a success. Thus we return a non-empty thought.
209210
return new SusiThought().addObservation("REJECTED", "");
210211
});

src/ai/susi/mind/SusiSkill.java

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ public class SusiSkill {
5353
private final static Pattern pathseparator = Pattern.compile("/");
5454
private final static Pattern commaseparator = Pattern.compile(",");
5555
private final static Pattern pipeseparator = Pattern.compile("\\|");
56-
56+
57+
private final static int INDENT_STEP = 4; // like in python
58+
5759
private String skillName, description, author, authorURL, authorEmail, image;
5860
private String termsOfUse, kickoff, developerPrivacyPolicy;
5961
private String[] on;
6062
private Boolean protectedSkill, dynamicContent;
6163
private Set<String> examples, tags;
6264
private List<SusiIntent> skillIntents = new ArrayList<>();
6365
private SusiSkill.ID id;
64-
66+
6567
public static class ID implements Comparable<ID> {
6668
private String skillpath;
6769
private final String[] possible_path_prefixes = new String[] {
@@ -207,7 +209,6 @@ public SusiSkill(
207209
this.id = skillid;
208210
// read the text file and turn it into a intent json; then learn that
209211
boolean prior = false, dynamicContent = false, protectedSkill = false;
210-
int indentStep = 4; // like in python
211212
intentloop: for (IntentBlock block: skillFile) {
212213

213214
// we have intents without any model; these are 'single lines' and may contain configuration settings
@@ -283,8 +284,9 @@ public SusiSkill(
283284

284285
// parse model
285286
String bang_type = "", bang_answers = "";
286-
List<SusiIntent> fileIntents = new ArrayList<>();
287-
fileIntents.add(new SusiIntent(utterances, prior, 0, skillid));
287+
List<SusiIntent> blockIntents = new ArrayList<>();
288+
SusiIntent initialintent = new SusiIntent(utterances, prior, 0, skillid);
289+
blockIntents.add(initialintent);
288290
StringBuilder bang_bag = new StringBuilder();
289291
int blockNr = 0;
290292
readloop: while (blockNr < block.model.size()) {
@@ -297,7 +299,11 @@ public SusiSkill(
297299
for (int i = 0; i < line.length(); i++) {
298300
if (line.charAt(i) == ' ') sc++; else break;
299301
}
300-
int indentLevel = sc % indentStep;
302+
int indentLevel = sc % INDENT_STEP;
303+
blockIntents.forEach(intent -> {
304+
intent.setDepth(indentLevel);
305+
//intent.addInference(new SusiInference("SET _node=" + block.getUtteranceFingerprint(), SusiInference.Type.memory, 0));
306+
});
301307
line = line.trim();
302308

303309
// parse bang types and answer lines
@@ -310,19 +316,19 @@ public SusiSkill(
310316
if (paramspace >= 0) head = head.substring(0, paramspace).trim();
311317
// test bang type
312318
if (head.equals("example")) {
313-
fileIntents.forEach(intent -> intent.setExample(tail));
319+
blockIntents.forEach(intent -> intent.setExample(tail));
314320
} else if (head.equals("expect")) {
315-
fileIntents.forEach(intent -> intent.setExpect(tail));
321+
blockIntents.forEach(intent -> intent.setExpect(tail));
316322
} else if (head.equals("label")) {
317-
fileIntents.forEach(intent -> intent.setLabel(tail));
323+
blockIntents.forEach(intent -> intent.setLabel(tail));
318324
} else if (head.equals("implication")) {
319-
fileIntents.forEach(intent -> intent.setImplication(tail));
325+
blockIntents.forEach(intent -> intent.setImplication(tail));
320326
} else if (head.equals("first")) {
321-
fileIntents.forEach(intent -> intent.addInference(new SusiInference("FIRST", SusiInference.Type.flow, lineNumber)));
327+
blockIntents.forEach(intent -> intent.addInference(new SusiInference("FIRST", SusiInference.Type.flow, lineNumber)));
322328
} else if (head.equals("rest")) {
323-
fileIntents.forEach(intent -> intent.addInference(new SusiInference("REST", SusiInference.Type.flow, lineNumber)));
329+
blockIntents.forEach(intent -> intent.addInference(new SusiInference("REST", SusiInference.Type.flow, lineNumber)));
324330
} else if (head.equals("plan")) {
325-
fileIntents.forEach(intent -> intent.addInference(new SusiInference("PLAN " + param + ":" + tail, SusiInference.Type.flow, lineNumber)));
331+
blockIntents.forEach(intent -> intent.addInference(new SusiInference("PLAN " + param + ":" + tail, SusiInference.Type.flow, lineNumber)));
326332
} else {
327333
// start multi-line bang
328334
bang_type = head;
@@ -350,7 +356,7 @@ public SusiSkill(
350356
// answers; must contain $!$
351357
SusiAction action = new SusiAction(SusiAction.answerAction(lineNumber, this.id.language(), pipeseparator.split(bang_answers)));
352358

353-
fileIntents.forEach(intent -> {
359+
blockIntents.forEach(intent -> {
354360
intent.addInference(inference);
355361
intent.addAction(action);
356362
extendParentWithAnswer(this.skillIntents, intent);
@@ -369,15 +375,15 @@ public SusiSkill(
369375
} catch (JSONException e) {
370376
throw new JSONException(e.getMessage() + " \"" + bang_bag.toString() + "\"");
371377
}
372-
fileIntents.forEach(intent -> intent.addInference(inference));
378+
blockIntents.forEach(intent -> intent.addInference(inference));
373379

374380
// actions; we may have several actions here
375381

376382
// verify actions
377383
if (definition.has("actions")) {
378384
JSONArray bo_actions = definition.getJSONArray("actions");
379385
bo_actions.forEach(action -> {
380-
fileIntents.forEach(intent -> {
386+
blockIntents.forEach(intent -> {
381387
try {
382388
intent.addAction(new SusiAction((JSONObject) action));
383389
} catch (SusiActionException e) {
@@ -396,11 +402,11 @@ public SusiSkill(
396402
// answers; must contain names from the console result array
397403
if (bang_answers.length() > 0) try {
398404
SusiAction action = new SusiAction(SusiAction.answerAction(lineNumber, this.id.language(), pipeseparator.split(bang_answers)));
399-
fileIntents.forEach(intent -> intent.addAction(action));
405+
blockIntents.forEach(intent -> intent.addAction(action));
400406
} catch (SusiActionException e) {
401407
DAO.severe(e.getMessage());
402408
}
403-
fileIntents.forEach(intent -> {
409+
blockIntents.forEach(intent -> {
404410
extendParentWithAnswer(this.skillIntents, intent);
405411
this.skillIntents.add(intent);
406412
});
@@ -423,33 +429,33 @@ public SusiSkill(
423429
String ifsubstring = line.substring(thenpos + 1).trim();
424430
if (ifsubstring.length() > 0) {
425431
String[] answers = pipeseparator.split(ifsubstring);
426-
fileIntents.forEach(intent -> {
432+
blockIntents.forEach(intent -> {
427433
try {
428434
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
429435
} catch (SusiActionException e) {
430436
e.printStackTrace();
431437
}
432438
});
433-
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
439+
blockIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
434440
continue readloop;
435441
}
436442
} else {
437443
// here we must clone all current intents
438444
final List<SusiIntent> clonedIntents = new ArrayList<>();
439-
fileIntents.forEach(intent -> clonedIntents.add((SusiIntent) intent.clone()));
445+
blockIntents.forEach(intent -> clonedIntents.add((SusiIntent) intent.clone()));
440446

441447
// "? if : then : else"
442448
String ifsubstring = line.substring(thenpos + 1, elsepos).trim();
443449
if (ifsubstring.length() > 0) {
444450
String[] answers = pipeseparator.split(ifsubstring);
445-
fileIntents.forEach(intent -> {
451+
blockIntents.forEach(intent -> {
446452
try {
447453
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
448454
} catch (SusiActionException e) {
449455
e.printStackTrace();
450456
}
451457
});
452-
fileIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
458+
blockIntents.forEach(intent -> intent.addInference(new SusiInference("IF " + condition, SusiInference.Type.memory, lineNumber)));
453459
}
454460
String elsesubstring = line.substring(elsepos + 1).trim();
455461
if (elsesubstring.length() > 0) {
@@ -463,13 +469,13 @@ public SusiSkill(
463469
});
464470
clonedIntents.forEach(intent -> intent.addInference(new SusiInference("NOT " + condition, SusiInference.Type.memory, lineNumber)));
465471
// attach the clones intents to the list of intents
466-
clonedIntents.forEach(intent -> fileIntents.add(intent));
472+
clonedIntents.forEach(intent -> blockIntents.add(intent));
467473
}
468474
continue readloop;
469475
}
470476
} else {
471477
String[] answers = pipeseparator.split(line);
472-
fileIntents.forEach(intent -> {
478+
blockIntents.forEach(intent -> {
473479
try {
474480
intent.addAction(new SusiAction(SusiAction.answerAction(lineNumber, skillid.language(), answers)));
475481
} catch (SusiActionException e) {
@@ -479,7 +485,7 @@ public SusiSkill(
479485
continue readloop;
480486
}
481487
}
482-
fileIntents.forEach(intent -> {
488+
blockIntents.forEach(intent -> {
483489
extendParentWithAnswer(this.skillIntents, intent);
484490
this.skillIntents.add(intent);
485491
});
@@ -529,7 +535,7 @@ private static void extendParentWithAnswer(List<SusiIntent> preceding_intents, S
529535
int parent_id = parent_intent.hashCode();
530536

531537
// - set an invisible assignment in parent for linking variable to ID
532-
parent_intent.addInferences(new SusiInference("SET ", SusiInference.Type.memory, 0));
538+
parent_intent.addInferences(new SusiInference("SET ", SusiInference.Type.memory, 0)); // TODO: this SET is incomplete!
533539

534540
// - add a check rule in child so a child fires only if linking ID is set correctly
535541

src/ai/susi/mind/SusiSkillFile.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ public IntentBlock(final List<Line> numberedLines) {
115115
this.utterance = impl;
116116
}
117117

118+
public String getUtteranceFingerprint() {
119+
String l = this.utterance.toLowerCase();
120+
StringBuilder s = new StringBuilder();
121+
for (int i = 0; i < l.length(); i++) {
122+
char c = l.charAt(i);
123+
if (c >= 'a' && c <= 'z') s.append(c);
124+
if (s.length() > 21) break;
125+
}
126+
while (s.length() <= 21) s.append('x');
127+
StringBuilder h = new StringBuilder(8);
128+
h.append(s.charAt(0)); h.append(s.charAt(1)); h.append(s.charAt(2)); h.append(s.charAt(3));
129+
h.append(s.charAt(5)); h.append(s.charAt(8)); h.append(s.charAt(13)); h.append(s.charAt(21));
130+
return h.toString();
131+
}
132+
118133
public String toString() {
119134
StringBuilder sb = new StringBuilder();
120135
sb.append(utterance).append("\n");

0 commit comments

Comments
 (0)