Example #1
0
        //(mapcar #'(lambda (x) (pwhen (member col x) ))  (denotation-mapper "isa"))

        private bool lookupCycTerm(string template, string text, Unifiable filter, out Unifiable term)
        {
            string textStr = text.StartsWith("\"") ? text : ("\"" + text + "\"");

            if (!char.IsLetter(text[0]))
            {
            }
            template = template.Replace("\"%s\"", textStr);

            //template = template.Replace("#$%s-TheWord", textWord);
            template = template.Replace("%s", text);


            try
            {
                term = EvalSubL(TextPatternUtils.SafeFormat("(first (ask-template '?CYCOBJECT '(#$and {0} (#$isa ?CYCOBJECT {1})) #$EverythingPSC))", template, Unifiable.ToVMString(filter)), null);
            }
            catch (System.Exception ex)
            {
                term = null;
                return(false);
            }
            if (Unifiable.IsFalse(term))
            {
                return(false);
            }
            return(true);
        }
        //public static Unifiable Format(string s, params object[] args)
        //{
        //    return string.Format(s, args);
        //}

        //static public bool operator ==(Unifiable t, string s)
        //{
        //    return t.AsString().ToLower() == s.ToLower();
        //}


        public static bool IsFalse(Unifiable tf)
        {
            if (ReferenceEquals(tf, null))
            {
                return(true);
            }
            if (ReferenceEquals(tf.Raw, null))
            {
                return(true);
            }
            return(tf.IsFalse());
        }
Example #3
0
 public bool Lookup1(Unifiable textIn, Unifiable filter, out Unifiable term, SubQuery subquery)
 {
     if (Unifiable.IsNullOrEmpty(textIn))
     {
         term = null;
         return(false);
     }
     if (textIn.AsString().Contains("#$"))
     {
         term = textIn;
         if (!Unifiable.IsNullOrEmpty(term))
         {
             if (!IsaFilter(term, filter))
             {
                 return(false);
             }
         }
         return(true);
     }
     lock (stringTOResult)
     {
         string key = "" + textIn + "=" + filter;
         if (stringTOResult.TryGetValue(key, out term))
         {
             if (term == NILTerm)
             {
                 return(false);
             }
             return(true);
         }
         bool t          = lookup0(textIn, filter, out term, subquery);
         bool meansFalse = Unifiable.IsFalse(term);
         if (t != meansFalse)
         {
             if (meansFalse)
             {
                 writeToLog("NILTerm true=" + t + " term=" + term + " textIn" + textIn);
                 term = NILTerm;
             }
             else
             {
                 string paraphrase = Paraphrase(term);
                 writeToLog("true={0} term={1} textIn{2}  paraPhrase={3}", t, term, textIn, paraphrase);
             }
         }
         stringTOResult[key] = term;
         return(t);
     }
 }
Example #4
0
 protected override Unifiable ProcessChangeU()
 {
     if (!finalResult.IsValid)
     {
         var v = Recurse();
         var r = Proc.SystemExecute(v, GetAttribValue("lang", "bot"), request);
         if (Unifiable.IsFalse(r))
         {
             //finalResult = r;
             return(Unifiable.Empty);
         }
         else if (Unifiable.IsTrue(r))
         {
             finalResult.Value = r;
             //templateNodeInnerText = isValueSetStart + v;
         }
         return(r);
     }
     return(FinalResult);
 }
Example #5
0
 internal bool IsaFilter(Unifiable term, Unifiable filter)
 {
     if (!Unifiable.IsValue(term))
     {
         return(false);
     }
     if (term == "NIL")
     {
         return(false);
     }
     if (!Unifiable.IsNullOrEmpty(filter))
     {
         if (Unifiable.IsFalse(filter))
         {
             return(true);
         }
         if (this.EvalSubL(TextPatternUtils.SafeFormat("(ask-template 'T `(#$isa {0} {1}) #$EverythingPSC)", Cyclify(term), Cyclify(filter)), null) == "NIL")
         {
             return(false);
         }
     }
     return(true);
 }
Example #6
0
 private object ExecCycQuery(string cmd, Request user)
 {
     try
     {
         if (String.IsNullOrEmpty(cmd))
         {
             return("NIL");
         }
         Unifiable ss = EvalSubL("(cyc-query '" + cmd + " #$EverythingPSC)", null);
         if (Unifiable.IsFalse(ss))
         {
             return(Unifiable.Empty);
         }
         return(ss);
     }
     catch (Exception e)
     {
         TheBot.writeToLog(e);
         string s = "ExecCycQuery: " + e;
         AltBot.writeDebugLine(s);
         writeToLog(s);
         return(null);
     }
 }
Example #7
0
        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);
        }