!747 !multi-line strings m { !include #1001509 // rules & statements static Map> synMap = new TreeMap>(); p { statements.add("one is a synonym of 1."); statements.add("\"eins\" is a synonym of 1."); statements.add("two is a synonym of 2."); makeSynMap(); ask([[what are the synonyms of 1?]]); ask([[what are the synonyms of 2?]]); } static void makeSynMap() { for (Map m : matchStatements("$1 is a synonym of $2")) addSynonym(unquote(m.get("$1")), unquote(m.get("$2"))); } static void addSynonym(S a, S b) { L la = synMap.get(a), lb = synMap.get(b); if (la == null && lb == null) { L l = litlist(a, b); synMap.put(a, l); synMap.put(b, l); } else if (la == lb) { // nothing to do, they're already synonyms } else if (lb == null) { la.add(b); synMap.put(b, la); } else if (la == null) { lb.add(a); synMap.put(a, lb); } else mergeLists(la, lb); } static void mergeLists(L la, L lb) { for (S b : lb) { la.add(b); synMap.put(b, la); } } static void ask(S q) { print("? " + q); new Map map; if (match4("what are the synonyms of $1?", q, map)) print(" => " + structure(getSynonyms(map.get("$1")))); else print("dunno."); } static L getSynonyms(S x) { ret synMap.get(x); } }