public override float CanUnify(Unifiable with) { if (templateNode.NodeType == XmlNodeType.Text) { string srch = (" " + with.ToValue(query) + " ").ToUpper(); return(((" " + templateNode.InnerText + " ").ToUpper().Contains(srch)) ? AND_TRUE : AND_FALSE); } if (templateNode.HasChildNodes) { // recursively check foreach (XmlNode childNode in templateNode.ChildNodes) { try { if (childNode.NodeType == XmlNodeType.Text) { string srch = (" " + with.ToValue(query) + " ").ToUpper(); return(((" " + childNode.InnerText + " ").ToUpper().Contains(srch)) ? AND_TRUE : AND_FALSE); } AIMLTagHandler part = GetChildTagHandler(childNode); if (part.CallCanUnify(with) > 0) { return(AND_FALSE); } } catch (Exception e) { Proc.writeToLog(e); writeToLogWarn("" + e); } } return(AND_TRUE); } return(AND_TRUE); }
public override string ToValue(SubQuery subquery) { if (best != null) { string res = best.ToValue(subquery); if (res != null && Trim(res).Length > 0) { return(res); } } foreach (Unifiable u in List) { if (ReferenceEquals(best, u)) { continue; } string res = u.ToValue(subquery); if (res != null && Trim(res).Length > 0) { best = u; return(res); } } throw noBest(); }
protected override Unifiable ComputeInnerOrNull() { if (FinalResultValid) { return(FinalResult); } Unifiable u = ProcessChange0(); if (IsIncomplete(u)) { Unifiable defaultVal = GetAttribValue("default", null); if (defaultVal == null) { QueryHasFailed = true; return(FAIL); } return(defaultVal); } string s = u.ToValue(query); if (s.Contains(" ")) { return(s); } if (s.ToLower().StartsWith("unknown")) { s = s.Substring(7); writeToLog("SETTINGS UNKNOWN " + s); return("unknown " + s); } return(u); }
/// <summary> /// The method that does the actual Processing of the text. /// </summary> /// <returns>The resulting Processed text</returns> protected override Unifiable Format(Unifiable templateNodeInnerText) { if (CheckNode("uppercase")) { return(templateNodeInnerText.ToValue(query).ToUpper(Proc.Locale)); } return(Unifiable.Empty); }
public override float CanUnify(Unifiable with) { if (templateNode.NodeType == XmlNodeType.Text) { string srch = ToUpper(" " + with.ToValue(query) + " "); return((ToUpper(" " + templateNode.InnerText + " ").Equals(srch)) ? STAR_TRUE : STAR_FALSE); } if (templateNode.HasChildNodes) { { Unifiable rest = with; // recursively check foreach (XmlNode childNode in templateNode.ChildNodes) { with = rest.First; rest = rest.Rest; try { if (childNode.NodeType == XmlNodeType.Text) { string srch = ToUpper(" " + with.ToValue(query) + " "); return((ToUpper(" " + templateNode.InnerText + " ").Equals(srch)) ? STAR_TRUE : STAR_FALSE); } AIMLTagHandler part = GetChildTagHandler(childNode); if (part.CallCanUnify(with) > 0) { return(STAR_FALSE); } } catch (Exception e) { AltBot.writeDebugLine("" + e); } } return(IsNullOrEmpty(rest) ? STAR_TRUE : STAR_FALSE); } } return(IsNullOrEmpty(with) ? STAR_TRUE : STAR_FALSE); }
public override float CanUnify(Unifiable with) { string re = ComputeInner(); var matcher = new Regex(re); if (matcher.IsMatch(with.ToValue(query))) { SetWith(templateNode, with); return(AND_TRUE); } return(AND_FALSE); }
protected bool TextWith(XmlNode templateNode, Unifiable with, out bool tf, out string toXMLValueNotOuter) { toXMLValueNotOuter = ToXMLValueNotOuter(templateNode); if (toXMLValueNotOuter != null) { string srch = (" " + with.ToValue(query) + " ").ToUpper(); tf = (" " + toXMLValueNotOuter + " ").ToUpper().Contains(srch); SetWith(templateNode, with); return(true); } tf = false; return(false); }
internal Unifiable SystemExecute(Unifiable cmd, Unifiable langu, Request user) { if (IsNullOrEmpty(langu)) { langu = GlobalSettings.grabSetting("systemlang") ?? "bot"; } else { langu = ToLower(Trim(langu)); } Unifiable s = "The system tag should be doing '" + cmd + "' lang=" + langu; writeToLog(s.AsString()); SystemExecHandler handler; if (SettingsDictionaryReal.TryGetValue(ExecuteHandlers, langu, out handler)) { try { object o = handler(cmd, user); return(Unifiable.Create(o)); } catch (Exception e) { writeToLog(e); return(Unifiable.Empty); } } else { try { object self = user; ScriptInterpreter parent = null; ScriptInterpreter si = ScriptManager.LoadScriptInterpreter(langu, self, parent); object o = ScriptManager.EvalScriptInterpreter(cmd.ToValue(user.CurrentQuery), langu, self, parent, writeToLog); string siStr = si.Str(o); return(Unifiable.Create(siStr)); } catch (Exception e) { writeToLog(e); } } writeToLog(s); return(Unifiable.Empty); }
public static bool IsLogicTF(Unifiable v, SubQuery subquery) { if (IsFalse(v)) { return(false); } String value = v.ToValue(subquery).ToLower(); if (value.Length == 0) { return(false); } char c = value[0]; if (c == 'n' || c == 'f') { return(false); } return(true); }
protected override Unifiable ProcessChangeU() { if (base.CheckNode("cycterm")) { Unifiable filter = base.GetAttribValue("filter", GetAttribValue("isa", "Thing")); Unifiable pos = base.GetAttribValue("pos", null); int maxWords = int.Parse(base.GetAttribValue("maxwords", "1")); Unifiable r = Recurse(); if (Unifiable.IsNullOrEmpty(r)) { QueryHasFailed = true; return(FAIL); } string s = r.ToValue(query); if (s.Split(' ').Length > maxWords) { QueryHasFailed = true; return(FAIL); } Unifiable term; if (Proc.TheCyc.Lookup(r, filter, out term, query)) { s = term.AsString(); if (s.Length < 2) { writeToLog("CYCTERM: " + r + "=>" + s); } return(term); } else { QueryHasFailed = true; return(FAIL); } } return(Unifiable.Empty); }
public override float CanUnify(Unifiable with) { string srch = with.ToValue(query); if (templateNode.NodeType == XmlNodeType.Text) { bool unifyWithTextNode = UnifyWithTextNode(templateNode, srch); return(unifyWithTextNode ? AND_TRUE : AND_FALSE); } if (templateNode.HasChildNodes) { // recursively check foreach (XmlNode childNode in templateNode.ChildNodes) { try { if (childNode.NodeType == XmlNodeType.Text) { return(UnifyWithTextNode(childNode, srch) ? AND_TRUE : AND_FALSE); } AIMLTagHandler part = GetChildTagHandler(childNode); if (part.CallCanUnify(with) > 0) { return(AND_FALSE); } } catch (Exception e) { Proc.writeToLog(e); writeToLogWarn("" + e); } } return(AND_TRUE); } return(AND_TRUE); }
public override float CanUnify(Unifiable with) { string re = ComputeInner(); string wnPos = GetAttribValue("wnpos", "").ToLower(); string wnRelation = GetAttribValue("wnrel", "").ToLower(); //Lookup definition for current word we could unify with string wordAttributes = ""; string key = (string)with.ToValue(query).Trim(); if (Proc.wordAttributeHash.Contains(key)) { wordAttributes = (string)Proc.wordAttributeHash[key]; } else { if (Proc.wordAttributeHash.Contains(key.ToLower())) { key = key.ToLower(); wordAttributes = (string)Proc.wordAttributeHash[key]; } } // Can you find a match inside ? var matcher = new Regex(re); if (matcher.IsMatch(wordAttributes)) { return(AND_TRUE); } // Ok, lets try WordNet WordNetEngine ourWordNetEngine = Proc.wordNetEngine; Set <SynSet> synPatternSet = null; // find our POS domain if possible WordNetEngine.POS ourPOS = WordNetEngine.POS.Noun; if (wnPos.Length > 0) { // populate POS list foreach (WordNetEngine.POS p in Enum.GetValues(typeof(WordNetEngine.POS))) { if (p != WordNetEngine.POS.None) { if (p.ToString().ToLower().Equals(wnPos) || p.ToString().ToLower().StartsWith(wnPos)) { ourPOS = p; } } } } if (ourWordNetEngine == null) { writeDebugLine("Wordnet engine not loaded .. returning AND_FALSE"); { return(AND_FALSE); } } try { synPatternSet = ourWordNetEngine.GetSynSets(re, ourPOS); } catch (Exception) { return(AND_FALSE); } if (synPatternSet.Count == 0) { try { synPatternSet = ourWordNetEngine.GetSynSets(re.ToLower(), ourPOS); } catch (Exception) { return(AND_FALSE); } } Set <SynSet> synInputSet = null; try { synInputSet = ourWordNetEngine.GetSynSets(key, ourPOS); } catch (Exception) { return(AND_FALSE); } if (synInputSet.Count == 0) { try { synInputSet = ourWordNetEngine.GetSynSets(key.ToLower(), ourPOS); } catch (Exception) { return(AND_FALSE); } } List <WordNetEngine.SynSetRelation> vlist = new List <WordNetEngine.SynSetRelation>(); //[2]; //vlist[0] = WordNetEngine.SynSetRelation.Hyponym; //vlist[1] = WordNetEngine.SynSetRelation.InstanceHyponym; //vlist[0] = WordNetEngine.SynSetRelation.Hypernym ; //vlist[1] = WordNetEngine.SynSetRelation.InstanceHypernym; if (wnRelation.Length == 0) { vlist.Add(WordNetEngine.SynSetRelation.Hypernym); vlist.Add(WordNetEngine.SynSetRelation.InstanceHypernym); } else { // populate Relation list foreach (WordNetEngine.SynSetRelation r in Enum.GetValues(typeof(WordNetEngine.SynSetRelation))) { if (r != WordNetEngine.SynSetRelation.None) { if (r.ToString().ToLower().Contains(wnRelation)) { vlist.Add(r); } } } } if ((synInputSet.Count > 0) && (synPatternSet.Count > 0)) { foreach (SynSet synDstSet in synInputSet) { foreach (SynSet synSrcSet in synPatternSet) { //synSets.Items.Add(synSet); List <SynSet> linkageList = null; linkageList = synDstSet.GetShortestPathTo(synSrcSet, vlist); if ((linkageList != null) && (linkageList.Count > 0)) { return(AND_TRUE); } } } return(AND_FALSE); } return(AND_FALSE); }
private bool lookup0(Unifiable textIn, Unifiable filter, out Unifiable term, SubQuery subquery) { string text = textIn.ToValue(subquery); if (text.Length < 2) { term = text; return(false); } string textStr = text.StartsWith("\"") ? text : String.Format("\"{0}\"", text); if (!CycEnabled) { term = textStr; return(true); } filter = Cyclify(filter); bool nospaces = !text.Contains(" "); string nqtext = textStr.Trim("\"".ToCharArray()); string ptext = nqtext.Substring(0, 1).ToUpper() + nqtext.Substring(2); if (nospaces) { if (Unifiable.IsFalse(EvalSubL(String.Format("(car (fi-complete \"{0}-TheWord\"))", ptext), null))) { //no word used nospaces = false; } } if (false || lookupCycTerm("(#$nameString ?CYCOBJECT \"%s\")", text, filter, out term) || (nospaces && (lookupCycTerm("(#$denotation #$%s-TheWord ?TEXT ?TYPE ?CYCOBJECT)", ptext, filter, out term) || lookupCycTerm("(#$denotationRelatedTo #$%s-TheWord ?TEXT ?TYPE ?CYCOBJECT)", ptext, filter, out term))) || lookupCycTerm("(#$initialismString ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$abbreviationString-PN ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$preferredNameString ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$countryName-LongForm ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$countryName-ShortForm ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$acronymString ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$scientificName ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$termStrings-GuessedFromName ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$prettyString ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$preferredTermStrings ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$and (#$isa ?P #$ProperNamePredicate-Strict)(?P ?CYCOBJECT \"%s\"))", text, filter, out term) || lookupCycTerm("(#$and (#$isa ?P #$ProperNamePredicate-General)(?P ?CYCOBJECT \"%s\"))", text, filter, out term) || (nospaces && lookupCycTerm("(#$preferredGenUnit ?CYCOBJECT ?POS #$%s-TheWord )", ptext, filter, out term)) || lookupCycTerm("(#$termStrings ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$nicknames ?CYCOBJECT \"%s\")", text, filter, out term) || lookupCycTerm("(#$and (#$wordStrings ?WORD \"%s\") (#$or (#$denotation ?WORD ?TEXT ?TYPE ?CYCOBJECT) (#$denotationRelatedTo ?WORD ?TEXT ?TYPE ?CYCOBJECT) ))", text, filter, out term)) { return(true); } term = EvalSubL(String.Format("(car (fi-complete \"{0}\"))", nqtext), null); // Followed by asking Cyc to guess at the word using (fi-complete \”%s\”) if (Unifiable.IsTrue(term)) { if (IsaFilter(term, filter)) { return(true); } } term = EvalSubL(String.Format("(cdr (car (denotation-mapper \"{0}\")))", nqtext), null); if (Unifiable.IsTrue(term)) { if (IsaFilter(term, filter)) { return(true); } } term = EvalSubL(String.Format("(car (denots-of-string \"{0}\"))", nqtext), null); if (Unifiable.IsTrue(term)) { if (IsaFilter(term, filter)) { return(true); } } // and if that fails returns a Unifiable of using #$\”%s\” term = string.Format("#${0}", text); return(false); }
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; }