!7 static Pair text2tag, tag2answer; static SS text2tag_symbols; static new ThreadLocal session; static Map spellMap; static L countries; sbool debug; svoid setSession(S s) { session.set(s); } p { // load stuff Pair text2tag_orig = dropAsterisks(parseText2Tag(#1008363)); text2tag = pairMap(f dropNoWordKeys, text2tag_orig); text2tag_symbols = onlyNoWordKeys(text2tag_orig.a); printStructure(text2tag_symbols); tag2answer = dropAsterisks(parseTag2Answer(#1008366)); spellMap = loadSpellList(#1008389); //printStruct(spellMap); countries = commonCountryNames(); veryBigConsole(); bot(); } sS answer(S s) { if (creator() == null) if "debug" set debug; S _s = s; s = levenAttract(spellMap, s); if (neqic(s, _s)) print("Spell-correct => " + s); for (S country : countries) if (containsIgnoreCase(s, country)) ret shippingAnswer(country); S tag = text2tag(s); if (debug) print("tag: " + tag); if (nempty(tag)) { S answer = oneOf(splitAtVerticalBar(tag2answer(tag))); if (nempty(answer)) ret answer; } if (!match("nice", s)) { S country = cityToCountry(simpleSpaces(s)); if (country != null) ret shippingAnswer(country); } for (S t : javaTokC(s)) { S country = cityToCountry(t); if (country != null && neqic(t, "nice")) ret shippingAnswer(country); } ret "Sorry, I didn't understand that"; //ret oneOf("Uh...", "Sorry - I don't think I understand"); //ret oneOf("Oh really?", "Oh God.", "Aha.", "What?", "Oh COME ON"); } static S text2tag(S s) { if (!containsWords(s)) ret text2tag_symbols.get(levenClosest(text2tag_symbols, s)); // try text2tag table try answer lookupByNLMatch(text2tag.a, s); // try match on category and 2nd part of category for (S tag : keys(tag2answer.a)) { if (match(tag, s)) ret tag; S x = dropUntilComma(tag); if (l(x) != l(s) && match(x, s)) ret tag; } // partial match on asterisk parts try answer lookupByNLFind(text2tag.b, s); // partial match on full lines try answer lookupByNLFind(text2tag.a, s); // partial match on 2nd part of category for (S tag : keys(tag2answer.a)) { S x = dropUntilComma(tag); if (find3(x, s)) ret tag; } null; } static S tag2answer(S s) { ret lookupPossiblyIgnoringCase(tag2answer.a, s); } static Pair dropAsterisks(SS map) { SS a = mapKeysAndValues_litOrderedMap(f tok_dropAsterisks, map); SS b = mapKeysAndValues_litOrderedMap(f asteriskPartsOnly, f tok_dropAsterisks, map); b.remove(""); ret pair(a, b); } sS initialMessage() { ret "Stefan's Bot here. What's up?"; } sS shippingAnswer(S country) { bool yes = eqicOneOf(country, "United States", "Canada", "United Kingdom", "Spain", "Australia", "New Zealand", "Ireland"); ret yes ? "Yes, we ship to " + country : "Sorry, we do not ship to " + country; }