Eliza decomposition rule
コード例 #1
0
        /// <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);
        }
コード例 #2
0
 /// <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);
 }
コード例 #3
0
		/// <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;
		}