!752 m { static new MultiMap map; static S name = "Multi Assoc Bot"; static boolean chatEnabled = true; p { readLocally("map"); makeAndroid3(name + " at " + getComputerID() + "."); if (chatEnabled) toChat(); } static void toChat() { while (true) { pcall { for (ChatLine l : suckHumans(name)) sayInWebChat(name, answer(l.text)); } sleep(1000); } } static L lookup(S s) { ret map.get(simplify(s)); } // should simplify a sentence neatly static S simplify(S s) { // TODO: probably leave case of quoted strings intact S result = join(" ", codeTokensOnly(tokensToLowerCase(nlTok(s)))); print(s); print("=> " + result); ret result; } static synchronized S answer(S input) { L tok = nlTok(input); int idx = tok.indexOf("="); if (idx >= 0) { S l = join(tok.subList(0, idx)).trim(); S r = join(tok.subList(idx+1, l(tok))).trim(); l = simplify(l); L values = map.get(l); int i = indexOfSentence(values, r); // standard case (new value) if (i < 0) { map.put(l, r); saveLocally("map"); ret "ok, added"; } // already latest value if (i == values.size()-1) ret "yup, i know :)"; // reordering case values.add(r); values.remove(i); saveLocally("map"); ret "ok, moving value to end"; } L values = lookup(input); if (values.isEmpty()) ret null; ret input + "=" + join(", ", values); } static int indexOfSentence(L l, S sentence) { sentence = simplify(sentence); for (int i = 0; i < l(l); i++) if (eq(simplify(l.get(i)), sentence)) ret i; ret -1; } }