Beispiel #1
0
 internal string FixPronouns(string englishIn, Func <string, string> whoAmI)
 {
     var pns = new[]
     {
         "him", "he", "she", "her", "them", "they", "it", "this",
         "i", "you", "me", "my", "your", "our", "their",
     };
     string english        = " " + englishIn.Trim() + " ";
     string englishToLower = english.ToLower();
     {
         foreach (var pronoun in pns)
         {
             if (englishToLower.Contains(" " + pronoun + " "))
             {
                 if (whoAmI == null)
                 {
                     writeToLog("FixPronouns: DONT KNOW THE USER TO RESOLVE '" + pronoun + "'");
                     return(english);
                 }
                 Unifiable v = Unifiable.Create(whoAmI(pronoun));
                 bool      goodReplacement = !Unifiable.IsIncomplete(v) && !Unifiable.IsNullOrEmpty(v) &&
                                             !Unifiable.IsMissing(v);
                 if (!goodReplacement)
                 {
                     writeToLog("FixPronouns: BAD REPLACEMENT '" + pronoun + "' => '" + v + "'");
                     continue;
                 }
                 english = ReplaceWord(english, pronoun, v);
                 if (pronoun != pronoun.Trim())
                 {
                     english = ReplaceWord(english, pronoun + "s", v + "s");
                 }
             }
         }
         englishToLower = english.ToLower();
         foreach (var pronoun in pns)
         {
             if (englishToLower.Contains(" " + pronoun + " "))
             {
                 Unifiable v = Unifiable.Create(whoAmI(pronoun));
                 writeToLog("FixPronouns: BAD REPLACEMENT '" + pronoun + "' => '" + v + "'");
             }
             continue;
         }
         return(english);
     }
 }
        private static bool ShouldSet(XmlNode templateNode, ISettingsDictionary dictionary, string name, object newValue, Unifiable oldValue, SubQuery query)
        {
            if (templateNode == null)
            {
                return(true);
            }
            bool canSet = query.UseDictionaryForSet(dictionary);

            ;
            bool onlyIfUnknown;

            if (StaticXMLUtils.TryParseBool(templateNode, "ifUnknown", out onlyIfUnknown))
            {
                if (onlyIfUnknown)
                {
                    return((Unifiable.IsMissing(oldValue) || IsIncomplete(oldValue)) && canSet);
                }
            }

            bool overwriteExisting;

            if (StaticXMLUtils.TryParseBool(templateNode, "overwriteExisting", out overwriteExisting))
            {
                if (!overwriteExisting)
                {
                    return((Unifiable.IsNullOrEmpty(oldValue) || IsIncomplete(oldValue)) && canSet);
                }
                //if (overwriteExisting)
                return(true);
            }

            string oldMatch  = StaticXMLUtils.GetAttribValue(templateNode, "existing", null);
            bool   shouldSet = true;

            if (oldMatch != null)
            {
                if (!StaticAIMLUtils.IsPredMatch(oldMatch, oldValue, null))
                {
                    shouldSet = false;
                }
            }
            var    newValueU = Unifiable.Create(newValue);
            string newMatch  = StaticXMLUtils.GetAttribValue(templateNode, "matches", null);

            if (newMatch != null)
            {
                if (!StaticAIMLUtils.IsPredMatch(newMatch, newValueU, null))
                {
                    shouldSet = false;
                }
            }
            string wontvalue = StaticXMLUtils.GetAttribValue(templateNode, "wontvalue", null);

            if (wontvalue != null)
            {
                if (StaticAIMLUtils.IsPredMatch(wontvalue, newValueU, null))
                {
                    shouldSet = false;
                }
            }
            return(shouldSet && canSet);
        }
        static public Unifiable GetSettingForType(string subject, SubQuery query, ISettingsDictionary dict, string name, out string realName, string gName, Unifiable defaultVal, out bool succeed, XmlNode node)
        {
            Request             request    = query.Request;
            OutputDelegate      writeToLog = request.writeToLog ?? TextPatternUtils.DEVNULL;
            AltBot              TargetBot  = request.TargetBot;
            ISettingsDictionary udict;
            string              dictName = AIMLTagHandler.GetNameOfDict(query, subject ?? dict.NameSpace, node, out udict);
            // try to use a global blackboard predicate
            User gUser = TargetBot.ExemplarUser;

            defaultVal = StaticXMLUtils.GetAttribValue(node, "default,defaultValue", defaultVal);
            gName      = StaticXMLUtils.GetAttribValue(node, "global_name", gName);

            string realName0;


            var vv = ScriptManager.GetGroup(query.TargetBot.ObjectRequester, dictName, name);
            {
                if (vv != null)
                {
                    if (vv.Count == 0)
                    {
                        succeed  = true;
                        realName = name;
                        return("");
                    }
                    succeed  = true;
                    realName = name;
                    foreach (var e in vv)
                    {
                        return(Unifiable.Create(e));
                    }
                }
            }
            Unifiable resultGet = SettingsDictionaryReal.grabSettingDefaultDict(udict, name, out realName0);

            if (ReferenceEquals(resultGet, null))
            {
                realName  = null;
                resultGet = Unifiable.NULL;
            }
            // if ((!String.IsNullOrEmpty(result)) && (!result.IsWildCard())) return result; // we have a local one

            String realNameG;
            // try to use a global blackboard predicate
            Unifiable gResult = SettingsDictionaryReal.grabSettingDefaultDict(gUser.Predicates, gName, out realNameG);

            if ((Unifiable.IsMissing(resultGet)) && (!Unifiable.IsMissing(gResult)))
            {
                // result=nothing, gResult=something => return gResult
                writeToLog("SETTINGS OVERRIDE " + gResult);
                succeed  = true;
                realName = realNameG;
                // return gResult;
            }
            string sresultGet = resultGet.ToValue(query);

            // if Unknown or empty
            if (UseLuceneForGet && Unifiable.IsMissing(sresultGet))
            {
                Unifiable userName = udict.grabSetting("id");
                if (Unifiable.IsNullOrEmpty(userName))
                {
                    writeToLog("ERROR IsNullOrEmpty id in " + udict.NameSpace);
                }
                ITripleStore userbotLuceneIndexer = (ITripleStore)query.Request.TargetBot.TripleStore;
                string       resultLucene         = userbotLuceneIndexer.queryTriple(userName, name, node);
                if (!string.IsNullOrEmpty(resultLucene))
                {
                    succeed  = true;
                    realName = name;
                    return(resultLucene);
                }
            }


            if (sresultGet != null)
            {
                if (sresultGet.ToUpper() == "UNKNOWN")
                {
                    succeed  = false;
                    realName = null;
                    return(sresultGet + " " + name);
                }
                else if (Unifiable.IsEMPTY(resultGet))
                {
                    succeed  = true;
                    realName = name;
                    return(resultGet);
                }
                else if (Unifiable.IsUnknown(resultGet))
                {
                    succeed  = false;
                    realName = name;
                    return(resultGet);
                }
            }
            if (!String.IsNullOrEmpty(sresultGet))
            {
                succeed  = true;
                realName = realName0;
                query.GetDictValue++;
                if (!IsNullOrEmpty(gResult))
                {
                    if (resultGet.IsWildCard)
                    {
                        realName = realNameG;
                        // result=*, gResult=something => return gResult
                        return(gResult);
                    }
                    // result=something, gResult=something => return result
                    return(resultGet);
                }
                else
                {
                    // result=something, gResult=nothing => return result
                    return(resultGet);
                }
            }
            if (defaultVal == null)
            {
                succeed  = false;
                realName = null;
                return(defaultVal);
            }
            // default => return defaultVal
            succeed  = true;
            realName = realName0;
            return(ReturnSetSetting(udict, name, defaultVal));
            //return defaultVal;
        }
        /// <summary>
        /// The method that does the actual Processing of the text.
        /// </summary>
        /// <returns>The resulting Processed text</returns>
        protected override Unifiable ProcessChangeU()
        {
            if (templateNode.NodeType == XmlNodeType.Comment)
            {
                return(Succeed(templateNode.Value));
            }
            if (templateNode.NodeType == XmlNodeType.Text)
            {
                string s = Trim(templateNode.InnerText);
                if (String.IsNullOrEmpty(s))
                {
                    return(Unifiable.Empty);
                }
                return(s);
            }
            string currentNodeName = templateNode.Name.ToLower();

            if (currentNodeName == "substitutions")
            {
                var prevDict = request.TargetSettings;
                // Process each of these child "settings"? nodes
                try
                {
                    request.TargetSettings = request.TargetBot.InputSubstitutions;
                    SettingsDictionaryReal.loadSettingNode(request.TargetSettings, templateNode, SettingsPolicy.Default, request);
                }
                finally
                {
                    request.TargetSettings = prevDict;
                }
                return(ProcessSucceed());
            }
            if (currentNodeName == "genlmt")
            {
                string name       = GetAttribValue(templateNode, "name,mt,to,super,into", null);
                string removeTo   = GetAttribValue(templateNode, "remove", null);
                string from       = GetAttribValue(templateNode, "graph,from", null);
                bool   deleteLink = false;
                if (name == null)
                {
                    name = Trim(templateNode.InnerText);
                }
                if (removeTo != null)
                {
                    deleteLink = true;
                    name       = removeTo;
                }
                GraphMaster FROM = request.GetGraph(from);
                GraphMaster TO   = request.GetGraph(name);
                if (FROM != null && TO != null)
                {
                    if (deleteLink)
                    {
                        FROM.RemoveGenlMT(TO, writeToLog);
                    }
                    else
                    {
                        FROM.AddGenlMT(TO, writeToLog);
                    }
                    return(Succeed("GENLMT: " + FROM + " => " + name + " => " + TO));
                }
            }
            if (currentNodeName == "sraigraph")
            {
                string name = GetAttribValue(templateNode, "name,mt,to,super,into", null);
                string from = GetAttribValue(templateNode, "graph,from", null);
                if (name == null)
                {
                    name = Trim(templateNode.InnerText);
                }
                GraphMaster FROM = request.GetGraph(from);
                GraphMaster TO   = request.GetGraph(name);
                if (FROM != null && TO != null)
                {
                    FROM.Srai = name;
                    return(Succeed("SRAI: " + FROM + " => " + name + " => right now " + TO));
                }
                return(Failure("FROM '" + from + "'='" + FROM + "'" + " TO '" + name + "'='" + TO + "'"));
            }
            if (currentNodeName == "meta")
            {
                return(Succeed("UNUSED: " + templateNode.OuterXml));
            }
            if (currentNodeName == "#comment")
            {
                return(Succeed("UNUSED: " + templateNode.OuterXml));
            }
            if (currentNodeName == "item")
            {
                SettingsDictionaryReal.loadSettingNode(request.TargetSettings, templateNode, SettingsPolicy.Default, request);
                return(ProcessSucceed());
            }
            if (currentNodeName == "bot")
            {
                SettingsDictionaryReal.loadSettingNode(request.TargetBot.Settings, templateNode, SettingsPolicy.Default, request);
                return(ProcessSucceed());
            }
            string currentNodeOuterXml = templateNode.OuterXml;

            if (currentNodeOuterXml.Length > 280)
            {
                currentNodeOuterXml = TextFilter.ClipString(currentNodeOuterXml, 280);
            }
            writeToLog("ImmediateAiml: " + currentNodeOuterXml);

            /*
             * <TestCase name="connect">
             *      <Input>CONNECT</Input>
             *      <ExpectedAnswer>Connected to test case AIML set.</ExpectedAnswer>
             * </TestCase>
             */

            if (templateNode.NodeType == XmlNodeType.Comment)
            {
                return(ProcessSucceed());
            }

            // pull from late bound sustituion dictionaries
            var sd = request.GetSubstitutions(currentNodeName, false);

            if (sd != null)
            {
                if (FinalResultValid)
                {
                    return(FinalResult);
                }
                if (!Unifiable.IsIncomplete(FinalResult))
                {
                    return(FinalResult);
                }
                Func <Unifiable, Unifiable> Format = (v) => ApplySubstitutions.Substitute(sd, templateNodeInnerText);
                if (base.isRecursive && !ReadOnly)
                {
                    FinalResult = Format(TransformAtomically(null, true));
                    return(finalResult.Value);
                }
                return(FinalResult = TransformAtomically(Format, false));
            }

            if (AltBot.UnknownTagsAreBotVars)
            {
                var dict = Proc.GlobalSettings;
                if (dict.containsSettingCalled(currentNodeName))
                {
                    var v = dict.grabSetting(currentNodeName);
                    if (!Unifiable.IsMissing(v))
                    {
                        return(v);
                    }
                }
            }
            var            vs         = Proc.EvalAiml(templateNode, request, request.writeToLog);
            StringBuilder  sb         = new StringBuilder();
            int            writeThrus = 0;
            int            total      = 0;
            OutputDelegate WriteLine  = DLRConsole.SystemWriteLine;

            foreach (var node in vs)
            {
                total++;
                string nodeOuterXml = ToXmlValue(node);
                WriteLine(nodeOuterXml);
                string p = GetAttribValue(node, "PASSED", "FALSE");
                if (p == "False")
                {
                    writeThrus++;
                    sb.Append("\n" + nodeOuterXml.Replace("\" ", "\"\n ") + "\n");
                }
                WriteLine("");
            }

            WriteLine("");
            WriteLine("");
            WriteLine("" + writeThrus);
            WriteLine("");
            string ss = sb.ToString();

            WriteLine(ss);
            WriteLine("");
            WriteLine("");
            WriteLine("");
            return(Succeed("total is " + total));
        }