1 | sclass PoemBot { |
2 | L<E> poem; |
3 | int index; |
4 | new Map<S, S> wordMap; |
5 | |
6 | *() {} |
7 | *(L<E> *poem) {} |
8 | |
9 | bool linesEq(S a, S b) { |
10 | ret eqic(simplifyLine(a), simplifyLine(b)); |
11 | } |
12 | |
13 | S simplifyLine(S a) { |
14 | ret join(" ", codeTokens(nlTok2(a))); |
15 | } |
16 | |
17 | void reset() { |
18 | index = 0; |
19 | wordMap.clear(); |
20 | } |
21 | |
22 | S answer(S s) { |
23 | exceptionToUser { |
24 | if (index >= l(poem)) ret "Sorry, out of script. ^^"; |
25 | learnLine(assertNotNull(poem.get(index).q), s); |
26 | ++index; |
27 | if (index >= l(poem)) ret "Sorry, out of script. ^^"; |
28 | S a = poem.get(index).a; |
29 | if (nempty(a)) { |
30 | ++index; |
31 | ret translateLine(a); |
32 | } |
33 | } |
34 | ret ""; |
35 | } |
36 | |
37 | void learnLine(S in, S out) { |
38 | L<S> t1 = nlTok2(dropPunctuation2(in)); |
39 | L<S> t2 = nlTok2(dropPunctuation2(out)); |
40 | if (l(t1) != l(t2)) fail("Please say something like this: " + in); |
41 | |
42 | for (int i = 1; i < l(t1); i += 2) { |
43 | S w1 = t1.get(i), w2 = t2.get(i); |
44 | if (!eqic(w1, w2)) |
45 | // just overwrite - be flexible! |
46 | wordMap.put(w1.toLowerCase(), w2.toLowerCase()); |
47 | } |
48 | } |
49 | |
50 | S translateLine(S s) { |
51 | L<S> tok = nlTok2(s); |
52 | for (int i = 1; i < l(tok); i += 2) { |
53 | S w = wordMap.get(tok.get(i).toLowerCase()); |
54 | if (nempty(w)) |
55 | tok.set(i, w); |
56 | } |
57 | ret join(tok); |
58 | } |
59 | } |
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: | 628 / 823 |
Referenced in: | [show references] |