sconcept Line {
  S text;
  new Ref<Interpretation> interpretation;
  new Ref<Guess> guess;
  new Ref<Guess> 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> interpretation;
  new Ref<Line> guessedAs;
  O guessedBy;
}
sconcept Interpretation {}
// generic stuff
sconcept Praise extends Interpretation {}
sconcept NotForMe extends Interpretation {}
sconcept Hello extends Interpretation {}
sconcept ByeBye extends Interpretation {}
sconcept 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 RC 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<Line, Int> 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<S> 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));
  }
}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: | #1004972 | 
| Snippet name: | Text AI [Include] | 
| Eternal ID of this version: | #1004972/3 | 
| Text MD5: | cc2cd542dbc7ff285b95b595365ddc2b | 
| Author: | stefan | 
| Category: | javax | 
| Type: | JavaX fragment (include) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2017-03-15 14:18:59 | 
| Source code size: | 5508 bytes / 207 lines | 
| Pitched / IR pitched: | No / No | 
| Views / Downloads: | 858 / 1344 | 
| Version history: | 2 change(s) | 
| Referenced in: | [show references] |