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

213
LINES

< > BotCompany Repo | #1005037 // Text AI [Include], v2 with interpretation class finding

JavaX fragment (include)

concept 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;
  }
}

concept GuessClass {
  new Ref<Line> line;
  S className;
  O guessedBy;
}

concept Guess {
  new Ref<Interpretation> interpretation;
  new Ref<Line> 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<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));
  }
}

Author comment

Began life as a copy of #1004972

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: #1005037
Snippet name: Text AI [Include], v2 with interpretation class finding
Eternal ID of this version: #1005037/1
Text MD5: 44ff972d262dfd11eaa24400d53dae43
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-29 19:53:22
Source code size: 5585 bytes / 213 lines
Pitched / IR pitched: No / No
Views / Downloads: 414 / 373
Referenced in: [show references]