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

!752

static L<E> poem;
static int index;
static new Map<S, S> wordMap;

p {
  poem = parsePoem(first(loadPoems("#1003074")).lines);
  printStructure(poem);
  testWith(parsePoem(first(loadPoems("#1003076")).lines));
  reset();
  makeBot("Chemistry Bot");
}

static void testWith(L<E> test) {
  for (int i = 0; i+1 < l(test); i += 2) {
    S q = test.get(i).q, a = test.get(i+1).a;
    S answer = callStaticAnswerMethod(mc(), q);
    if (!linesEq(a, answer))
      fail("Test failed: " + quote(answer) + " vs " + quote(a));
    print("OK: " + answer);
  }
  print("Test OK!");
}

static bool linesEq(S a, S b) {
  ret eqic(simplifyLine(a), simplifyLine(b));
}

static S simplifyLine(S a) {
  ret join(" ", codeTokens(nlTok2(a)));
}

static void reset() {
  index = 0;
  wordMap.clear();
}

answer {
  exceptionToUser {
    if (index >= l(poem)) ret "Sorry, out of script. ^^";
    learnLine(assertNotNull(poem.get(index).q), s);
    ++index;
    if (index >= l(poem)) ret "Sorry, out of script. ^^";
    S a = poem.get(index).a;
    if (nempty(a)) {
      ++index;
      ret translateLine(a);
    }
  }
}

static void learnLine(S in, S out) {
  L<S> t1 = nlTok2(dropPunctuation2(in));
  L<S> t2 = nlTok2(dropPunctuation2(out));
  if (l(t1) != l(t2)) fail("Please say something like this: " + in);
  
  for (int i = 1; i < l(t1); i += 2) {
    S w1 = t1.get(i), w2 = t2.get(i);
    if (!eqic(w1, w2))
      // just overwrite - be flexible!
      wordMap.put(w1.toLowerCase(), w2.toLowerCase());
  }
}

static S translateLine(S s) {
  L<S> tok = nlTok2(s);
  for (int i = 1; i < l(tok); i += 2) {
    S w = wordMap.get(tok.get(i).toLowerCase());
    if (nempty(w))
      tok.set(i, w);
  }
  ret join(tok);
}

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