Download Jar. Libraryless. Click here for Pure Java version (10895L/78K).
1 | !7 |
2 | |
3 | static double aiCacheInterval = 30; |
4 | static new ThreadLocal<Bool> white; |
5 | static new ThreadLocal<CookieToConcept> ctc; |
6 | static int nComments = 30; |
7 | static int nRelations = 5; |
8 | |
9 | sS searchQueriesUniverse = "crefwbbhvngkmicy"; |
10 | |
11 | concept Search { |
12 | S q, ip; |
13 | } |
14 | |
15 | concept CookieToConcept { |
16 | S cookie, concept; |
17 | |
18 | *() { |
19 | if (!_loading()) concept = aGlobalID(); |
20 | } |
21 | } |
22 | |
23 | concept UserTruth { |
24 | S user, text; |
25 | } |
26 | |
27 | concept Comment { |
28 | S commentID, conceptID, user, text; |
29 | |
30 | *() { |
31 | if (!_loading()) commentID = aGlobalID(); |
32 | } |
33 | } |
34 | |
35 | p { |
36 | db(); |
37 | //loadAIConcepts_alwaysFromDisk = true; |
38 | aiConceptsMap_fastLoad = loadTruth_fastLoad = true; |
39 | startAIConceptsHere(); |
40 | startTruthTableHere(); |
41 | } |
42 | |
43 | html { |
44 | print("domain: " + domain()); |
45 | if (eqic(domain(), "code.ai1.lol")) |
46 | ret hrefresh("http://snippets.tinybrain.de"); |
47 | |
48 | long time = sysNow(); |
49 | white.set(false); // !oneIn(10)); |
50 | S cookie = cookieFromUser(); |
51 | CookieToConcept ctc = null; |
52 | if (nempty(cookie)) { |
53 | ctc = uniq(CookieToConcept, +cookie); |
54 | if (!hasAIConcept(ctc.concept)) |
55 | createAIConcept_listed(ctc.concept, "-> an anonymous cookie", "created automatically", searchQueriesUniverse); |
56 | } |
57 | |
58 | main.ctc.set(ctc); |
59 | |
60 | if (eq(uri, "/structure/comments")) |
61 | ret serveText(structure(list(Comment))); |
62 | |
63 | if (eq(uri, "/structure/concepts")) |
64 | ret serveText(structure(exportableAIConcepts())); |
65 | |
66 | S s = hmobilefix() + |
67 | hcommentSafe("Cookie: " + cookie |
68 | + (ctc != null ? ", concept: " + ctc.concept : "")) |
69 | + (white! ? "" : hcss("a:link, a:visited, a:hover, a:active { color: white; }")) |
70 | + hbody(htmlImpl(uri, params), style := white! |
71 | ? "background: white" |
72 | : "text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; color: white; background-color: #262626"); |
73 | // : "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"); |
74 | // : "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"); |
75 | ret s + p(hsmall((sysNow()-time) + " ms", style := "color: #EEEEEE"), style := "text-align: right"); |
76 | } |
77 | |
78 | static O htmlImpl(S uri, final SS params) { |
79 | print("Wiki URI: " + uri); |
80 | S vis = registerVisitor(); |
81 | uri = dropSuffix("/", uri); |
82 | uri = dropPrefix("/", uri); |
83 | S userConcept = userConcept(); |
84 | |
85 | processActions(params); |
86 | |
87 | if (eq(uri, "sleep")) { |
88 | sleepSeconds(5); |
89 | ret "Slept 5, just for you."; |
90 | } |
91 | |
92 | S q = trim(params.get("q")); |
93 | S qname = q, qcomment = ""; |
94 | new Matches m; |
95 | if (flexMatch("* [*]", q, m)) { |
96 | qname = $1; |
97 | qcomment = $2; |
98 | } |
99 | if (nempty(q)) { |
100 | cset(uniq(Search, +q, ip := getClientIP()), lastSeen := now()); |
101 | if (findAIConceptByNameAndCommentIC(qname, qcomment) == null) { |
102 | S universe = or2(qname.startsWith("->") ? userConcept : null, searchQueriesUniverse); |
103 | createAIConcept(qname, qcomment, universe); |
104 | //if (!loadAIConcepts_alwaysFromDisk) clearAICaches(); |
105 | } |
106 | } |
107 | |
108 | if (eq(uri, "import")) { |
109 | triggerStandardAIImport(); |
110 | ret "OK" + hrefresh(1, "/wiki/"); |
111 | } |
112 | |
113 | if (eq(uri, "export")) { |
114 | triggerStandardAIExport(); |
115 | ret "OK" + hrefresh(1, "/wiki/"); |
116 | } |
117 | |
118 | if (eq(uri, "searches")) { |
119 | ret htitle_h2("Latest Searches") |
120 | + htable(map(func(Search s) { litorderedmap(Query := dynamize(s.q), |
121 | IP := dynamize(s.ip)) }, takeFirst(10, sortedByFieldDesc("lastSeen", list(Search)))), false); |
122 | } |
123 | |
124 | clearAICaches(aiCacheInterval); |
125 | final Map<S, AIConcept> map = aiConceptsMap_cached(); |
126 | |
127 | if (possibleGlobalID(uri)) |
128 | ret renderConcept(uri, params); |
129 | |
130 | // Serve main page |
131 | |
132 | S results = ""; |
133 | |
134 | if (nempty(q)) { |
135 | L<AIConcept> l = imSearch(q); |
136 | if (empty(l)) |
137 | results = h3("No search results for " + htmlencode(quote(q))); |
138 | else |
139 | results = h3("Search results for " + htmlencode(quote(q))) |
140 | + ul(map(f aiConceptToHTML, l)); |
141 | } |
142 | |
143 | L<AIConcept> l = sortedByFieldDesc(values(map), "touched"); |
144 | |
145 | S question = ""; |
146 | pcall { |
147 | question = makeQuestion(); |
148 | question = toUpper(conceptLanguageToEnglish_xyz_noquote(question)); |
149 | } |
150 | |
151 | S search = |
152 | p("Search term, question or statement:", style := "margin-top: 40px") |
153 | + hform( |
154 | htextinput("q", q, autofocus := 1, style := "text-align: center", onfocus := "this.select()") + " " |
155 | + hsubmit("Go"), |
156 | "accept-charset" := "UTF-8"); |
157 | |
158 | S auth = ""; |
159 | if (webAuthed()) |
160 | auth = " " + ahref("/wiki/import", "Import.") |
161 | + " " + getExportSnippetID(aiConceptsProgram()) + "/" |
162 | + getExportSnippetID(truthTableProgram()) + ". " |
163 | + ahref("/wiki/export", "Export."); |
164 | |
165 | S line = "See " + ahref("https://www.youtube.com/watch?v=gTlgZkc95IU", "Video") + ". " + ahref("http://ai1.lol/new", htmlencode("More videos & articles")) + "." |
166 | + auth; |
167 | |
168 | bool all = nempty(params.get("all")); |
169 | L<AIConcept> conceptsToShow = all ? l : selectRandomUpTo(l, 20); |
170 | S concepts = ul(map(f aiConceptToHTML, conceptsToShow)); |
171 | |
172 | L<Statement> allStatements = reversed(loadTruth_cached()); |
173 | bool allRelations = nempty(params.get("allrelations")); |
174 | L<Statement> statements = allRelations ? allStatements : takeFirst(nRelations, allStatements); |
175 | S relations = ul(map(f statementToHTML, statements)); |
176 | |
177 | bool showAllComments = nempty(params.get("allcomments")); |
178 | L<Comment> allComments = sortedByFieldDesc(list(Comment), "created"); |
179 | L<Comment> lcomments = showAllComments ? allComments : takeFirst(nComments, allComments); |
180 | S comments = ul(map(f commentToHTML2, lcomments)); |
181 | |
182 | BigInteger[] ints = extractIntsFromString(vis); |
183 | BigInteger today = getBigInt(ints, 0), yesterday = getBigInt(ints, 2); |
184 | S load = formatDoubleFull(systemLoad(), 1); |
185 | S drive = iceil(userDirDriveFullness()) + "%"; |
186 | S s = ""; |
187 | if (nempty(userConcept)) |
188 | s = "You are: " + dynamize(userConcept) + " / "; |
189 | s += today + " : " + yesterday + " / " + load + " / " + drive; |
190 | S exp = "Visitors today: " + today + ", yesterday: " + yesterday + ", system load: " + load + ", drive use: " + drive; |
191 | S visBox = hdiv(hsmall(s), style := "position:absolute; top: 2px; right: 2px", title := exp); |
192 | |
193 | S latest = ""; |
194 | int nLatest = parseIntOpt(params.get("latest")); |
195 | if (nLatest != 0) nLatest = min(nLatest, 1000); else nLatest = 3; |
196 | if (!all) { |
197 | L<AIConcept> ll = takeFirst(nLatest, sortedByFieldDesc(values(map), "created")); |
198 | latest = h3("Latest concepts " + (nLatest < 100 ? ahref("?latest=100", "(more)") : "")) |
199 | + ul(map(f aiConceptToHTML, ll)); |
200 | } |
201 | |
202 | long nc = numComputers(); |
203 | S slogan = p( |
204 | // span(l(l) + " Concepts", style := "font-size: 40px") |
205 | span( |
206 | // targetBlank("https://www.twitch.tv/stefan_reich", "LET'S TWITCH"), |
207 | //targetBlank("http://all-computers.tinybrain.de", (nc == 0 ? "MANY" : str(nc)) + " COMPUTERS"), |
208 | targetBlank("http://ai1.lol/1002018/raw", "JOIN SLACK!"), |
209 | style := "font-size: 40px") |
210 | + "<br>" |
211 | + hsmall(b(ahref("http://ai1.lol/", "THE MACHINE") |
212 | + " " + ahref(progLink(#1007674), "understands Fibonacci numbers") + ", " + ahref(progLink(#1007649), "letters to mom") |
213 | + " and " + ahref(progLink(#1007814), "Scandinavian breakfast") + " :-)") + "<br>" + line) |
214 | + "<br><br>" |
215 | + "We're working on reducing the insanity. " + ahref("mailto:info@ai1.lol", "Info.") |
216 | + " " + ahref("https://www.meetup.com/Artificial-Intelligence-Meetup/", "Hamburg.") |
217 | + " " + ahref("https://slides.com/stefanreich/magnus_carlsens_breakfast", "Presentation.") |
218 | + "<br>" |
219 | + "Currently working on " + targetBlank(progLink(#1007924), "reading Wikipedia") + " & " + targetBlank(progLink(#1007987), "general super-parsing") + "." |
220 | // + b(ahref("http://tinybrain.de/1007649", "Demo 1.") + " " |
221 | // + ahref("http://tinybrain.de/1007674", "Demo 2.")) |
222 | ); |
223 | |
224 | ret htitle("Intelligence Machine") |
225 | + visBox |
226 | + hdiv(visitorsChart(""), style := "position:absolute; left: 2px; right: 2px; width: 150px; height: 110px;", title := "Visitors Chart") |
227 | + hcenter2( |
228 | (white! ? "" : slogan) |
229 | + (white! ? p(ahref("http://ai1.lol/", hTBImage(white! ? #1007526 : #1002549, "title", "Intelligence Machine"))) : "") |
230 | + (white! ? slogan : "") |
231 | + p(dynamize(question)) |
232 | + search) |
233 | + results |
234 | //+ hcenter2(latestDrawing() + randomDrawing()) |
235 | + "<br>" |
236 | + htableRaw_valignTop(ll(ll( |
237 | latest + |
238 | hdiv(_defaultAd(), style := "float: left; margin-right: 30px; margin-bottom: 20px") + |
239 | h3( |
240 | (all ? "Concepts (" + lstr(l) |
241 | : "Random concepts (" + l(conceptsToShow) + " of " + ahref("?all=1", l(l))) |
242 | + ")") + concepts, |
243 | //hdiv(_defaultAd(), style := "float: right; margin: 10px") + |
244 | h3(showAllComments ? "All Comments (" + l(lcomments) + ")" |
245 | : "Latest Comments (" + l(lcomments) + " of " + ahref("?allcomments=1", l(allComments)) + ") - YOU can comment too") |
246 | + comments |
247 | + h3(allRelations |
248 | ? "All Relations (" + l(statements) + ")" |
249 | : "Latest Relations (" + l(statements) + " of " + ahref("?allrelations=1", l(allStatements)) + ") - We should clean them up some time ^^") |
250 | + relations)), "border", "0") |
251 | + hcenter2(htableRaw_valignTop(ll(ll(latestDrawing(), randomDrawing())), "cellspacing", 20)) |
252 | ; |
253 | } |
254 | |
255 | static S aiConceptToHTML(AIConcept c) { |
256 | ret (nempty(c.imageMD5) ? "[w/image] " : |
257 | hasSuggestedImage(c) ? "[w/sugg. image] " : "") + |
258 | dynamize(c.globalID) + " - " + dynamize(aiConceptToString2(c)); |
259 | } |
260 | |
261 | static S statementToHTML(Statement s) { |
262 | ret ahref("http://ai1.lol/" + s.globalID, "*") + " " + dynamize(or2(s.possibleEnglishTranslation, "?") + " - " + s.text); |
263 | } |
264 | |
265 | static S commentToHTML(Comment c) { |
266 | ret hsafeComment(c.commentID) + dynamize(c.user + " says: " + c.text); |
267 | } |
268 | |
269 | // for start page |
270 | static S commentToHTML2(Comment c) { |
271 | ret ahref(intelligenceMachineLink(c.conceptID), dynamize(conceptToName(c.conceptID))) |
272 | + dynamize( |
273 | //c.user + " says on " + c.conceptID + ": " + c.text |
274 | ": " + c.text |
275 | ); |
276 | } |
277 | |
278 | sS renderConcept(S uri, SS params) { |
279 | // Serve concept |
280 | |
281 | final Map<S, AIConcept> map = aiConceptsMap_cached(); |
282 | final AIConcept c = map.get(toLower(uri)); |
283 | Statement st = findWhere(loadTruth_cached(), globalID := toLower(uri)); |
284 | |
285 | if (st != null) ret renderStatement(c, st, params); |
286 | if (c == null) ret "Concept not found: " + toLower(uri); |
287 | |
288 | S html = htitle(c.name + " [" + |
289 | (nempty(c.comment) ? c.comment + " / " : "") + c.globalID + "]") |
290 | + h2(dynamize(aiConceptToString(c))); |
291 | |
292 | html += ul( |
293 | "Universe: " + dynamize(c.originatingUniverse), |
294 | eq(c.originatingUniverse, userConcept()) ? "You made this" : null, |
295 | //"Image MD5: " + htmlencode(c.imageMD5) |
296 | "Created: " + formatDateAndTime(c.created), |
297 | "Touched: " + formatDateAndTime(c.touched), |
298 | c.imported != 0 || nempty(c.importedFrom) ? "Imported: " + formatDateAndTime(c.imported) + " from " + dynamize(c.importedFrom) : null |
299 | ); |
300 | |
301 | if (nempty(c.imageMD5) && imageServerHas(c.imageMD5)) |
302 | html += p(himg(imageServerJPEGLink(c.imageMD5))); |
303 | |
304 | if (c.suggestedImage.has()) { |
305 | print("PNG path: " + c.suggestedImage->pngPath); |
306 | BufferedImage img = c.suggestedImage->getImage(); |
307 | if (img != null) { |
308 | uploadToImageServerIfNotThere(img, aiConceptToString(c)); |
309 | if (empty(c.suggestedImageMD5)) |
310 | c.suggestedImageMD5 = imageMD5(img); |
311 | } |
312 | } |
313 | |
314 | if (nempty(c.suggestedImageMD5)) |
315 | html += h3("Suggested image (from Google search)") + p(himg(imageServerJPEGLink(c.suggestedImageMD5)); |
316 | |
317 | L<Statement> statements = [Statement s : loadTruth_cached() | containsIgnoreCase(s.text, c.globalID)]; |
318 | if (nempty(statements)) |
319 | html += h3("Statements about this") + ul(map(f statementToHTML, statements)); |
320 | |
321 | L<Comment> comments = conceptsWhere(Comment, conceptID := c.globalID); |
322 | html += h3("Comments") + ul(map(f commentToHTML, comments)); |
323 | |
324 | /* |
325 | Map<S, Node> nodes = allNodes(); |
326 | html += h3("Add forward relation") + hform( |
327 | hhidden(action := "addRelation") |
328 | + hhidden(concept1 := c.globalID) |
329 | + htableRaw_valignTop(ll( |
330 | ll("Relation Concept", hselectNode(map, "pggfeqzkqnezzfto", name := "rel")), |
331 | ll("Concept 2", hselectNode(nodes, name := "concept2", allowEmpty := true)), |
332 | ll("", hsubmit("Create relation")) |
333 | ))); |
334 | */ |
335 | |
336 | html += h3("Add comment") + hform( |
337 | hhidden(action := "addComment") |
338 | + hhidden(conceptID := c.globalID) |
339 | + htableRaw_valignTop(ll( |
340 | ll("Your Comment", hinputfield("text")), |
341 | ll("", hsubmit("Add comment")) |
342 | ))); |
343 | |
344 | html += p(ahref("http://ai1.lol/wiki", "[Home]")); |
345 | |
346 | ret html; |
347 | } |
348 | |
349 | sS hselectNode(Map<S, ? extends Node> map, O... params) { |
350 | ret hselect(orderMapByValue(mapValues(f nodeToString, map)), params); |
351 | } |
352 | |
353 | sS renderStatement(AIConcept c, Statement st, SS params) { |
354 | if (nempty(params.get("retranslate"))) { |
355 | RemoteDB db = new RemoteDB(truthTableProgram()); |
356 | db.xset(db.rc(st.id), possibleEnglishTranslation := null); |
357 | db.close(); |
358 | st.possibleEnglishTranslation = null; |
359 | } |
360 | |
361 | bool hasTranslation = nempty(st.possibleEnglishTranslation); |
362 | S html = htitle_h2("Statement " + st.globalID) |
363 | + ul( |
364 | "English (machine-generated): " + dynamize(or2(st.possibleEnglishTranslation, "?")), |
365 | "Computer Language: " + dynamize(st.text), |
366 | st.imported != 0 || nempty(st.importedFrom) ? "Imported: " + formatDateAndTime(st.imported) + " from " + dynamize(st.importedFrom) : null) |
367 | + (!hasTranslation ? "" : p(ahref("?retranslate=1", "Request retranslation"))); |
368 | html += p(ahref("http://ai1.lol/wiki", "[Home]")); |
369 | ret html; |
370 | } |
371 | |
372 | svoid processActions(SS params) { |
373 | S action = params.get("action"); |
374 | |
375 | if (eq(action, "addRelation")) { |
376 | S id1 = params.get("concept1"); |
377 | S id2 = params.get("concept2"); |
378 | S rel = params.get("rel"); |
379 | if (nempty(id1) && nempty(rel)) |
380 | createStatement(conceptQuote(rel) + " " + conceptQuote(id1) + " " + conceptQuoteOpt(id2)); |
381 | } |
382 | |
383 | if (eq(action, "addComment")) { |
384 | S conceptID = params.get("conceptID"); |
385 | S text = params.get("text"); |
386 | S user = ctc->concept; |
387 | uniq(Comment, +user, +conceptID, +text); |
388 | } |
389 | } |
390 | |
391 | static S latestDrawing() { |
392 | L metas = getDrawings(); |
393 | ret empty(metas) ? "" : |
394 | h3("Latest Drawing") |
395 | + renderDrawing(first(metas)); |
396 | } |
397 | |
398 | sS renderDrawing(O meta) { |
399 | ret p(renderMeta(meta) + "<br>" |
400 | + hsmall(ahref(progLink(#1007609), "Make your own.") |
401 | + " " + ahref("http://ai1.lol/images", "More drawings."))); |
402 | } |
403 | |
404 | static S randomDrawing() { |
405 | O meta = random(dropFirst(getDrawings())); |
406 | if (meta == null) ret ""; |
407 | ret h3("Random Drawing") |
408 | + renderDrawing(meta); |
409 | } |
410 | |
411 | static L getDrawings() { |
412 | pcall { |
413 | O imageServer = getBot(#1004590); |
414 | L metas = cast call(get(imageServer, "mainConcepts"), "list", "ImageMeta"); |
415 | metas = [O meta : metas | startsWithIC(getString(meta, "name"), "Drawing ")]; |
416 | metas = sortedByFieldDesc(metas, "created"); |
417 | ret metas; |
418 | } |
419 | ret ll(); |
420 | } |
421 | |
422 | static S renderMeta(O meta) { |
423 | S md5 = getString(meta, "md5"); |
424 | S jpg = imageServerJPEGLink(md5); |
425 | ret ahref(jpg, himg(jpg, |
426 | //style := "zoom: 50%" // works only on Chrome |
427 | onload := "this.width *= 0.7; this.onload=null;" |
428 | )); |
429 | } |
430 | |
431 | static L exportableAIConcepts() { |
432 | new L l; |
433 | for (AIConcept c : aiConcepts()) { |
434 | Concept cc = unlisted(Concept); |
435 | cc.className = "AIConcept"; |
436 | cc.created = c.created; |
437 | copyConceptFields(c, cc, "globalID", "name", "comment"); |
438 | l.add(cc); |
439 | } |
440 | ret l; |
441 | } |
442 | |
443 | static S userConcept() { |
444 | ret ctc! != null ? ctc->concept : ""; |
445 | } |
446 | |
447 | static long numComputers() { |
448 | ret getLong(getBot(#1003253), "numComputers"); |
449 | } |
450 | |
451 | !include #1007812 // Question Maker |
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: | 868 / 1546 |
Referenced in: | [show references] |