!747 m { static S sentence1 = "Yes is the opposite of no. No is the opposite of yes."; static S sentence2 = "Green is the opposite of white."; !include #1000988 // MultiSet p { L tok1 = parse(sentence1); L tok2 = parse(sentence2); Map> map = makeMMapPrefix(tok1, tok2); if (map == null) print("No match."); else print(structure(map)); print(complete(sentence1, sentence2)); } static L parse(S s) { return tokensToLowerCase(javaTok(s)); } static S complete(S sentence1, S sentence2) { L tok1 = parse(sentence1); L tok2 = parse(sentence2); Map> map = makeMMapPrefix(tok1, tok2); if (map == null) return null; new L tok; tok.addAll(tok2.subList(0, tok2.size()-1)); for (int i = tok2.size()-1; i < tok1.size(); i++) { S t = tok1.get(i); MultiSet set = map.get(t); tok.add(set == null ? t : set.getMostPopularEntry()); } return join(tok); } static Map> makeMMapPrefix(L tok1, L tok2) { if (tok1.size() < tok2.size()) return null; Map> map = new TreeMap>(); for (int i = 1; i < tok2.size(); i += 2) { S t1 = tok1.get(i), t2 = tok2.get(i); MultiSet set = map.get(t1); if (set == null) map.put(t1, new MultiSet(t2)); else set.add(t2); } // match succeeds return map; } }