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

62
LINES

< > BotCompany Repo | #1002221 // Idle Bot - compress some text

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

Libraryless. Click here for Pure Java version (1353L/9K/29K).

!752

/*
While idle:
  Take some text
  Try to compress it, e.g. by repeating elements
  Save result (if successful) with notes
*/

static S compressorID = "#1002225";

p {
  logOutput();
  //while true {
    pcall { doIt(); }
    sleepSeconds(1);
  //}
}

static void doIt() {
  S text = takeSomeText();
  print("Processing input, size " + toK(l(text)) + " k chars, md5: " + md5(text));
  S compression = tryToCompress(text);
  if (empty(compression))
    print("No compression found");
  else {
    boolean ok = verifyCompression(text, compression);
    if (ok) {
      print("Compression verifies.");
      print(indent(2, compression));
    } else
      print("Compression does not verify. Compressor apparently unreliable.");
    int m = l(text), n = l(compression);
    long percent = n*100L/m;
    if (n < m)
      print("Text successfully compressed to " + percent + "%");
    else
      print("Compression is equal size or bigger than original (+" + (percent-100) + "%)");
  }
}

static S takeSomeText() {
  return fromLines(repeat("Hello", 5));
}

static S tryToCompress(S text) {
  O compressor = hotwireCached(compressorID);
  ret (S) call(compressor, "compress", text);
}

static S expand(S compression) {
  L<S> lines = toLines(compression);
  new Matches m;
  assertTrue(match("expand with #*", lines.get(0), m));
  S expanderID = m.get(0);
  O expander = hotwireCached(expanderID);
  ret (S) call(expander, "expand", compression);
}

static boolean verifyCompression(S text, S compression) {
  ret toLines(text).equals(toLines(expand(compression)));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002221
Snippet name: Idle Bot - compress some text
Eternal ID of this version: #1002221/1
Text MD5: 0315428e4fc4620c94066c3814fbc88c
Transpilation MD5: b4c67182a20334d322ad063e30007ff9
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-30 00:36:46
Source code size: 1628 bytes / 62 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 982 / 880
Referenced in: [show references]