!752 static L poem; static int index; static new Map wordMap; p { poem = parsePoem(first(loadPoems("#1003074")).lines); printStructure(poem); testWith(parsePoem(first(loadPoems("#1003076")).lines)); reset(); makeBot("Chemistry Bot"); } static void testWith(L test) { for (int i = 0; i+1 < l(test); i += 2) { S q = test.get(i).q, a = test.get(i+1).a; S answer = callStaticAnswerMethod(mc(), q); if (!linesEq(a, answer)) fail("Test failed: " + quote(answer) + " vs " + quote(a)); print("OK: " + answer); } print("Test OK!"); } static bool linesEq(S a, S b) { ret eqic(simplifyLine(a), simplifyLine(b)); } static S simplifyLine(S a) { ret join(" ", codeTokens(nlTok2(a))); } static void reset() { index = 0; wordMap.clear(); } answer { 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); } } } static 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()); } } static 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); }