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

74
LINES

< > BotCompany Repo | #1003075 // Chemistry Bot (a Poet Bot)

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

Libraryless. Click here for Pure Java version (2654L/19K/59K).

1  
!752
2  
3  
static L<E> poem;
4  
static int index;
5  
static new Map<S, S> wordMap;
6  
7  
p {
8  
  poem = parsePoem(first(loadPoems("#1003074")).lines);
9  
  printStructure(poem);
10  
  testWith(parsePoem(first(loadPoems("#1003076")).lines));
11  
  reset();
12  
  makeBot("Chemistry Bot");
13  
}
14  
15  
static void testWith(L<E> test) {
16  
  for (int i = 0; i+1 < l(test); i += 2) {
17  
    S q = test.get(i).q, a = test.get(i+1).a;
18  
    S answer = callStaticAnswerMethod(mc(), q);
19  
    if (!linesEq(a, answer))
20  
      fail("Test failed: " + quote(answer) + " vs " + quote(a));
21  
    print("OK: " + answer);
22  
  }
23  
  print("Test OK!");
24  
}
25  
26  
static bool linesEq(S a, S b) {
27  
  ret eqic(simplifyLine(a), simplifyLine(b));
28  
}
29  
30  
static S simplifyLine(S a) {
31  
  ret join(" ", codeTokens(nlTok2(a)));
32  
}
33  
34  
static void reset() {
35  
  index = 0;
36  
  wordMap.clear();
37  
}
38  
39  
answer {
40  
  exceptionToUser {
41  
    if (index >= l(poem)) ret "Sorry, out of script. ^^";
42  
    learnLine(assertNotNull(poem.get(index).q), s);
43  
    ++index;
44  
    if (index >= l(poem)) ret "Sorry, out of script. ^^";
45  
    S a = poem.get(index).a;
46  
    if (nempty(a)) {
47  
      ++index;
48  
      ret translateLine(a);
49  
    }
50  
  }
51  
}
52  
53  
static void learnLine(S in, S out) {
54  
  L<S> t1 = nlTok2(dropPunctuation2(in));
55  
  L<S> t2 = nlTok2(dropPunctuation2(out));
56  
  if (l(t1) != l(t2)) fail("Please say something like this: " + in);
57  
  
58  
  for (int i = 1; i < l(t1); i += 2) {
59  
    S w1 = t1.get(i), w2 = t2.get(i);
60  
    if (!eqic(w1, w2))
61  
      // just overwrite - be flexible!
62  
      wordMap.put(w1.toLowerCase(), w2.toLowerCase());
63  
  }
64  
}
65  
66  
static S translateLine(S s) {
67  
  L<S> tok = nlTok2(s);
68  
  for (int i = 1; i < l(tok); i += 2) {
69  
    S w = wordMap.get(tok.get(i).toLowerCase());
70  
    if (nempty(w))
71  
      tok.set(i, w);
72  
  }
73  
  ret join(tok);
74  
}

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: #1003075
Snippet name: Chemistry Bot (a Poet Bot)
Eternal ID of this version: #1003075/1
Text MD5: bfb15b2ed39d7ee53ed389216a85361f
Transpilation MD5: 854c225fdc020b7aef67114dc9cff912
Author: stefan
Category: eleu / nl
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-24 02:10:39
Source code size: 1778 bytes / 74 lines
Pitched / IR pitched: No / No
Views / Downloads: 606 / 647
Referenced in: [show references]