Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

303
LINES

< > BotCompany Repo | #1017805 // Baked Bot Backup 1

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (14537L/111K).

!7

!include once #1017764 // VirtualMechLists

sS mechLibID = #1400124;
static O onOutput; // VF1<S>
static bool authorized;

static L<AbstractThinkBot> bots;

abstract sclass AbstractThinkBot {
  transient VF1<? super S> postMessage;
  transient new L<Map> recentHistory;

  abstract void thinkAbout(S input);
  
  void postMessage(S s) {
    callF(postMessage, s);
  }
}

p {
  mech_useLibrary(mechLibID);
  L<Class<? extends AbstractThinkBot>> classes = botClasses();
  print("Have think classes: " + classNames(classes));
  makeBots();
  bot();
}

static L<Class<? extends AbstractThinkBot>> botClasses() {
  ret myNonAbstractClassesImplementing(AbstractThinkBot);
}

svoid makeBots() {
  bots = map nuWithoutArguments(botClasses());
}

sbool telegram_amIAuthorized() {
  ret authorized;
}

sS answer(S s) {
  temp tempSetThreadLocal(telegram_msg_tl(), new Map);
  final new L<S> output;
  for (AbstractThinkBot bot : bots) pcall {
    bot.postMessage = voidfunc(S s) {
      print(">> " + s);
      pcallF(onOutput, s);
      output.add(s);
    };
    bot.thinkAbout(s);
  }
  ret rtrim(lines(output));
}




sclass TelegramThinkBot > AbstractThinkBot {
  void thinkAbout(S s) {
    s = trim(s);
    
    final new Matches m;
    S procedure = nlLookup(mechMap("Telegram Procedures"), s, m);
    if (procedure != null) {
      procedure = expandDollarRefsToMatches(procedure, m, true);
      print(">> " + procedure);
      postMessage(strOrNull(javaEvalOrInterpret(procedure)));
    }
    
    print("s=" + s);
    S language = 'english;
    if (swic_trim(s, "!german ", m)) {
      language = 'german;
      s = m.rest();
      print("s=" + s);
    }
    ai_setLanguage(language);

    if (swic_trim(s, "!say ", m))
      postMessage(m.rest());
    
    if (eq(s, "!gac"))
      postMessage(random_gac36k());
    
    if (swic_trim(s, "!parse ", m))
      postMessage(ai_renderCulledParseTree(ai_parseToTreeWithGuessing(m.rest())));
    
    if (swic_trim(s, "!simplify ", m))
      postMessage(lines(ai_parseTree_simplifiedTexts(ai_parseToTreeWithGuessing(m.rest()))));
    
    if (telegram_amIAuthorized()) {
      if (eq(s, "!fresh")) dm_refreshTranspiler();
      
      if (swic_trim(s, "!eval ", m))
        postMessage(pcallOrExceptionText(func { strOrNull(javaEvalOrInterpret(m.rest())) }));
        
      if (swic_trim(s, "!rule ", m)) {
        S rule = m.rest();
        LS rules = trimAll(ai_unparsedTalkRules());
        if (!contains(rules, rule)) { // TODO: ignore global IDs
          appendToMechList_noUniq("NL Logic Examples", "\n" + rule);
          postMessage("Rule saved. Have " + n2(l(rules)+1, "rule") + ".");
        }
      }
      
      if (swic_trim(s, "!fact ", m)) {
        S fact = m.rest();
        LS facts = mL("Random facts");
        if (!contains(facts, fact)) {
          appendToMechList_noUniq("Random facts", fact);
          postMessage("Fact saved. Have " + n2(l(facts)+1, "fact") + ".");
        }
      }
    }
  }
}




sclass TelegramFactsBot > AbstractThinkBot {
  S evalExp(Exp e, NLLogicChecker_v2.Matching m) {
    S code = nlLogic_text(e);
    print("Eval'ing: " + code);
    temp tempAdd(hotwire_copyOver_after, voidfunc(Class c) { copyFields(mc(), c, 'telegram_msg_tl) });
    S result = str(evalWithDollarVars(code, m.matches));
    print("Result: " + shorten(result, 100));
    ret result;
  }

  void thinkAbout(fS s) {
    final Map msg = telegram_msg();
    
    NLLogicChecker_v2 c = new NLLogicChecker_v2 {
      L<S> entities;
      
      bool checkExpression(Exp e, Matching m) {
        new Matches mm;
        if (e cast Func) {
          // FUNCTION CONDITIONS
          
          if (eq(e.name, "verbPhraseFromThirdPerson"))
            ret nlLogic_stringFunction(f ai_verbPhraseFromThirdPerson, e, m.matches);
          else if (eq(e.name, "singular"))
            ret nlLogic_stringFunction(f singular, e, m.matches);
          else if (eq(e.name, 'eval))
            ret eq("true", evalExp(e.arg, m));
          else if (eq(e.name, 'entity)) {
            if (entities == null) {
              long time = sysNow();
              fS switched = switcheroo(s);
              print("Switched >> " + switched);
              entities = evalWithTimeoutOrNull(5000, func -> LS {
                mapMethod('text, ai_extractEntities_v1(switched))
              });
              print("Entities (" + elapsedMS(time) + " ms): " + joinWithComma(entities));
              if (entities == null) entities = new L;
            }
            for (S entity : entities)
              if (matcher.match(nlLogic_text(e.arg), entity, m.matches)) true;
          } else if (startsWith(e.name, "line", mm) && isInteger(mm.rest())) {
            int n = parseInt(mm.rest())-nlLogic_numberOfLinesReferenced(checkingRule->in);
            S line = n == 0 ? s : getString(get(recentHistory, l(recentHistory)+n), 'text);
            print("Recent: " + recentHistory);
            S pat = nlLogic_text(e.arg);
            print("n=" + n + ", Matching " + e + " with " + line);
            ret matcher.match(pat, line, m.matches);
          } else if (eq(e.name, 'unknownIf)) {
            S statement = nlLogic_text(apply(e.arg, m));
            print("Checking statement: " + statement);
            ret !cic(facts, statement) && !cic(facts, "Untrue: " + statement);
          } else if (eq(e.name, 'inputContainsTokens))
            ret call_verbose jcontains(s, nlLogic_text(e.arg));
          else if (eq(e.name, 'inputStartsWith))
            ret call_verbose startsWith(s, nlLogic_text(e.arg));
          else if (eq(e.name, 'followingUpOn)) {
            S text = nlLogic_text(e.arg);
            // e.g. "lhnshnhcklhabvmu with input=$input"
            if (!match("* with input=*", text, mm) || !isDollarVar(mm.get(1))) false;
            S ruleID = mm.unq(0), dollarInput = mm.get(1);
            Map /*prev = last(recentHistory),*/ prevPrev = nextToLast(recentHistory);
            S msgID = getString(prevPrev, 'globalID);
            S input = getString(prevPrev, 'text);
            print("followingUpOn: msgID=" + msgID + ", input=" + input);
            printStruct(+prevPrev);
            //printStruct(+prev);
            S pat = "Rule " + ruleID + " fired on message " + msgID + " ";
            print("pat=" + pat);
            for (S ruleFired : mL("Telegram Rule Fires"))
              if (swic(ruleFired, pat))
                ret print("result=", strictPutIC(m.matches, dollarInput, input));
            false;
          }
        }
        
        if (e cast Eq) {
          // EQUATION CONDITIONS
          
          Exp r = e.right;
          S var = nlLogic_text(e.left);
          if (r cast Func) pcall {
            if (eq(r.name, 'eval))
              ret new NLStringMatcher_singleDollarVar().match(var, evalExp(r.arg, m), m.matches);
          }
        }
        
        if (e instanceof Literal) {
          // TEXT CONDITIONS
          
          S text = nlLogic_text(e);
          if (eq(text, "authorized"))
            ret telegram_amIAuthorized();
        }
        
        ret super.checkExpression(e, m);
      }
    };
    c.matcher = new NLStringMatcher_dollarVars_underscores;
    c.input = s;
    c.facts = mL_facts();
    
    LS unparsedRules = ai_unparsedTalkRules();
    
    for (S listName : mL("Rule & Fact Lists"))
      for (S x : splitAtEmptyLines(mL_raw(listName)))
        if (anyJavaTokens(x))
          if (nlLogic_parseRule(x) != null)
            unparsedRules.add(x);
          else
            c.facts.add(x);

    unparsedRules = sortedByCalculatedField(unparsedRules, func(S s) { jcontains(s, "entity(") });
    L<IfThen> rules = map_pcall nlLogic_parseRule(unparsedRules);
    
    for (IfThen r : rules) {
      continue if r == null;
      Exp out = r.out;
      if (out cast Func)
        if (eq(out.name, "sayIfTrue")) {
          r.out = Func('output, out.arg);
          r.in = And(r.in, Func('fact, out.arg));
          print(r);
        }
    }
    
    fS msgGlobalID = getString(msg, 'globalID);
    if (nempty(msgGlobalID))
      applyNLLogicFacts_v3_onRuleFired.set(voidfunc(IfThen rule, NLLogicChecker_v2.Matching m) {
        if (empty(rule.globalID)) ret;
        S fact = format("Rule * fired on message * at *", rule.globalID, msgGlobalID, localDateWithMilliseconds());
         if (nempty(m.matches))
           fact += " with vars " + dropPrefix("lhm", struct(m.matches));
        addToMechList("Telegram Rule Fires", fact);
      });

    applyNLLogicFacts_v3(c, voidfunc(Exp e) {
      // Rule fired!
      
      if (e cast Func) {
        if (eq(e.name, 'output))
          postMessage(nlLogic_text(e.arg));
        else if (eq(e.name, 'fact)) {
          S fact = nlLogic_text(e.arg);
          if (!contains(mL("Random facts"), fact) && mech_changed(addToMechList("Derived facts", fact)))
            postMessage("Storing fact: " + fact);
        } else if (eq(e.name, 'storeRule)) {
          S rule = nlLogic_text(e.arg);
          if (mech_changed(appendToMechList_noUniq("NL Logic Examples", "\n" + rule)))
            postMessage("Rule stored");
        }
      }
    }, rules);
  }
}




sclass TelegramInitiativeBot > AbstractThinkBot {
  new L<Pair<S, IfThen>> activeTempRules;
  
  void thinkAbout(S s) {
    if (eq(s, "!initiative")) {
      L<IfThen> rules = mL_parsedNLRulesWithoutIDs("Initiative rules");
      for (IfThen rule : shuffledIterator(rules)) {
        Pair<Exp> p = nlLogic_extractFirstCondition(rule.in);
        if (p == null) continue;
        if (nlLogic_isFunc(p.a, 'initiative)) {
          postMessage(nlLogic_text(((Func) p.a).arg));
          IfThen temp = IfThen(p.b, rule.out);
          print("Have temp rule: " + sfu(temp));
          activeTempRules.add(pair(aGlobalID(), temp));
          ret;
        }
      }
    }
    
    applyNLLogicFacts_v2(s, voidfunc(S s) { postMessage(s) },
      new L, activeTempRules);
    activeTempRules.clear();
  }
}

Author comment

Began life as a copy of #1017706

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1017805
Snippet name: Baked Bot Backup 1
Eternal ID of this version: #1017805/1
Text MD5: df9b93e8135970424c693eb93ae01e65
Transpilation MD5: d2ad0af00b1acfb7103941ef9523c9ef
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-14 10:59:20
Source code size: 10249 bytes / 303 lines
Pitched / IR pitched: No / No
Views / Downloads: 638 / 1103
Referenced in: [show references]