sclass PoemBot { L poem; int index; new Map wordMap; *() {} *(L *poem) {} bool linesEq(S a, S b) { ret eqic(simplifyLine(a), simplifyLine(b)); } S simplifyLine(S a) { ret join(" ", codeTokens(nlTok2(a))); } void reset() { index = 0; wordMap.clear(); } S answer(S s) { exceptionToUser { if (index >= l(poem)) ret "Sorry, out of script. ^^"; learnLine(assertNotNull(poem.get(index).q), s); ++index; if (index >= l(poem)) ret "Sorry, out of script. ^^"; S a = poem.get(index).a; if (nempty(a)) { ++index; ret translateLine(a); } } ret ""; } void learnLine(S in, S out) { L t1 = nlTok2(dropPunctuation2(in)); L t2 = nlTok2(dropPunctuation2(out)); if (l(t1) != l(t2)) fail("Please say something like this: " + in); for (int i = 1; i < l(t1); i += 2) { S w1 = t1.get(i), w2 = t2.get(i); if (!eqic(w1, w2)) // just overwrite - be flexible! wordMap.put(w1.toLowerCase(), w2.toLowerCase()); } } S translateLine(S s) { L tok = nlTok2(s); for (int i = 1; i < l(tok); i += 2) { S w = wordMap.get(tok.get(i).toLowerCase()); if (nempty(w)) tok.set(i, w); } ret join(tok); } }