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

1  
!752
2  
3  
/*
4  
While idle:
5  
  Take some text
6  
  Try to compress it, e.g. by repeating elements
7  
  Save result (if successful) with notes
8  
*/
9  
10  
static S compressorID = "#1002225";
11  
12  
p {
13  
  logOutput();
14  
  //while true {
15  
    pcall { doIt(); }
16  
    sleepSeconds(1);
17  
  //}
18  
}
19  
20  
static void doIt() {
21  
  S text = takeSomeText();
22  
  print("Processing input, size " + toK(l(text)) + " k chars, md5: " + md5(text));
23  
  S compression = tryToCompress(text);
24  
  if (empty(compression))
25  
    print("No compression found");
26  
  else {
27  
    boolean ok = verifyCompression(text, compression);
28  
    if (ok) {
29  
      print("Compression verifies.");
30  
      print(indent(2, compression));
31  
    } else
32  
      print("Compression does not verify. Compressor apparently unreliable.");
33  
    int m = l(text), n = l(compression);
34  
    long percent = n*100L/m;
35  
    if (n < m)
36  
      print("Text successfully compressed to " + percent + "%");
37  
    else
38  
      print("Compression is equal size or bigger than original (+" + (percent-100) + "%)");
39  
  }
40  
}
41  
42  
static S takeSomeText() {
43  
  return fromLines(repeat("Hello", 5));
44  
}
45  
46  
static S tryToCompress(S text) {
47  
  O compressor = hotwireCached(compressorID);
48  
  ret (S) call(compressor, "compress", text);
49  
}
50  
51  
static S expand(S compression) {
52  
  L<S> lines = toLines(compression);
53  
  new Matches m;
54  
  assertTrue(match("expand with #*", lines.get(0), m));
55  
  S expanderID = m.get(0);
56  
  O expander = hotwireCached(expanderID);
57  
  ret (S) call(expander, "expand", compression);
58  
}
59  
60  
static boolean verifyCompression(S text, S compression) {
61  
  ret toLines(text).equals(toLines(expand(compression)));
62  
}

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