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

148
LINES

< > BotCompany Repo | #1004039 // Generators for Random v9

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

Libraryless. Click here for Pure Java version (6760L/47K/159K).

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

Author comment

Began life as a copy of #1004022

download  show line numbers  debug dex  old transpilations   

Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, etmzoiygucik, etryasgzbotu, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, vqftxbmjmmzu, xprdwmaupziu

No comments. add comment

Snippet ID: #1004039
Snippet name: Generators for Random v9
Eternal ID of this version: #1004039/1
Text MD5: 4ed6242af36dc7a2aa2eaeea5b9f2afd
Transpilation MD5: dd4ddb7e95fa986ca4a5d3ddcfaa7b28
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-18 20:27:49
Source code size: 4545 bytes / 148 lines
Pitched / IR pitched: No / No
Views / Downloads: 719 / 2181
Referenced in: [show references]