/** * Assembly a reply from a decomp rule and the input. If the reassembly rule is goto, return null * and give the gotoKey to use. Otherwise return the response. */ String assemble(Decomp d, String reply[], Key gotoKey) { String lines[] = new String[3]; d.stepRule(); String rule = d.nextRule(); if (EString.match(rule, "goto *", lines)) { // goto rule -- set gotoKey and return false. gotoKey.copy(keys.getKey(lines[0])); if (gotoKey.key() != null) return null; System.out.println("Goto rule did not match key: " + lines[0]); return null; } String work = ""; while (EString.match(rule, "* (#)*", lines)) { // reassembly rule with number substitution rule = lines[2]; // there might be more int n = 0; try { n = Integer.parseInt(lines[1]) - 1; } catch (NumberFormatException e) { System.out.println("Number is wrong in reassembly rule " + lines[1]); } if (n < 0 || n >= reply.length) { System.out.println("Substitution number is bad " + lines[1]); return null; } reply[n] = post.translate(reply[n]); work += lines[0] + " " + reply[n]; } work += rule; if (d.mem()) { mem.save(work); return null; } return work; }
/** * Decompose a string according to the given key. Try each decomposition rule in order. If it * matches, assemble a reply and return it. If assembly fails, try another decomposition rule. If * assembly is a goto rule, return null and give the key. If assembly succeeds, return the reply; */ String decompose(Key key, String s, Key gotoKey) { String reply[] = new String[10]; for (int i = 0; i < key.decomp().size(); i++) { Decomp d = (Decomp) key.decomp().elementAt(i); String pat = d.pattern(); if (syns.matchDecomp(s, pat, reply)) { String rep = assemble(d, reply, gotoKey); if (rep != null) return rep; if (gotoKey.key() != null) return null; } } return null; }