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

301
LINES

< > BotCompany Repo | #1004930 // New AI [Show / Hide Window / Learns Any Language!]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 13052K of libraries. Click here for Pure Java version (7262L/50K/165K).

!752

!include #1004681 // Concepts

static Map<Class, S> 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> 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 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<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 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<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 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<Class, S> 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));
  }
}

Author comment

Began life as a copy of #1004922

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xhsrmoityime

No comments. add comment

Snippet ID: #1004930
Snippet name: New AI [Show / Hide Window / Learns Any Language!]
Eternal ID of this version: #1004930/1
Text MD5: db0e932e7a017f5223cb18c1e932f511
Transpilation MD5: 06f3e643fe396a8b5746cfd78b454620
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-19 02:38:49
Source code size: 7859 bytes / 301 lines
Pitched / IR pitched: No / No
Views / Downloads: 857 / 1723
Referenced in: [show references]