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

!759

!include #1004027 // WordAdapter

static bool adapt_debug;

// put func {}'s returning a string in there
// not using _log parameter anymore
static void deterministicGenerators(L<Gen> l) {
  gen(l, "plus1", func { parseLong(gI())+1 });
  gen(l, "delta", func { parseLong(g0())*2-parseLong(g1()) });
  gen(l, "delta unicode", func {
    S a = g1(), b = g0();
    if (l(a) == 1 && l(b) == 1)
      ret intToChar(charToInt(b)*2-charToInt(a));
    null;
  });
  gen(l, "exponential", func { sqr(parseLong(g0()))/parseLong(g1()) });
  gen(l, "assoc", gOneAssoc());
  gen(l, "adjective magic", func {
    lineAbove(findAdjectiveComment(findAdjective()))
  });
  gen(l, "complete line/ll", func {
    gSearchNeqNoSystem(gLongLog(), func(S s) { startsWithIgnoreCase(s, gI()) })
  });
  gen(l, "complete line", func {
    gSearchNeqNoSystem(func(S s) { startsWithIgnoreCase(s, gI()) })
  });
  gen(l, "longest prefix match -> answer", func {
    lineBelow(gLongestPrefixMatchNeqIdx(gI()))
  });
  gen(l, "word search", func {
    gSearchNeqNoSystem(func(S s) { match(gI(), s) })
  });
  gen(l, "raw string search", func {
    gSearchNeqNoSystem(func(S s) { containsIgnoreCase(s, gI()) })
  });
  gen(l, "raw string search (!java)", func {
    gSearchNeqNoSystem(func(S s) { s.startsWith("!java") && containsIgnoreCase(s, gI()) })
  });
  gen(l, "yes/no from java", func {
    L<S> tok = javaTok(gI());
    if (contains(tok, "t")) ret "yes";
    if (contains(tok, "f")) ret "no";
    null;
  });
  gen(l, "learned", func {
    for (L<S> learn : gAllLearns()) {
      if (l(learn) >= 2 && match(first(learn), gI()))
        try answer lastNonSystemLine(learn);
    }
    null;
  });
  gen(l, "learned (single step)", func {
    for (L<S> learn : gAllLearns()) {
      for (int i = 0; i < l(learn)-1; i++)
        if (match(learn.get(i), gI()))
          ret learn.get(i+1);
    }
    null;
  });
  
  gen(l, "learned (single step 2)", func {
    thislearn: for (L<S> learn : gAllLearns()) {
      L<S> log = gLog();
      int j = l(log)-l(learn)+1; // TODO: wrong logic
      if (j < 0) null;
      for (int i = 0; i < l(learn)-1; i++)
        if (!match(learn.get(i), log.get(j+i)))
          break thislearn;
      ret last(learn); // all entries match
    }
    null;
  });
  
  gen(l, "learned (single step 2, with adapter)", func {
    L<S> log = gLog();
    S bestResult = null;
    double bestValue = 0;
    thislearn: for (L<S> learn : gAllLearns()) {
      if (adapt_debug) print("Testing learn: " + struct(learn));
      
      // j is starting point of match in log
      j: for (int j = max(0, l(log)-l(learn)+1); j < l(log); j++) pcall {
        if (adapt_debug) print("\nl(log)=" + l(log) + ", j=" + j);
        Adapter adapter = new WordAdapter;
        int k;
        for (k = j; k < l(log); k++) {
          S in = learn.get(k-j), out = log.get(k);
          if (adapt_debug) print(in + " => " + out);
          if (!adapter.canMatch(in, out)) {
            if (adapt_debug) print("Can't match");
            continue j;
          }
          adapter = adapter.plus(in, out);
          if (adapt_debug) printStructure("  ", adapter);
        }
        double v = adapter.size();
        if (bestResult == null || v < bestValue) {
          bestResult = adapter.get(learn.get(k-j));
          bestValue = v;
          if (adapt_debug) print("New best: " + formatDouble(bestValue, 1) + " -- " + bestResult);
        }
      }
    }
    ret bestResult;
  });
  
  gen(l, "grow suffix", func {
    S a = g1(), b = g0();
    if (b.startsWith(a))
      ret b + b.substring(l(a));
    null;
  });
  
  gen(l, "grow prefix", func {
    S a = g1(), b = g0();
    ret growPrefix(a, b);
  });
  
  gen(l, "strip common prefix + grow prefix", func {
    S a = g1(), b = g0();
    int l = lCommonPrefix(a, b);
    if (l == 0) null;
    S s = growPrefix(a.substring(l), b.substring(l));
    if (empty(s)) null;
    ret a.substring(0, l) + s;
  });
  
  gen(l, "alternate prefix", func {
    ret commonPrefix(gN(2), gN(4));
  });

  gen(l, "rewrite", func { gStandardRewrites().get(g0()) });
}

static S findAdjective() { ret findAdjective(gI()); }

static S findAdjective(S s) {
  ret findOneOfTheWords(s, "crazy", "nice");
}

static int findAdjectiveComment(final S adjective) {
  if (adjective == null) ret -1;
  ret gSearchIdx(func(S s) {
    matchStart("that's", s) && find3(adjective, s)
  });
}

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