!752 !include #1004681 // Concepts static Map initialLines() { ret lithashmap( ShowWindow.class, "Show the window!", HideWindow.class, "Hide the window!", IsShowing.class, "Is the window showing?", Praise.class, "Nicely done, AI.", Hello.class, "Hello AI!", NotForMe.class, "[Statement not meant for me]", Unclear.class, "[Unclear statement]"); } sconcept Line { S text; new Ref interpretation; new Ref guess; new Ref acceptedGuess; *() {} *(S *text) {} Guess guessAs(Line l, S guessedBy) { new Guess g; g.guessedAs.set(l); g.interpretation.set(l.interpretation); g.guessedBy = guessedBy; guess.set(g); ret g; } } sconcept Guess { new Ref interpretation; new Ref guessedAs; O guessedBy; } sconcept Interpretation {} // generic stuff sconcept Praise extends Interpretation {} sconcept NotForMe extends Interpretation {} sconcept Hello extends Interpretation {} sconcept Unclear extends Interpretation {} // the application (show/hide window) sconcept ShowWindow extends Interpretation {} sconcept HideWindow extends Interpretation {} sconcept IsShowing extends Interpretation { new Ref answerModulator; // optional } sconcept TheFlag { bool set; } static JFrame frame; static JComponent controls; static Guess guessShowing; static Line lineShowing; static JList listShowing; p-substance { bootstrapConceptsFrom("#1004922"); loadAndAutoSaveConcepts(); initialConcepts(); concepts_quietSave = true; frame = getFrame(showImage("#1002235", "Eleu walks")); onFrameClose(frame, r { print("> Window closed by user."); setFlag(false); }); updateVisibility(); print("Lines: " + l(conceptsOfType("Line"))); makeBot("Boolean Flag Test."); methodsBot("Boolean Flag DB.", listPlus( exposedDBMethods, "xfindLine")); //typeWriterConsole(); //boldConsole(); consoleFont(loadFont( //"#1004887", //"#1004968", //"#1004969", "#1004970", 20f)); setConsoleHeight(500); centerBigConsole(); renameConsole(programTitle()); consoleMargin(10); defaultControls(); clearConsole(); print("AI ready to rock. Go \"Admin\" to see my commands."); print("Or try \"show window\" and \"hide window\"."); kevin("Hello"); print(); } static TheFlag getFlag() { ret unary(TheFlag.class); } synchronized answer { TheFlag flag = getFlag(); Line line = findLine(s); if (line == null) line = new Line(s); Interpretation ip = line.interpretation.get(); if (ip == null) { Guess guess = guess(line); if (guess == null) ret "No interpretation - or guess - for " + quote(s); lineShowing = line; guessShowing = guess; final S q = "I'm guessing you mean " + quote(guess.guessedAs.get().text) + "?"; awt { setControls(centerAndSouth( withBottomMargin(jboldLabel(q)), centerAndEast(jCenteredLine( jbutton("Yes", "guessYes"), jbutton("No", "guessNo")), jCenteredLine(jbutton("Admin", "admin"), jbutton("Contribute", "contribute"))))); } ret q; } if (ip instanceof Praise) ret kevin("Thank you :)"); if (ip instanceof Hello) ret kevin("Hello! :)"); if (ip instanceof NotForMe) ret kevin("Talk to the hand"); if (ip instanceof Unclear) ret "[Unclear statement]"; if (ip instanceof IsShowing) ret kevin(flag.set ? "Window is showing." : "Window is not showing."); if (ip instanceof ShowWindow) { if (flag.set) ret "Window already showing!"; setFlag(true); updateVisibility(); kevin("showing"); ret "OK, showing window."; } if (ip instanceof HideWindow) { if (!flag.set) ret "Window already hidden!"; setFlag(false); updateVisibility(); kevin("hiding"); ret "OK, hiding window."; } } static Line findLine(S text) { ret findWhere(list(Line.class), "text", text); } static PassRef xfindLine(S text) { ret toPassRef(findLine(text)); } static Guess guess(Line line) { line.guess.clear(); // guess every time //if (line.guess.has()) ret line.guess.get(); // "match" method /*for (Line l : list(Line.class)) if (l.interpretation.has() && match(l.text, line.text)) ret line.guessAs(l, "match");*/ // levenshtein method new Map scores; for (Line l : list(Line.class)) if (l.interpretation.has()) { int dist = leven(toLower(l.text), toLower(line.text)); //print(line.text + " vs " + l.text + " => " + dist); scores.put(l, -dist); } if (nempty(scores)) ret line.guessAs(highest(scores), "leven"); null; } svoid updateVisibility { awt { frame.setVisible(getFlag().set); swingLater(200, r { consoleFrame().toFront(); }); //focusConsole(); } } svoid setControls(final JComponent controls) { awtIfNecessary { hideControls(); JComponent _controls = withMargin(controls); main.controls = _controls; addToConsole2(_controls); } } svoid hideControls { removeFromConsole2(controls); controls = null; } svoid guessYes { print("Yes"); Line line = lineShowing; line.acceptedGuess.set(guessShowing); line.interpretation.set(guessShowing.interpretation); line.guess.clear(); guessShowing = null; lineShowing = null; defaultControls(); execLine(line); } svoid execLine(Line line) { print("> " + callStaticAnswerMethod(mc(), line.text)); } svoid guessNo { print("No"); new L texts; for (Line line : reversedList(sortedByField(list(Line.class), "created"))) if (line.interpretation.has()) texts.add(line.text); //texts = sortedIgnoreCase(texts); S q = "What does " + quote(lineShowing.text) + " mean then?"; print("> " + q); setControls(northCenterAndSouth( withBottomMargin(jboldLabel(q)), listShowing = jlist(texts), centerAndEast(jCenteredLine( jbutton("Accept", "accept"), jbutton("Cancel", "cancel")), jCenteredLine(jbutton("Admin", "admin"), jbutton("Contribute", "contribute"))))); onDoubleClick(listShowing, r { accept() }); } svoid admin { runInNewThread("#1004926"); } svoid defaultControls { setControls(centerAndEast(new JPanel, jCenteredLine(jbutton("Admin", "admin"), jbutton("Contribute", "contribute")))); focusConsole(); } svoid accept { S text = cast getSelectedItem(listShowing); if (text == null) ret; Line l = findLine(text); if (l == null) ret; print("It means " + quote(text) + " (" + shortClassName(l.interpretation.get()) + ")"); lineShowing.interpretation.set(l.interpretation); execLine(lineShowing); cancel(); } svoid cancel { guessShowing = null; lineShowing = null; listShowing = null; defaultControls(); } svoid initialConcepts { Map initialLines = initialLines(); for (Class c : keys(initialLines)) { Interpretation intp = cast unary(c); Line line = findBackRef(intp, Line.class); if (line == null) { S text = initialLines.get(c); print("Making initial line " + quote(text)); new Line(text).interpretation.set(intp); } } } svoid setFlag(bool flag) { getFlag().set = flag; change(); } svoid contribute { kevin("Do you want to contribute?"); if (confirmYesNo(consoleFrame(), "Do you want to contribute your language database to the project?")) { S id = ntUpload(programID(), "Language Database from " + computerID(), loadTextFile(getProgramFile("concepts.structure"))); print("Thanks! Uploaded to " + shortSnippetLink(id)); } }