예제 #1
0
        void DoMatching(List <PatternTemplateSource> dicta, string input)
        {
            if (verbose > 0)
            {
                Console.WriteLine("Parsing input...");
            }
            IParsedPhrase phrase = parser.Parse(input);

            if (verbose > 0)
            {
                Console.WriteLine("Matching templates...");
            }

            // Add a codelet for each of these, to match the input
            if (!serialmode)
            {
                foreach (PatternTemplateSource dictum in dicta)
                {
                    IFailure fail = tryToRescueMatch.MakeFailure(phrase, dictum, this, new NopCallable(), coderack);
                    dictum.Generate(coderack, phrase, this, fail, 1.0);
                }
            }
            else
            {
                SerialTemplateMatcher matcher = new SerialTemplateMatcher(this, this, coderack, tryToRescueMatch, phrase, dicta, 1.0);
                matcher.MatchNextSentence();
            }

            RunToEnd();
        }
        public override bool CallRescue(Coderack coderack, IParsedPhrase input, PatternTemplateSource patternTemplateSource, string reason, IContinuation skip, IContinuation succ, IFailure fail)
        {
            List <string> words = GroupPhrase.PhraseToTexts(input);

            bool          changed   = false;
            List <string> corrected = new List <string>();

            foreach (string word in words)
            {
                string correct = comparer.GetCorrects(word)[0];
                if (correct.ToLower() != word.ToLower())
                {
                    changed = true;
                }
                corrected.Add(correct);
            }

            if (changed)
            {
                IParsedPhrase correct  = parser.Parse(StringUtilities.JoinWords(corrected));
                IFailure      fallfail = fallback.MakeFailure(input, patternTemplateSource, succ, fail, coderack);
                patternTemplateSource.Generate(coderack, correct, succ, fallfail, weight);
                return(true);
            }
            else
            {
                return(fallback.CallRescue(coderack, input, patternTemplateSource, reason, skip, succ, fail));
            }
        }
예제 #3
0
        protected bool MatchNextTemplate()
        {
            if (remainingDicta.Count == 0)
            {
                return(false);
            }

            PatternTemplateSource dictum = remainingDicta.Dequeue();

            if (dictum.Pattern.Contents[0].Name == "%sentence")
            {
                IFailure fail = tryToRescueMatch.MakeFailure(inputs[inputIndex], dictum, this, this, coderack);
                dictum.Generate(coderack, inputs[inputIndex], this, fail, weight);

                return(true);
            }
            else if (dictum.Pattern.Contents[0].Name == "%sentences")
            {
                int count = 1;
                foreach (IContent content in dictum.Pattern.Contents)
                {
                    if (content.Name == "/")
                    {
                        count++;
                    }
                }

                GroupPhrase groupPhrase = new GroupPhrase("=P", inputs.GetRange(inputIndex, count));
                IFailure    fail        = tryToRescueMatch.MakeFailure(groupPhrase, dictum, this, this, coderack);
                dictum.Generate(coderack, groupPhrase, this, fail, weight);

                return(true);
            }
            else
            {
                receiver.Receive(String.Format("Template starting with {0} not available in serial mode.", dictum.Pattern.Contents[0].Name), dictum);
                return(MatchNextTemplate());
            }
        }