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

511
LINES

< > BotCompany Repo | #1007510 // Intelligence Machine [no longer live]

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

Download Jar. Libraryless. Click here for Pure Java version (18284L/135K).

!7

set flag TripleAI.
set flag LispAI.

static double aiCacheInterval = 10*60 /*30*/; // 10 minutes
static new ThreadLocal<Bool> white;
static new ThreadLocal<CookieToConcept> ctc;
static int nComments = 30;
static int nRelations = 5;

sS searchQueriesUniverse = "crefwbbhvngkmicy";

static TimedCache<S> news = new TimedCache(60);

concept Search {
  S q, ip;
}

concept CookieToConcept {
  S cookie, concept;
  
  *() {
    if (!_loading()) concept = aGlobalID();
  }
}

concept UserTruth {
  S user, text;
}

concept Comment {
  S commentID, conceptID, user, text;
  
  *() {
    if (!_loading()) commentID = aGlobalID();
  }
}

p {
  aiConceptsMap_cached_autoClearInterval = aiCacheInterval;
  db();
  indexConceptField(CookieToConcept, 'cookie);
  //loadAIConcepts_alwaysFromDisk = true;
  aiConceptsMap_fastLoad = loadTruth_fastLoad = true;
  setOpt(startAIConceptsHere(), grabImages := true);
  startTruthTableHere();
}

html {
  // check host
  
  long time = sysNow();
  white.set(false); // !oneIn(10));
  S cookie = cookieFromUser();
  CookieToConcept ctc = cookieToConcept();

  if (nempty(params.get("news"))) news.clear();
  
  if (eq(uri, "/structure/comments"))
    ret serveText(structure(list(Comment)));

  if (eq(uri, "/structure/concepts"))
    ret serveText(structure(exportableAIConcepts()));
    
  new Matches m;
  
  if (startsWith(uri, "/structure/conceptsByTokens/", m))
    ret serveText(structure(exportableAIConcepts(aiConceptsByTokens(urldecode($1)))));
    
  if (startsWith(uri, "/structure/", m)) {
    if (possibleGlobalID($1)) {
      AIConcept c = getAIConcept($1);
      if (c != null)
        ret serveText(structure(exportableConcept(c))); 
    }
    ret "";
  }

  if (eq(uri, "/structure/statements"))
    ret serveText(structure(loadTruth_cached()));

  S s = hmobilefix() +
    hcommentSafe("Cookie: " + cookie
      + (ctc != null ? ", concept: " + ctc.concept : ""))
    + (white! ? "" : hcss("a:link, a:visited, a:hover, a:active { color: white; }"))
    + hbody(htmlImpl(uri, params), style := white!
      ? "background: white"
      : "text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; color: white; background-color: #262626");
      // : "text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; color: white; background-image: url(\"" + snippetImageGIFURL(#1007679) + "\"); background-repeat: no-repeat; background-position: 50% 0; background-color: #262626");
      // : "text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; color: white; background-image: url(\"" + snippetImageGIFURL(#1007907) + "\"); background-repeat: no-repeat; background-position: 50% 0; background-color: #141414");
  ret s + p(hsmall((sysNow()-time) + " ms", style := "color: #EEEEEE"), style := "text-align: right");
}

static O htmlImpl(S uri, final SS params) {
  print("Wiki URI: " + uri);
  S vis = registerVisitor();
  uri = dropSuffix("/", uri);
  uri = dropPrefix("/", uri);
  S userConcept = userConcept();
  
  processActions(params);
  
  if (eq(uri, "sleep")) {
    sleepSeconds(5);
    ret "Slept 5, just for you.";
  }
  
  S q = trim(params.get("q"));
  S qname = q, qcomment = "";
  new Matches m;
  if (flexMatch("* [*]", q, m)) {
    qname = $1;
    qcomment = $2;
  }
  if (nempty(q)) {
    cset(uniq(Search, +q, ip := getClientIP()), lastSeen := now());
    if (findAIConceptByNameAndCommentIC(qname, qcomment) == null) {
      S universe = or2(qname.startsWith("->") ? userConcept : null, searchQueriesUniverse);
      createAIConcept(qname, qcomment, universe);
      //if (!loadAIConcepts_alwaysFromDisk) clearAICaches();
    }
  }
    
  if (eq(uri, "import")) {
    triggerStandardAIImport();
    ret "OK" + hrefresh(1, "/wiki/");
  }
  
  if (eq(uri, "export")) {
    triggerStandardAIExport();
    ret "OK" + hrefresh(1, "/wiki/");
  }
  
  if (eq(uri, "searches")) {
    ret htitle_h2("Latest Searches") 
      + htable(map(func(Search s) { litorderedmap(Query := dynamize(s.q),
      IP := dynamize(s.ip)) }, takeFirst(10, sortedByFieldDesc("lastSeen", list(Search)))), false);
  }
  
  clearAICaches(aiCacheInterval);
  final Map<S, AIConcept> map = aiConceptsMap_cached();
    
  if (possibleGlobalID(uri))
    ret renderConcept(uri, params);
  
  // Serve main page

  S results = "";
    
  if (nempty(q)) {
    L<AIConcept> l = imSearch(q);
    if (empty(l))
      results = h3("No search results for " + htmlencode(quote(q)));
    else
      results = h3("Search results for " + htmlencode(quote(q)))
        + ul(map(f aiConceptToHTML, l));
  }
  
  L<AIConcept> l = withoutArrowConcepts(sortedByFieldDesc(values(map), "touched"));
  
  /*S question = "";
  pcall {
    question = makeQuestion();
    question = toUpper(conceptLanguageToEnglish_xyz_noquote(question));
  }*/
  bool didAddComment = eq("addComment", params.get("action"));
  S randomConcept = "";
  /*pcall {
    S id = randomNonTechnicalConcept();
    randomConcept =
      hform(
          hhidden(action := "addComment")
        + hhidden(conceptID := id)
        + "What do you think about "
        + b(dynamize("\"" + conceptToName(id) + "\""))
        + "? "
        + hinputfield("text", "", autofocus := didAddComment ? "1" : null, style := "text-align: center") + " " + hsubmit("Submit spontaneous comment"));
  }*/
  
  S search =
      h3("Search")
    + p("Search term, question or statement:")
    + hform(
        htextinput("q", q, /*autofocus := !didAddComment ? "1" : null,*/ style := "text-align: center", onfocus := "this.select()") + " "
      + hsubmit("Go"),
      "accept-charset" := "UTF-8");
    
  S auth = "";
  if (webAuthed())
    auth = " " + ahref("/wiki/import", "Import.")
      + " " + getExportSnippetID(aiConceptsProgram()) + "/"
      + getExportSnippetID(truthTableProgram()) + ". "
      + ahref("/wiki/export", "Export.");
    
  S line = "See " + ahref("https://www.youtube.com/watch?v=gTlgZkc95IU", "Video") + ". " + ahref("http://ai1.lol/new", htmlencode("More videos & articles")) + ". " + ahref("http://tinybrain.de/x30.jar", "Download.")
    + auth;
    
  bool all = nempty(params.get("all"));
  L<AIConcept> conceptsToShow = all ? l : selectRandomUpTo(l, 20);
  S concepts = ul(map(f aiConceptToHTML, conceptsToShow));

  L<Statement> allStatements = reversed(loadTruth_cached());
  bool allRelations = nempty(params.get("allrelations"));
  L<Statement> statements = allRelations ? allStatements : takeFirst(nRelations, allStatements); 
  S relations = ul(map(f statementToHTML, statements));
  
  bool showAllComments = nempty(params.get("allcomments"));
  L<Comment> allComments = sortedByFieldDesc(list(Comment), "created");
  L<Comment> lcomments = showAllComments ? allComments : takeFirst(nComments, allComments); 
  S comments = ul(map(f commentToHTML2, lcomments));
  
  BigInteger[] ints = extractIntsFromString(vis);
  BigInteger today = getBigInt(ints, 0), yesterday = getBigInt(ints, 2);
  S load = formatDoubleFull(systemLoad(), 1);
  S drive = iceil(userDirDriveFullness()) + "%";
  S s = "";
  if (nempty(userConcept))
    s = "You are: " + dynamize(userConcept) + " / ";
  s += today + " : " + yesterday + " / " + load + " / " + drive;
  S exp = "Visitors today: " + today + ", yesterday: " + yesterday + ", system load: " + load + ", drive use: " + drive;
  S visBox = hdiv(hsmall(s), style := "position:absolute; top: 2px; right: 2px", title := exp);

  S latest = "";
  int nLatest = parseIntOpt(params.get("latest"));
  if (nLatest != 0) nLatest = min(nLatest, 1000); else nLatest = 3;
  if (!all) {
    L<AIConcept> ll = takeFirst(nLatest, withoutArrowConcepts(sortedByFieldDesc(values(map), "created")));
    latest = h3("Latest concepts " + (nLatest < 100 ? ahref("?latest=100", "(more)") : "")) 
      + ul(map(f aiConceptToHTML, ll));
  }
  
  long nc = numComputers();
  S slogan = p(
    // span(l(l) + " Concepts", style := "font-size: 40px") 
      span(
        //targetBlank("https://www.twitch.tv/stefan_reich", "LET'S TWITCH.")
        //targetBlank("http://all-computers.tinybrain.de", (nc == 0 ? "MANY" : str(nc)) + " COMPUTERS"),
        //+ "<br>" + targetBlank("http://ai1.lol/", "LET'S CHAT!"
        targetBlank("http://ai1.lol/", himg(snippetImgLink(#1009213), width := 394, height := 74, title := "BotCompany.de")),
        style := "font-size: 40px")
    + "<br>"
    + hsmall(b(ahref("http://ai1.lol/wiki/", "THE MACHINE")
    + " " + ahref(progLink(#1007674), "understands Fibonacci numbers") + ", " + ahref(progLink(#1007649), "letters to mom")
    + ", " + ahref(progLink(#1007814), "Scandinavian breakfast")
    + " and " + ahref(progLink(#1008300), "products")
    + " :-)") + "<br>" + line)
    + "<br><br>"
    + "We're working on reducing the insanity. " + ahref("mailto:info@ai1.lol", "Mail.")
    + " " + ahref("https://www.meetup.com/Artificial-Intelligence-Meetup/", "Hamburg.")
    + " " + ahref("https://slides.com/stefanreich", "Presentations (German/English).")
    + "<br>"
    + "Currently working on " + targetBlank(progLink(#1007924), "reading Wikipedia") + " &amp; " + targetBlank(progLink(#1007987), "general super-parsing") + "."
    // + b(ahref("http://tinybrain.de/1007649", "Demo 1.") + " "
    // + ahref("http://tinybrain.de/1007674", "Demo 2."))
  );
    
  ret htitle("Intelligence Machine")
    + visBox
    + hdiv(visitorsChart(""), style := "position:absolute; left: 2px; right: 2px; width: 150px; height: 110px;", title := "Visitors Chart")
    + hcenter2(
        (white! ? "" : slogan)
      + (white! ? p(ahref("http://ai1.lol/", hTBImage(white! ? #1007526 : #1002549, "title", "Intelligence Machine"))) : "")
      + (white! ? slogan : "")
      //+ p("It seems we're moving towards actual applications. New contacts!")
      + news.get(func -> S { loadSnippet(#1008166) })
      + youtubeEmbedSideBySide("KXPLUaaVPIw", /*"wuz8yhnN9fs",*/ "UVMiYHeotFE")
      
      //+ p(htbimg(#1008147))
      //+ p(htbimg(#1008146))
      
      // + p(dynamize(question))
      + randomConcept
      + search)
    + results
    //+ hcenter2(latestDrawing() + randomDrawing())
    + "<br>"
    + htableRaw_valignTop(ll(ll(
      latest +
      hdiv(_defaultAd(), style := "float: left; margin-right: 30px; margin-bottom: 20px") +
      h3(
        (all ? "Concepts (" + lstr(l) 
        : "Random concepts (" + l(conceptsToShow) + " of " + ahref("?all=1", l(l)))
        + ")") + concepts, 
      //hdiv(_defaultAd(), style := "float: right; margin: 10px") +
      h3(showAllComments ? "All Comments (" + l(lcomments) + ")"
        : "Latest Comments (" + l(lcomments) + " of " + ahref("?allcomments=1", l(allComments)) + ") - YOU can comment too")
        + comments
    + h3(allRelations
        ? "All Relations (" + l(statements) + ")"
        : "Latest Relations (" + l(statements) + " of " + ahref("?allrelations=1", l(allStatements)) + ") - We should clean them up some time ^^")
        + relations)), "border", "0")
    + hcenter2(htableRaw_valignTop(ll(ll(latestDrawing(), randomDrawing())), "cellspacing", 20))
    ;
}

static S aiConceptToHTML(AIConcept c) {
  ret (nempty(c.imageMD5) ? "[w/image] " :
    hasSuggestedImage(c) ? "[w/sugg. image] " : "") +
    dynamize(c.globalID) + " - " + dynamize(aiConceptToString2(c));
}

static S statementToHTML(Statement s) {
  ret ahref("http://ai1.lol/" + s.globalID, "*") + " " + dynamize(or2(s.possibleEnglishTranslation, "?") + " - " + s.text);
}

static S commentToHTML(Comment c) {
  ret hsafeComment(c.commentID) + dynamize(c.user + " says: " + c.text);
}

// for start page
static S commentToHTML2(Comment c) {
  ret ahref(intelligenceMachineLink(c.conceptID), dynamize(conceptToName(c.conceptID)))
  + dynamize(
    //c.user + " says on " + c.conceptID + ": " + c.text
    ": " + c.text
  );
}

sS renderConcept(S uri, SS params) {
  // Serve concept
  
  final Map<S, AIConcept> map = aiConceptsMap_cached();
  final AIConcept c = map.get(toLower(uri));
  Statement st = findWhere(loadTruth_cached(), globalID := toLower(uri));
  
  if (st != null) ret renderStatement(c, st, params);
  if (c == null) {
    pcall-short {
      loadPage_extraHeaders.set(litmap("X-Forwarded-For", clientIP()));
      ret loadPage(smartBotURL() + "/" + toLower(uri));
    }
    ret "[Smart Bot Reloading]";
  }

  S html = htitle(c.name + " [" + 
    (nempty(c.comment) ? c.comment + " / " : "") + c.globalID + "]")
    + h2(dynamize(aiConceptToString(c)));
  
  html += ul(
    "Universe: " + dynamize(c.originatingUniverse),
    eq(c.originatingUniverse, userConcept()) ? "You made this" : null,
    //"Image MD5: " + htmlencode(c.imageMD5)
    "Created: " + formatDateAndTime(c.created),
    "Touched: " + formatDateAndTime(c.touched),
    c.imported != 0 || nempty(c.importedFrom) ? "Imported: " + formatDateAndTime(c.imported) + " from " + dynamize(c.importedFrom) : null
  );
  
  if (nempty(c.imageMD5) && imageServerHas(c.imageMD5))
    html += p(himg(imageServerJPEGLink(c.imageMD5)));
    
  if (c.suggestedImage.has()) {
    print("PNG path: " + c.suggestedImage->pngPath);
    BufferedImage img = c.suggestedImage->getImage();
    if (img != null) {
      uploadToImageServerIfNotThere(img, aiConceptToString(c));
      if (empty(c.suggestedImageMD5))
        c.suggestedImageMD5 = imageMD5(img);
    }
  }
  
  if (nempty(c.suggestedImageMD5))
    html += h3("Suggested image (from Google search)") + p(himg(imageServerJPEGLink(c.suggestedImageMD5));

  L<Statement> statements = [Statement s : loadTruth_cached() | containsIgnoreCase(s.text, c.globalID)];
  if (nempty(statements))
    html += h3("Statements about this") + ul(map(f statementToHTML, statements));
    
  L<Comment> comments = conceptsWhere(Comment, conceptID := c.globalID);
  html += h3("Comments") + ul(map(f commentToHTML, comments));

  html += h3("Add comment") + hform(
    hhidden(action := "addComment")
  + hhidden(conceptID := c.globalID)
  + htableRaw_valignTop(ll(
    ll("Your Comment", hinputfield("text")),
    ll("", hsubmit("Add comment"))
  )));
  
  html += p(ahref("http://ai1.lol/wiki", "[Home]"));
  
  ret html;
}

sS hselectNode(Map<S, ? extends Node> map, O... params) {
  ret hselect(orderMapByValue(mapValues(f nodeToString, map)), params);
}

sS renderStatement(AIConcept c, Statement st, SS params) {
  if (nempty(params.get("retranslate"))) {
    RemoteDB db = new RemoteDB(truthTableProgram());
    db.xset(db.rc(st.id), possibleEnglishTranslation := null);
    db.close();
    st.possibleEnglishTranslation = null;
  }
  
  bool hasTranslation = nempty(st.possibleEnglishTranslation);
  S html = htitle_h2("Statement " + st.globalID)
    + ul(
      "English (machine-generated): " + dynamize(or2(st.possibleEnglishTranslation, "?")),
      "Computer Language: " + dynamize(st.text),
      st.imported != 0 || nempty(st.importedFrom) ? "Imported: " + formatDateAndTime(st.imported) + " from " + dynamize(st.importedFrom) : null)
    + (!hasTranslation ? "" : p(ahref("?retranslate=1", "Request retranslation")));
  html += p(ahref("http://ai1.lol/wiki", "[Home]"));
  ret html;
}

svoid processActions(SS params) {
  S action = params.get("action");
  
  if (eq(action, "addRelation")) {
    S id1 = params.get("concept1");
    S id2 = params.get("concept2");
    S rel = params.get("rel");
    if (nempty(id1) && nempty(rel))
      createStatement(conceptQuote(rel) + " " + conceptQuote(id1) + " " + conceptQuoteOpt(id2));
  }
  
  if (eq(action, "addComment")) {
    S conceptID = params.get("conceptID");
    S text = trim(params.get("text"));
    if (possibleGlobalID(conceptID) && nempty(text)) {
      S user = ctc->concept;
      uniq(Comment, +user, +conceptID, +text);
    }
  }
}

static S latestDrawing() {
  L metas = getDrawings();
  ret empty(metas) ? "" : 
      h3("Latest Drawing")
    + renderDrawing(first(metas));
}

sS renderDrawing(O meta) {
  ret p(renderMeta(meta) + "<br>"
    + hsmall(ahref(progLink(#1009961), "Make your own.")
    + " " + ahref("http://ai1.lol/images", "More drawings.")));
}

static S randomDrawing() {
  O meta = random(dropFirst(getDrawings()));
  if (meta == null) ret "";
  ret h3("Random Drawing")
    + renderDrawing(meta);
}

static L getDrawings() {
  pcall {
    O imageServer = getBot(#1004590);
    L metas = cast call(get(imageServer, "mainConcepts"), "list", "ImageMeta");
    metas = [O meta : metas | startsWithIC(getString(meta, "name"), "Drawing ")];
    metas = sortedByFieldDesc(metas, "created");
    ret metas;
  }
  ret ll();
}

static S renderMeta(O meta) {
  S md5 = getString(meta, "md5");
  S jpg = imageServerJPEGLink(md5);
  ret ahref(jpg, himg(jpg,
    //style := "zoom: 50%" // works only on Chrome
    onload := "this.width *= 0.7; this.onload=null;"
  ));
}

static L exportableAIConcepts(Collection<AIConcept> l) {
  ret map(f exportableConcept, l);
}

static L exportableAIConcepts() {
  ret exportableAIConcepts(aiConcepts());
}

static Concept exportableConcept(AIConcept c) {
  Concept cc = unlisted(Concept);
  cc.className = "AIConcept";
  cc.created = c.created;
  copyConceptFields(c, cc, "globalID", "name");
  if (nempty(c.comment)) copyConceptFields(c, cc, "comment");
  ret cc;
}

static S userConcept() {
  ret ctc! != null ? ctc->concept : "";
}

static long numComputers() {
  ret getLong(getBot(#1003253), "numComputers");
}

static CookieToConcept cookieToConcept() {
  S cookie = cookieFromUser();
  CookieToConcept ctc = null;
  if (nempty(cookie)) {
    ctc = uniq(CookieToConcept, +cookie);
    if (!hasAIConcept(ctc.concept))
      createAIConcept_listed(ctc.concept, "-> an anonymous cookie", "created automatically", searchQueriesUniverse);
  }
  main.ctc.set(ctc);
  ret ctc;
}

static S conceptIDForCookie() {
  CookieToConcept ctc = cookieToConcept();
  ret ctc == null ? null : ctc.concept;
}

!include #1007812 // Question Maker

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007510
Snippet name: Intelligence Machine [no longer live]
Eternal ID of this version: #1007510/387
Text MD5: 3caefdf40c9c6af1bccc3f51fb620a6f
Transpilation MD5: b497dae66ae059bf100c5ff84b2b34f9
Author: stefan
Category: javax / a.i. / html
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-07-02 15:04:45
Source code size: 18299 bytes / 511 lines
Pitched / IR pitched: No / No
Views / Downloads: 2178 / 7334
Version history: 386 change(s)
Referenced in: #1002576 - Eleu Web Serving (Include)
#1007570 - Truth Table from jtubtzbbkimh
#1008005 - Intelligence Machine [LIVE at ai1.lol/wiki] backup
#1009219 - cookieConcept
#1011258 - Thin a program's backups (-> thinAProgramsBackups)
#1013896 - Eleutheria Main for butter.botcompany.de + Stefan's OS (LIVE)
#1024026 - Eleutheria Main for butter.botcompany.de (backup without Stefan's OS)
#1029953 - Eleu Web Serving (transpilation test)