Warning: session_start(): open(/var/lib/php/sessions/sess_r6knhpc1qchkp45he3ta2vfsdu, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!7
sS mechLibID = #1400124;
static O onOutput; // VF1
static L bots;
abstract sclass AbstractThinkBot {
transient VF1 super S> postMessage;
transient new L recentHistory;
abstract void thinkAbout(S input);
void postMessage(S s) {
callF(postMessage, s);
}
}
p {
mech_useLibrary(mechLibID);
L> classes = botClasses();
print("Have think classes: " + classNames(classes));
makeBots();
bot();
}
static L> botClasses() {
ret myNonAbstractClassesImplementing(AbstractThinkBot);
}
svoid makeBots() {
bots = map nuWithoutArguments(botClasses());
}
sS answer(S s) {
for (AbstractThinkBot bot : bots) pcall {
bot.postMessage = vfPrint();
temp tempSetThreadLocal(telegram_msg_tl(), new Map);
bot.thinkAbout(s);
}
ret " ";
}
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 (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)) {
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) {
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(javaEval(code));
print("Result: " + shorten(result, 100));
ret result;
}
void thinkAbout(fS s) {
NLLogicChecker_v2 c = new NLLogicChecker_v2 {
L entities;
bool checkExpression(Exp e, Matching m) {
new Matches mm;
if (e cast Func) {
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));
else if (eq(e.name, 'entity)) {
if (entities == null) {
long time = sysNow();
S switched = switcheroo(s);
print("Switched >> " + switched);
entities = mapMethod('text, ai_extractEntities_v1(switched));
print("Entities (" + elapsedMS(time) + " ms): " + joinWithComma(entities));
}
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);
}
}
if (e cast Eq) {
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.matches);
}
}
ret super.checkExpression(e, m);
}
};
c.matcher = new NLStringMatcher_dollarVars_underscores;
c.input = s;
c.facts = mL_facts();
L unparsedRules = combineWithGlobalIDs(ai_unparsedTalkRules());
unparsedRules = sortedByCalculatedField(unparsedRules, func(Pair p) { jcontains(p.b, "entity(") });
L> rules = mapPairB_pcall nlLogic_parseRule(unparsedRules);
for (Pair p : rules) {
IfThen r = p.b;
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);
}
}
applyNLLogicFacts_v3(c, voidfunc(Exp e) {
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);
}
}
}, rules);
}
}
sclass TelegramInitiativeBot > AbstractThinkBot {
new L> activeTempRules;
void thinkAbout(S s) {
if (eq(s, "!initiative")) {
L rules = mL_parsedNLRulesWithoutIDs("Initiative rules");
for (IfThen rule : shuffledIterator(rules)) {
Pair 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();
}
}