!747 !multi-line strings m { static new L rules; static new L statements; p { rules.add([["$1 is the singular of $2." <=> "$2 is the plural of $1."]]); statements.add("house is the singular of houses."); process(); print(fromLines(statements)); ask([[how many plurals does "house" have?]]); ask([[what are the plurals of "house"?]]); } static void ask(S q) { print("? " + q); new Map map; if (match4("how many plurals does $1 have?", q, map)) print(getPlurals(map.get("$1")).size()); else if (match4("what are the plurals of $1?", q, map)) print(structure(getPlurals(map.get("$1")))); else print("dunno."); } static L getPlurals(S x) { S c = "$2 is the plural of " + x; print("[" + c + "]"); MultiMap matches = matchAll(c); ret matches.get("$2"); } // result of matchStatements ordered by variable static MultiMap matchAll(S pat) { new MultiMap map; L> l = matchStatements(pat); print("[" + l.size() + " matches for " + quote(pat) + "]"); for (Map m : l) { for (Map.Entry e : m.entrySet()) map.put(e.getKey(), e.getValue()); } ret map; } static void process() { for (S rule : rules) { new Matches m; if (match3("* <=> *", rule, m)) { S l = m.unq(0), r = m.unq(1); for (Map map : matchStatements(l)) addStatement(convert(r, map)); } } } static L> matchStatements(S pat) { L> l = new ArrayList>(); for (S s : statements) { Map m = match4(pat, s); if (m != null) l.add(m); } ret l; } static Map match4(S pat, S s) { new Map m; ret match4(pat, s, m) ? m : null; } static boolean match4(S pat, S s, Map m) { L tok1 = parse3(pat), tok2 = parse3(s); if (tok1.size() != tok2.size()) ret false; m.clear(); for (int i = 1; i < tok1.size(); i += 2) { S t1 = tok1.get(i), t2 = tok2.get(i); if (isVar(t1)) m.put(t1, t2); else if (!tokEq(t1, t2)) ret false; } ret true; } static boolean tokEq(S a, S b) { a = unquote(a); b = unquote(b); ret a.equalsIgnoreCase(b); } static boolean isVar(S s) { ret s.startsWith("$") && s.length() > 1; } static S convert(S r, Map matches) { L tok = javaTokPlusPeriod(r); for (int i = 1; i < tok.size(); i += 2) { if (tok.get(i).startsWith("$")) { S m = matches.get(tok.get(i)); if (m != null) tok.set(i, m); } } ret join(tok); } static void addStatement(S s) { for (S st : statements) if (match3(st, s)) ret; statements.add(s); } !include #2000515 // unquote !include #1001497 // parseBoolean !include #1000709 // formatSnippetID !include #1001296 // MultiMap }