concept 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; } } concept GuessClass { new Ref line; S className; O guessedBy; } concept Guess { new Ref interpretation; new Ref guessedAs; O guessedBy; } concept Interpretation {} // generic stuff concept Praise extends Interpretation {} concept NotForMe extends Interpretation {} concept Hello extends Interpretation {} concept ByeBye extends Interpretation {} concept Unclear extends Interpretation {} // variables static JComponent controls; static Guess guessShowing; static Line lineShowing; static JList listShowing; svoid initConsole { consoleFont(loadFont("#1004970", 20f)); setConsoleHeight(500); centerBigConsole(); renameConsole(programTitle()); consoleMargin(10); defaultControls(); clearConsole(); } synchronized static S answer(S s) { 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; } ret answerInterpretedWithStandards(s, ip); } static S answerInterpretedWithStandards(S s, Interpretation ip) { if (ip instanceof Praise) ret kevin("Thank you :)"); if (ip instanceof Hello) ret kevin("Hello! :)"); if (ip instanceof ByeBye) { kevin("Bye user! :)"); sleepSeconds(1); cleanKillVM(); } if (ip instanceof NotForMe) ret kevin("Talk to the hand"); if (ip instanceof Unclear) ret "[Unclear statement]"; ret answerInterpreted(s, ip); } 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 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 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 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)); } }