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).

1  
!752
2  
3  
!include #1004681 // Concepts
4  
5  
static Map<Class, S> initialLines() {
6  
  ret lithashmap(
7  
    ShowWindow.class, "Show the window!",
8  
    HideWindow.class, "Hide the window!",
9  
    IsShowing.class, "Is the window showing?",
10  
    Praise.class, "Nicely done, AI.",
11  
    Hello.class, "Hello AI!",
12  
    NotForMe.class, "[Statement not meant for me]",
13  
    Unclear.class, "[Unclear statement]");
14  
}
15  
16  
sconcept Line {
17  
  S text;
18  
  new Ref<Interpretation> interpretation;
19  
  new Ref<Guess> guess;
20  
  new Ref<Guess> acceptedGuess;
21  
  
22  
  *() {}
23  
  *(S *text) {}
24  
  
25  
  Guess guessAs(Line l, S guessedBy) {
26  
    new Guess g;
27  
    g.guessedAs.set(l);
28  
    g.interpretation.set(l.interpretation);
29  
    g.guessedBy = guessedBy;
30  
    guess.set(g);
31  
    ret g;
32  
  }
33  
}
34  
35  
sconcept Guess {
36  
  new Ref<Interpretation> interpretation;
37  
  new Ref<Line> guessedAs;
38  
  O guessedBy;
39  
}
40  
41  
sconcept Interpretation {}
42  
43  
// generic stuff
44  
sconcept Praise extends Interpretation {}
45  
sconcept NotForMe extends Interpretation {}
46  
sconcept Hello extends Interpretation {}
47  
sconcept Unclear extends Interpretation {}
48  
49  
// the application (show/hide window)
50  
sconcept ShowWindow extends Interpretation {}
51  
sconcept HideWindow extends Interpretation {}
52  
sconcept IsShowing extends Interpretation {
53  
  new Ref answerModulator; // optional
54  
}
55  
56  
sconcept TheFlag {
57  
  bool set;
58  
}
59  
60  
static JFrame frame;
61  
static JComponent controls;
62  
static Guess guessShowing;
63  
static Line lineShowing;
64  
static JList listShowing;
65  
66  
p-substance {
67  
  bootstrapConceptsFrom("#1004922");
68  
  loadAndAutoSaveConcepts();
69  
  initialConcepts();
70  
  concepts_quietSave = true;
71  
  
72  
  frame = getFrame(showImage("#1002235", "Eleu walks"));
73  
  onFrameClose(frame, r {
74  
    print("> Window closed by user.");
75  
    setFlag(false);
76  
  });
77  
  updateVisibility();
78  
  print("Lines: " + l(conceptsOfType("Line")));
79  
  makeBot("Boolean Flag Test.");
80  
  methodsBot("Boolean Flag DB.", listPlus(
81  
    exposedDBMethods, "xfindLine"));
82  
  //typeWriterConsole();
83  
  //boldConsole();
84  
  consoleFont(loadFont(
85  
    //"#1004887",
86  
    //"#1004968",
87  
    //"#1004969",
88  
    "#1004970",
89  
    20f));
90  
  setConsoleHeight(500);
91  
  centerBigConsole();
92  
  renameConsole(programTitle());
93  
  consoleMargin(10);
94  
  defaultControls();
95  
  clearConsole();
96  
  print("AI ready to rock. Go \"Admin\" to see my commands.");
97  
  print("Or try \"show window\" and \"hide window\".");
98  
  kevin("Hello");
99  
  print();
100  
}
101  
102  
static TheFlag getFlag() {
103  
  ret unary(TheFlag.class);
104  
}
105  
106  
synchronized answer {
107  
  TheFlag flag = getFlag();
108  
  Line line = findLine(s);
109  
  if (line == null)
110  
    line = new Line(s);
111  
  Interpretation ip = line.interpretation.get();
112  
  if (ip == null) {
113  
    Guess guess = guess(line);
114  
    if (guess == null)
115  
      ret "No interpretation - or guess -  for " + quote(s);
116  
    lineShowing = line;
117  
    guessShowing = guess;
118  
    final S q = "I'm guessing you mean " + quote(guess.guessedAs.get().text) + "?";
119  
    awt {
120  
      setControls(centerAndSouth(
121  
        withBottomMargin(jboldLabel(q)),
122  
        centerAndEast(jCenteredLine(
123  
        jbutton("Yes", "guessYes"),
124  
        jbutton("No", "guessNo")),
125  
        jCenteredLine(jbutton("Admin", "admin"), jbutton("Contribute", "contribute")))));
126  
    }
127  
    ret q;
128  
  }
129  
  if (ip instanceof Praise) ret kevin("Thank you :)");
130  
  if (ip instanceof Hello) ret kevin("Hello! :)");
131  
  if (ip instanceof NotForMe) ret kevin("Talk to the hand");
132  
  if (ip instanceof Unclear) ret "[Unclear statement]";
133  
  
134  
  if (ip instanceof IsShowing)
135  
    ret kevin(flag.set
136  
      ? "Window is showing."
137  
      : "Window is not showing.");
138  
    
139  
  if (ip instanceof ShowWindow) {
140  
    if (flag.set) ret "Window already showing!";
141  
    setFlag(true); updateVisibility();
142  
    kevin("showing");
143  
    ret "OK, showing window.";
144  
  }
145  
  
146  
  if (ip instanceof HideWindow) {
147  
    if (!flag.set) ret "Window already hidden!";
148  
    setFlag(false); updateVisibility();
149  
    kevin("hiding");
150  
    ret "OK, hiding window.";
151  
  }
152  
}
153  
154  
static Line findLine(S text) {
155  
  ret findWhere(list(Line.class), "text", text);
156  
}
157  
158  
static PassRef xfindLine(S text) {
159  
  ret toPassRef(findLine(text));
160  
}
161  
162  
static Guess guess(Line line) {
163  
  line.guess.clear(); // guess every time
164  
  //if (line.guess.has()) ret line.guess.get();
165  
  
166  
  // "match" method
167  
  /*for (Line l : list(Line.class))
168  
    if (l.interpretation.has() && match(l.text, line.text))
169  
      ret line.guessAs(l, "match");*/
170  
    
171  
  // levenshtein method
172  
  new Map<Line, Int> scores;
173  
  for (Line l : list(Line.class))
174  
    if (l.interpretation.has()) {
175  
      int dist = leven(toLower(l.text), toLower(line.text));
176  
      //print(line.text + " vs " + l.text + " => " + dist);
177  
      scores.put(l, -dist);
178  
    }
179  
  if (nempty(scores))
180  
    ret line.guessAs(highest(scores), "leven");
181  
    
182  
  null;
183  
}
184  
185  
186  
svoid updateVisibility {
187  
  awt {
188  
    frame.setVisible(getFlag().set);
189  
    swingLater(200, r {
190  
      consoleFrame().toFront();
191  
    });
192  
    //focusConsole();
193  
  }
194  
}
195  
196  
svoid setControls(final JComponent controls) {
197  
  awtIfNecessary {
198  
    hideControls();
199  
    JComponent _controls = withMargin(controls);
200  
    main.controls = _controls;
201  
    addToConsole2(_controls);
202  
  }
203  
}
204  
205  
svoid hideControls {
206  
  removeFromConsole2(controls);
207  
  controls = null;
208  
}
209  
210  
svoid guessYes {
211  
  print("Yes");
212  
  Line line = lineShowing;
213  
  line.acceptedGuess.set(guessShowing);
214  
  line.interpretation.set(guessShowing.interpretation);
215  
  line.guess.clear();
216  
  guessShowing = null;
217  
  lineShowing = null;
218  
  defaultControls();
219  
  execLine(line);
220  
}
221  
222  
svoid execLine(Line line) {
223  
  print("> " + callStaticAnswerMethod(mc(), line.text));
224  
}
225  
226  
svoid guessNo {
227  
  print("No");
228  
  new L<S> texts;
229  
  for (Line line : reversedList(sortedByField(list(Line.class), "created")))
230  
    if (line.interpretation.has())
231  
      texts.add(line.text);
232  
  //texts = sortedIgnoreCase(texts);
233  
  S q = "What does " + quote(lineShowing.text) + " mean then?";
234  
  print("> " + q);
235  
  setControls(northCenterAndSouth(
236  
    withBottomMargin(jboldLabel(q)),
237  
    listShowing = jlist(texts), 
238  
      centerAndEast(jCenteredLine(
239  
        jbutton("Accept", "accept"),
240  
        jbutton("Cancel", "cancel")),
241  
        jCenteredLine(jbutton("Admin", "admin"),
242  
        jbutton("Contribute", "contribute")))));
243  
  onDoubleClick(listShowing, r { accept() });
244  
}
245  
246  
svoid admin {
247  
  runInNewThread("#1004926");
248  
}
249  
250  
svoid defaultControls {
251  
  setControls(centerAndEast(new JPanel,
252  
    jCenteredLine(jbutton("Admin", "admin"),
253  
    jbutton("Contribute", "contribute"))));
254  
  focusConsole();
255  
}
256  
257  
svoid accept {
258  
  S text = cast getSelectedItem(listShowing);
259  
  if (text == null) ret;
260  
  Line l = findLine(text);
261  
  if (l == null) ret;
262  
  print("It means " + quote(text) + " (" + shortClassName(l.interpretation.get()) + ")");
263  
  lineShowing.interpretation.set(l.interpretation);
264  
  execLine(lineShowing);
265  
  cancel();
266  
}
267  
268  
svoid cancel {
269  
  guessShowing = null;
270  
  lineShowing = null;
271  
  listShowing = null;
272  
  defaultControls();
273  
}
274  
275  
svoid initialConcepts {
276  
  Map<Class, S> initialLines = initialLines();
277  
  for (Class c : keys(initialLines)) {
278  
    Interpretation intp = cast unary(c);
279  
    Line line = findBackRef(intp, Line.class);
280  
    if (line == null) {
281  
      S text = initialLines.get(c);
282  
      print("Making initial line " + quote(text));
283  
      new Line(text).interpretation.set(intp);
284  
    }
285  
  }
286  
}
287  
288  
svoid setFlag(bool flag) {
289  
  getFlag().set = flag;
290  
  change();
291  
}
292  
293  
svoid contribute {
294  
  kevin("Do you want to contribute?");
295  
  if (confirmYesNo(consoleFrame(), "Do you want to contribute your language database to the project?")) {
296  
   S id = ntUpload(programID(),
297  
     "Language Database from " + computerID(),
298  
     loadTextFile(getProgramFile("concepts.structure")));
299  
     print("Thanks! Uploaded to " + shortSnippetLink(id));
300  
  }
301  
}

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: 859 / 1725
Referenced in: [show references]