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

451
LINES

< > BotCompany Repo | #1008005 // Intelligence Machine [LIVE at ai1.lol/wiki] backup

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

Download Jar. Libraryless. Click here for Pure Java version (10895L/78K).

!7

static double aiCacheInterval = 30;
static new ThreadLocal<Bool> white;
static new ThreadLocal<CookieToConcept> ctc;
static int nComments = 30;
static int nRelations = 5;

sS searchQueriesUniverse = "crefwbbhvngkmicy";

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 {
  db();
  //loadAIConcepts_alwaysFromDisk = true;
  aiConceptsMap_fastLoad = loadTruth_fastLoad = true;
  startAIConceptsHere();
  startTruthTableHere();
}

html {
  print("domain: " + domain());
  if (eqic(domain(), "code.ai1.lol"))
    ret hrefresh("http://snippets.tinybrain.de");

  long time = sysNow();
  white.set(false); // !oneIn(10));
  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);
  
  if (eq(uri, "/structure/comments"))
    ret serveText(structure(list(Comment)));

  if (eq(uri, "/structure/concepts"))
    ret serveText(structure(exportableAIConcepts()));

  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 = sortedByFieldDesc(values(map), "touched");
  
  S question = "";
  pcall {
    question = makeQuestion();
    question = toUpper(conceptLanguageToEnglish_xyz_noquote(question));
  }
  
  S search =
      p("Search term, question or statement:", style := "margin-top: 40px")
    + hform(
        htextinput("q", q, autofocus := 1, 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")) + "."
    + 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, 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"),
        targetBlank("http://ai1.lol/1002018/raw", "JOIN SLACK!"),
        style := "font-size: 40px")
    + "<br>"
    + hsmall(b(ahref("http://ai1.lol/", "THE MACHINE")
    + " " + ahref(progLink(#1007674), "understands Fibonacci numbers") + ", " + ahref(progLink(#1007649), "letters to mom")
    + " and " + ahref(progLink(#1007814), "Scandinavian breakfast") + " :-)") + "<br>" + line)
    + "<br><br>"
    + "We're working on reducing the insanity. " + ahref("mailto:info@ai1.lol", "Info.")
    + " " + ahref("https://www.meetup.com/Artificial-Intelligence-Meetup/", "Hamburg.")
    + " " + ahref("https://slides.com/stefanreich/magnus_carlsens_breakfast", "Presentation.")
    + "<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(dynamize(question))
      + 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) ret "Concept not found: " + toLower(uri);

  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));
    
  /*
  Map<S, Node> nodes = allNodes();
  html += h3("Add forward relation") + hform(
    hhidden(action := "addRelation")
  + hhidden(concept1 := c.globalID)
  + htableRaw_valignTop(ll(
    ll("Relation Concept", hselectNode(map, "pggfeqzkqnezzfto", name := "rel")),
    ll("Concept 2", hselectNode(nodes, name := "concept2", allowEmpty := true)),
    ll("", hsubmit("Create relation"))
  )));
  */

  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 = params.get("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(#1007609), "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() {
  new L l;
  for (AIConcept c : aiConcepts()) {
    Concept cc = unlisted(Concept);
    cc.className = "AIConcept";
    cc.created = c.created;
    copyConceptFields(c, cc, "globalID", "name", "comment");
    l.add(cc);
  }
  ret l;
}

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

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

!include #1007812 // Question Maker

Author comment

Began life as a copy of #1007510

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1008005
Snippet name: Intelligence Machine [LIVE at ai1.lol/wiki] backup
Eternal ID of this version: #1008005/1
Text MD5: 33012230d23d2656ad90d6e347af717d
Transpilation MD5: 80ba05c37fc50f3119242a4ee9070674
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: 2017-04-19 23:21:47
Source code size: 16213 bytes / 451 lines
Pitched / IR pitched: No / No
Views / Downloads: 867 / 1545
Referenced in: [show references]