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

59
LINES

< > BotCompany Repo | #1003209 // class PoemBot - word-by-word poem bot

JavaX fragment (include)

sclass PoemBot {
  L<E> poem;
  int index;
  new Map<S, S> wordMap;

  *() {}
  *(L<E> *poem) {}
  
  bool linesEq(S a, S b) {
    ret eqic(simplifyLine(a), simplifyLine(b));
  }

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

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

  S answer(S s) {
    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);
      }
    }
    ret "";
  }

  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());
    }
  }

  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);
  }
}

Author comment

Began life as a copy of #1003075

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: #1003209
Snippet name: class PoemBot - word-by-word poem bot
Eternal ID of this version: #1003209/1
Text MD5: b5737fdafb1fdfe3776061051808ee0c
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-31 00:27:17
Source code size: 1358 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 627 / 822
Referenced in: [show references]