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

140
LINES

< > BotCompany Repo | #1004022 // Generators for Random v8

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

Libraryless. Click here for Pure Java version (2083L/14K/54K).

1  
!759
2  
3  
!include #1004027 // WordAdapter
4  
5  
static bool adapt_debug;
6  
7  
// put func {}'s returning a string in there
8  
// not using _log parameter anymore
9  
static void makeGenerators(L<Gen> l, L<S> _log) {
10  
  gen(l, func { "3" });
11  
  gen(l, "plus1", func { parseLong(gI())+1 });
12  
  gen(l, "delta", func { parseLong(g0())*2-parseLong(g1()) });
13  
  gen(l, "delta unicode", func {
14  
    S a = g1(), b = g0();
15  
    if (l(a) == 1 && l(b) == 1)
16  
      ret intToChar(charToInt(b)*2-charToInt(a));
17  
    null;
18  
  });
19  
  gen(l, "exponential", func { sqr(parseLong(g0()))/parseLong(g1()) });
20  
  gen(l, "assoc", gOneAssoc());
21  
  gen(l, "adjective magic", func {
22  
    lineAbove(findAdjectiveComment(findAdjective()))
23  
  });
24  
  gen(l, "complete line", func {
25  
    gSearchNeqNoSystem(func(S s) { startsWithIgnoreCase(s, gI()) })
26  
  });
27  
  gen(l, "longest prefix match -> answer", func {
28  
    lineBelow(gLongestPrefixMatchNeqIdx(gI()))
29  
  });
30  
  gen(l, "word search", func {
31  
    gSearchNeqNoSystem(func(S s) { match(gI(), s) })
32  
  });
33  
  gen(l, "raw string search", func {
34  
    gSearchNeqNoSystem(func(S s) { containsIgnoreCase(s, gI()) })
35  
  });
36  
  gen(l, "raw string search (!java)", func {
37  
    gSearchNeqNoSystem(func(S s) { s.startsWith("!java") && containsIgnoreCase(s, gI()) })
38  
  });
39  
  gen(l, "yes/no from java", func {
40  
    L<S> tok = javaTok(gI());
41  
    if (contains(tok, "t")) ret "yes";
42  
    if (contains(tok, "f")) ret "no";
43  
    null;
44  
  });
45  
  gen(l, "learned", func {
46  
    for (L<S> learn : gLearns()) {
47  
      if (l(learn) >= 2 && match(first(learn), gI()))
48  
        try answer lastNonSystemLine(learn);
49  
    }
50  
    null;
51  
  });
52  
  gen(l, "learned (single step)", func {
53  
    for (L<S> learn : gLearns()) {
54  
      for (int i = 0; i < l(learn)-1; i++)
55  
        if (match(learn.get(i), gI()))
56  
          ret learn.get(i+1);
57  
    }
58  
    null;
59  
  });
60  
  
61  
  gen(l, "learned (single step 2)", func {
62  
    thislearn: for (L<S> learn : gLearns()) {
63  
      L<S> log = gLog();
64  
      int j = l(log)-l(learn)+1; // TODO: wrong logic
65  
      if (j < 0) null;
66  
      for (int i = 0; i < l(learn)-1; i++)
67  
        if (!match(learn.get(i), log.get(j+i)))
68  
          break thislearn;
69  
      ret last(learn); // all entries match
70  
    }
71  
    null;
72  
  });
73  
  
74  
  gen(l, "learned (single step 2, with adapter)", func {
75  
    L<S> log = gLog();
76  
    S bestResult = null;
77  
    double bestValue = 0;
78  
    thislearn: for (L<S> learn : gLearns()) {
79  
      if (adapt_debug) print("Testing learn: " + struct(learn));
80  
      
81  
      // j is starting point of match in log
82  
      j: for (int j = l(log)-l(learn)+1; j < l(log); j++) pcall {
83  
        if (adapt_debug) print("\nl(log)=" + l(log) + ", j=" + j);
84  
        Adapter adapter = new WordAdapter;
85  
        int k;
86  
        for (k = j; k < l(log); k++) {
87  
          S in = learn.get(k-j), out = log.get(k);
88  
          if (adapt_debug) print(in + " => " + out);
89  
          if (!adapter.canMatch(in, out)) {
90  
            if (adapt_debug) print("Can't match");
91  
            continue j;
92  
          }
93  
          adapter = adapter.plus(in, out);
94  
          if (adapt_debug) printStructure("  ", adapter);
95  
        }
96  
        double v = adapter.size();
97  
        if (bestResult == null || v < bestValue) {
98  
          bestResult = adapter.get(learn.get(k-j));
99  
          bestValue = v;
100  
          if (adapt_debug) print("New best: " + formatDouble(bestValue, 1) + " -- " + bestResult);
101  
        }
102  
      }
103  
    }
104  
    ret bestResult;
105  
  });
106  
  
107  
  gen(l, "grow suffix", func {
108  
    S a = g1(), b = g0();
109  
    if (b.startsWith(a))
110  
      ret b + b.substring(l(a));
111  
    null;
112  
  });
113  
  
114  
  gen(l, "grow prefix", func {
115  
    S a = g1(), b = g0();
116  
    ret growPrefix(a, b);
117  
  });
118  
  
119  
  gen(l, "strip common prefix + grow prefix", func {
120  
    S a = g1(), b = g0();
121  
    int l = lCommonPrefix(a, b);
122  
    if (l == 0) null;
123  
    S s = growPrefix(a.substring(l), b.substring(l));
124  
    if (empty(s)) null;
125  
    ret a.substring(0, l) + s;
126  
  });
127  
}
128  
129  
static S findAdjective() { ret findAdjective(gI()); }
130  
131  
static S findAdjective(S s) {
132  
  ret findOneOfTheWords(s, "crazy", "nice");
133  
}
134  
135  
static int findAdjectiveComment(final S adjective) {
136  
  if (adjective == null) ret -1;
137  
  ret gSearchIdx(func(S s) {
138  
    matchStart("that's", s) && find3(adjective, s)
139  
  });
140  
}

Author comment

Began life as a copy of #1003816

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004022
Snippet name: Generators for Random v8
Eternal ID of this version: #1004022/1
Text MD5: 0dc6df15d6fe84905c1f2c67f16fe4ba
Transpilation MD5: 45850907cb05e951ff7add1597400342
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-04 18:47:10
Source code size: 4278 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 983 / 1394
Referenced in: [show references]