Beispiel #1
0
 /// <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++)
     {
         var    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);
 }
Beispiel #2
0
        /// <summary>Process a line of input.</summary>
        /// <remarks>Process a line of input.</remarks>
        public virtual string ProcessInput(string s)
        {
            string reply;

            // Do some input transformations first.
            s = EString.Translate(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"
                                  );
            s = EString.Translate(s, "@#$%^&*()_-+=~`{[}]|:;<>\\\"", "                          "
                                  );
            s = EString.Translate(s, ",?!", "...");
            // Compress out multiple speace.
            s = EString.Compress(s);
            string[] lines = new string[2];
            // Break apart sentences, and do each separately.
            while (EString.Match(s, "*.*", lines))
            {
                reply = Sentence(lines[0]);
                if (reply != null)
                {
                    return(reply);
                }
                s = EString.Trim(lines[1]);
            }
            if (s.Length != 0)
            {
                reply = Sentence(s);
                if (reply != null)
                {
                    return(reply);
                }
            }
            // Nothing matched, so try memory.
            string m = mem.Get();

            if (m != null)
            {
                return(m);
            }
            // No memory, reply with xnone.
            Key key = keys.GetKey("xnone");

            if (key != null)
            {
                Key dummy = null;
                reply = Decompose(key, s, dummy);
                if (reply != null)
                {
                    return(reply);
                }
            }
            // No xnone, just say anything.
            return("I am at a loss for words.");
        }
Beispiel #3
0
        /// <summary>Translate a string s.</summary>
        /// <remarks>
        /// Translate a string s.
        /// (1) Trim spaces off.
        /// (2) Break s into words.
        /// (3) For each word, substitute matching src word with dest.
        /// </remarks>
        public virtual string Translate(string s)
        {
            string[] lines = new string[2];
            string   work  = EString.Trim(s);

            s = string.Empty;
            while (EString.Match(work, "* *", lines))
            {
                s   += Xlate(lines[0]) + " ";
                work = EString.Trim(lines[1]);
            }
            s += Xlate(work);
            return(s);
        }
Beispiel #4
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);
        }
        /// <summary>Break the string s into words.</summary>
        /// <remarks>
        /// Break the string s into words.
        /// For each word, if isKey is true, then push the key
        /// into the stack.
        /// </remarks>
        public virtual void BuildKeyStack(KeyStack stack, string s)
        {
            stack.Reset();
            s = EString.Trim(s);
            string[] lines = new string[2];
            Key      k;

            while (EString.Match(s, "* *", lines))
            {
                k = GetKey(lines[0]);
                if (k != null)
                {
                    stack.PushKey(k);
                }
                s = lines[1];
            }
            k = GetKey(s);
            if (k != null)
            {
                stack.PushKey(k);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Decomposition match,
        /// If decomp has no synonyms, do a regular match.
        /// </summary>
        /// <remarks>
        /// Decomposition match,
        /// If decomp has no synonyms, do a regular match.
        /// Otherwise, try all synonyms.
        /// </remarks>
        internal virtual bool MatchDecomp(string str, string pat, string[] lines)
        {
            if (!EString.Match(pat, "*@* *", lines))
            {
                //  no synonyms in decomp pattern
                return(EString.Match(str, pat, lines));
            }
            //  Decomp pattern has synonym -- isolate the synonym
            string first   = lines[0];
            string synWord = lines[1];
            string theRest = " " + lines[2];
            //  Look up the synonym
            WordList syn = Find(synWord);

            if (syn == null)
            {
                ConsoleSurrogate.WriteLine("Could not fnd syn list for " + synWord);
                return(false);
            }
            //  Try each synonym individually
            for (int i = 0; i < syn.Count; i++)
            {
                //  Make a modified pattern
                pat = first + (string)syn[i] + theRest;
                if (EString.Match(str, pat, lines))
                {
                    int n = EString.Count(first, '*');
                    //  Make room for the synonym in the match list.
                    for (int j = lines.Length - 2; j >= n; j--)
                    {
                        lines[j + 1] = lines[j];
                    }
                    //  The synonym goes in the match list.
                    lines[n] = (string)syn[i];
                    return(true);
                }
            }
            return(false);
        }
Beispiel #7
0
 /// <summary>Process a line of script input.</summary>
 /// <remarks>Process a line of script input.</remarks>
 private void Collect(string s)
 {
     string[] lines = new string[4];
     if (EString.Match(s, "*reasmb: *", lines))
     {
         if (lastReasemb == null)
         {
             ConsoleSurrogate.WriteLine("Error: no last reasemb");
             return;
         }
         lastReasemb.Add(lines[1]);
     }
     else
     {
         if (EString.Match(s, "*decomp: *", lines))
         {
             if (lastDecomp == null)
             {
                 ConsoleSurrogate.WriteLine("Error: no last decomp");
                 return;
             }
             lastReasemb = new ReasembList();
             string temp = lines[1];
             if (EString.Match(temp, "$ *", lines))
             {
                 lastDecomp.Add(lines[0], true, lastReasemb);
             }
             else
             {
                 lastDecomp.Add(temp, false, lastReasemb);
             }
         }
         else
         {
             if (EString.Match(s, "*key: * #*", lines))
             {
                 lastDecomp  = new DecompList();
                 lastReasemb = null;
                 int n = 0;
                 if (lines[2].Length != 0)
                 {
                     try
                     {
                         n = int.Parse(lines[2]);
                     }
                     catch (FormatException)
                     {
                         ConsoleSurrogate.WriteLine("Number is wrong in key: " + lines[2]);
                     }
                 }
                 keys.Add(lines[1], n, lastDecomp);
             }
             else
             {
                 if (EString.Match(s, "*key: *", lines))
                 {
                     lastDecomp  = new DecompList();
                     lastReasemb = null;
                     keys.Add(lines[1], 0, lastDecomp);
                 }
                 else
                 {
                     if (EString.Match(s, "*synon: * *", lines))
                     {
                         var words = new WordList();
                         words.Add(lines[1]);
                         s = lines[2];
                         while (EString.Match(s, "* *", lines))
                         {
                             words.Add(lines[0]);
                             s = lines[1];
                         }
                         words.Add(s);
                         syns.Add(words);
                     }
                     else
                     {
                         if (EString.Match(s, "*pre: * *", lines))
                         {
                             pre.Add(lines[1], lines[2]);
                         }
                         else
                         {
                             if (EString.Match(s, "*post: * *", lines))
                             {
                                 post.Add(lines[1], lines[2]);
                             }
                             else
                             {
                                 if (EString.Match(s, "*initial: *", lines))
                                 {
                                     initial = lines[1];
                                 }
                                 else
                                 {
                                     if (EString.Match(s, "*final: *", lines))
                                     {
                                         finl = lines[1];
                                     }
                                     else
                                     {
                                         if (EString.Match(s, "*quit: *", lines))
                                         {
                                             quit.Add(" " + lines[1] + " ");
                                         }
                                         else
                                         {
                                             ConsoleSurrogate.WriteLine("Unrecognized input: " + s);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }