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

207
LINES

< > BotCompany Repo | #1004972 // Text AI [Include]

JavaX fragment (include)

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

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: 590 / 1056
Version history: 2 change(s)
Referenced in: [show references]