public virtual void Copy(Key k) { key = k.GetKey(); rank = k.Rank(); decomp = k.Decomp(); }
/// <summary>Decompose a string according to the given key.</summary> /// <remarks> /// 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; /// </remarks> private string Decompose(Key key, string s, Key gotoKey) { string[] reply = new string[10]; for (int i = 0; i < key.Decomp().Count; i++) { Decomp d = (Decomp)key.Decomp()[i]; string pat = d.Pattern(); if (syns.MatchDecomp(s, pat, reply)) { string rep = Assemble(d, reply, gotoKey); if (rep != null) { return rep; } if (gotoKey.GetKey() != null) { return null; } } } return null; }
/// <summary>Assembly a reply from a decomp rule and the input.</summary> /// <remarks> /// 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. /// </remarks> private 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.GetKey() != null) { return null; } ConsoleSurrogate.WriteLine("Goto rule did not match key: " + lines[0]); return null; } string work = string.Empty; while (EString.Match(rule, "* (#)*", lines)) { // reassembly rule with number substitution rule = lines[2]; // there might be more int n = 0; try { n = int.Parse(lines[1]) - 1; } catch (FormatException) { ConsoleSurrogate.WriteLine("Number is wrong in reassembly rule " + lines[1]); } if (n < 0 || n >= reply.Length) { ConsoleSurrogate.WriteLine("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; }
/// <summary>Process a sentence.</summary> /// <remarks> /// Process a sentence. (1) Make pre transformations. (2) Check for quit /// word. (3) Scan sentence for keys, build key stack. (4) Try decompositions /// for each key. /// </remarks> private string Sentence(string s) { s = pre.Translate(s); s = EString.Pad(s); if (quit.Find(s)) { finished = true; return finl; } keys.BuildKeyStack(keyStack, s); for (int i = 0; i < keyStack.KeyTop(); i++) { Key gotoKey = new Key(); string reply = Decompose(keyStack.Key(i), s, gotoKey); if (reply != null) { return reply; } // If decomposition returned gotoKey, try it while (gotoKey.GetKey() != null) { reply = Decompose(gotoKey, s, gotoKey); if (reply != null) { return reply; } } } return null; }