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)

1  
concept Line {
2  
  S text;
3  
  new Ref<Interpretation> interpretation;
4  
  new Ref<Guess> guess;
5  
  new Ref<Guess> acceptedGuess;
6  
  
7  
  *() {}
8  
  *(S *text) {}
9  
  
10  
  Guess guessAs(Line l, S guessedBy) {
11  
    new Guess g;
12  
    g.guessedAs.set(l);
13  
    g.interpretation.set(l.interpretation);
14  
    g.guessedBy = guessedBy;
15  
    guess.set(g);
16  
    ret g;
17  
  }
18  
}
19  
20  
concept GuessClass {
21  
  new Ref<Line> line;
22  
  S className;
23  
  O guessedBy;
24  
}
25  
26  
concept Guess {
27  
  new Ref<Interpretation> interpretation;
28  
  new Ref<Line> guessedAs;
29  
  O guessedBy;
30  
}
31  
32  
concept Interpretation {}
33  
34  
// generic stuff
35  
36  
concept Praise extends Interpretation {}
37  
concept NotForMe extends Interpretation {}
38  
concept Hello extends Interpretation {}
39  
concept ByeBye extends Interpretation {}
40  
concept Unclear extends Interpretation {}
41  
42  
// variables
43  
44  
static JComponent controls;
45  
static Guess guessShowing;
46  
static Line lineShowing;
47  
static JList listShowing;
48  
49  
svoid initConsole {
50  
  consoleFont(loadFont("#1004970", 20f));
51  
  setConsoleHeight(500);
52  
  centerBigConsole();
53  
  renameConsole(programTitle());
54  
  consoleMargin(10);
55  
  defaultControls();
56  
  clearConsole();
57  
}
58  
59  
synchronized static S answer(S s) {
60  
  Line line = findLine(s);
61  
  if (line == null)
62  
    line = new Line(s);
63  
  Interpretation ip = line.interpretation.get();
64  
  if (ip == null) {
65  
    Guess guess = guess(line);
66  
    if (guess == null)
67  
      ret "No interpretation - or guess -  for " + quote(s);
68  
    lineShowing = line;
69  
    guessShowing = guess;
70  
    final S q = "I'm guessing you mean " + quote(guess.guessedAs.get().text) + "?";
71  
    awt {
72  
      setControls(centerAndSouth(
73  
        withBottomMargin(jboldLabel(q)),
74  
        centerAndEast(jCenteredLine(
75  
        jbutton("Yes", "guessYes"),
76  
        jbutton("No", "guessNo")),
77  
        jCenteredLine(jbutton("Admin", "admin"), jbutton("Contribute", "contribute")))));
78  
    }
79  
    ret q;
80  
  }
81  
  
82  
  ret answerInterpretedWithStandards(s, ip);
83  
}
84  
85  
static S answerInterpretedWithStandards(S s, Interpretation ip) {
86  
  if (ip instanceof Praise) ret kevin("Thank you :)");
87  
  if (ip instanceof Hello) ret kevin("Hello! :)");
88  
  if (ip instanceof ByeBye) {
89  
    kevin("Bye user! :)");
90  
    sleepSeconds(1);
91  
    cleanKillVM();
92  
  }
93  
  if (ip instanceof NotForMe) ret kevin("Talk to the hand");
94  
  if (ip instanceof Unclear) ret "[Unclear statement]";
95  
  
96  
  ret answerInterpreted(s, ip);
97  
}
98  
99  
static Line findLine(S text) {
100  
  ret findWhere(list(Line.class), "text", text);
101  
}
102  
103  
static PassRef xfindLine(S text) {
104  
  ret toPassRef(findLine(text));
105  
}
106  
107  
static Guess guess(Line line) {
108  
  line.guess.clear(); // guess every time
109  
  //if (line.guess.has()) ret line.guess.get();
110  
  
111  
  // "match" method
112  
  /*for (Line l : list(Line.class))
113  
    if (l.interpretation.has() && match(l.text, line.text))
114  
      ret line.guessAs(l, "match");*/
115  
    
116  
  // levenshtein method
117  
  new Map<Line, Int> scores;
118  
  for (Line l : list(Line.class))
119  
    if (l.interpretation.has()) {
120  
      int dist = leven(toLower(l.text), toLower(line.text));
121  
      //print(line.text + " vs " + l.text + " => " + dist);
122  
      scores.put(l, -dist);
123  
    }
124  
  if (nempty(scores))
125  
    ret line.guessAs(highest(scores), "leven");
126  
    
127  
  null;
128  
}
129  
130  
svoid setControls(final JComponent controls) {
131  
  awtIfNecessary {
132  
    hideControls();
133  
    JComponent _controls = withMargin(controls);
134  
    main.controls = _controls;
135  
    addToConsole2(_controls);
136  
  }
137  
}
138  
139  
svoid hideControls {
140  
  removeFromConsole2(controls);
141  
  controls = null;
142  
}
143  
144  
svoid guessYes {
145  
  print("Yes");
146  
  Line line = lineShowing;
147  
  line.acceptedGuess.set(guessShowing);
148  
  line.interpretation.set(guessShowing.interpretation);
149  
  line.guess.clear();
150  
  guessShowing = null;
151  
  lineShowing = null;
152  
  defaultControls();
153  
  execLine(line);
154  
}
155  
156  
svoid execLine(Line line) {
157  
  print("> " + callStaticAnswerMethod(mc(), line.text));
158  
}
159  
160  
svoid guessNo {
161  
  print("No");
162  
  new L<S> texts;
163  
  for (Line line : reversedList(sortedByField(list(Line.class), "created")))
164  
    if (line.interpretation.has())
165  
      texts.add(line.text);
166  
  //texts = sortedIgnoreCase(texts);
167  
  S q = "What does " + quote(lineShowing.text) + " mean then?";
168  
  print("> " + q);
169  
  setControls(northCenterAndSouth(
170  
    withBottomMargin(jboldLabel(q)),
171  
    listShowing = jlist(texts), 
172  
      centerAndEast(jCenteredLine(
173  
        jbutton("Accept", "accept"),
174  
        jbutton("Cancel", "cancel")),
175  
        jCenteredLine(jbutton("Admin", "admin"),
176  
        jbutton("Contribute", "contribute")))));
177  
  onDoubleClick(listShowing, r { accept() });
178  
}
179  
180  
svoid defaultControls {
181  
  setControls(centerAndEast(new JPanel,
182  
    jCenteredLine(jbutton("Admin", "admin"),
183  
    jbutton("Contribute", "contribute"))));
184  
  focusConsole();
185  
}
186  
187  
svoid accept {
188  
  S text = cast getSelectedItem(listShowing);
189  
  if (text == null) ret;
190  
  Line l = findLine(text);
191  
  if (l == null) ret;
192  
  print("It means " + quote(text) + " (" + shortClassName(l.interpretation.get()) + ")");
193  
  lineShowing.interpretation.set(l.interpretation);
194  
  execLine(lineShowing);
195  
  cancel();
196  
}
197  
198  
svoid cancel {
199  
  guessShowing = null;
200  
  lineShowing = null;
201  
  listShowing = null;
202  
  defaultControls();
203  
}
204  
205  
svoid contribute {
206  
  kevin("Do you want to contribute?");
207  
  if (confirmYesNo(consoleFrame(), "Do you want to contribute your language database to the project?")) {
208  
   S id = ntUpload(programID(),
209  
     "Language Database from " + computerID(),
210  
     loadTextFile(getProgramFile("concepts.structure")));
211  
     print("Thanks! Uploaded to " + shortSnippetLink(id));
212  
  }
213  
}

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