Libraryless. Click here for Pure Java version (14669L/89K).
1 | sclass AICDemo { |
2 | settable S snippetID = #1034022; |
3 | |
4 | swappable S makeInputText() { ret loadSnippet_cached(snippetID); } |
5 | settable S inputText; |
6 | settable S compressedText; |
7 | settable S decompressedText; |
8 | |
9 | AdaptiveIdentifierCompression aicComp; |
10 | AdaptiveIdentifierCompression aicDecomp; |
11 | LPairS tokenByToken; // input/compressed tokens as pairs |
12 | bool decompressedOK; |
13 | |
14 | S compress() { |
15 | if (inputText == null) inputText = makeInputText(); |
16 | aicComp = new AdaptiveIdentifierCompression; |
17 | adapt(aicComp); |
18 | tokenByToken = new L; |
19 | |
20 | new StringBuilder buf; |
21 | for (t : javaTok(inputText)) { |
22 | S encoded = aicComp.encode(t); |
23 | tokenByToken.add(pair(t, encoded)); |
24 | buf.append(encoded); |
25 | } |
26 | compressedText = str(buf); |
27 | |
28 | print("Compressed " + nChars(inputText) + " => " + nChars(compressedText) + " = " + doubleRatio(l(compressedText), l(inputText))); |
29 | ret compressedText; |
30 | } |
31 | |
32 | S decompress() { |
33 | aicDecomp = new AdaptiveIdentifierCompression; |
34 | adapt(aicDecomp); |
35 | new StringBuilder buf; |
36 | for (t : javaTok(compressedText)) |
37 | buf.append(aicDecomp.decode(t)); |
38 | decompressedText = str(buf); |
39 | |
40 | print("Deompressed " + nChars(compressedText) + " => " + nChars(decompressedText)); |
41 | |
42 | if (inputText != null) { |
43 | int correct = lCommonPrefix(inputText, decompressedText); |
44 | |
45 | print("Decompressed text correct for " + nChars(correct) + " out of " + nChars(inputText)); |
46 | |
47 | decompressedOK = correct == l(inputText) && correct == l(decompressedText); |
48 | if (decompressedOK) |
49 | print("Decompressed OK"); |
50 | else |
51 | print("First wrong words:\n" + |
52 | " " + shorten(substring(inputText, correct)) + "\n" + |
53 | " vs " + shorten(substring(decompressedText, correct))); |
54 | } |
55 | ret decompressedText; |
56 | } |
57 | |
58 | swappable void adapt(AdaptiveIdentifierCompression aic) {} |
59 | } |
Began life as a copy of #1034371
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034372 |
Snippet name: | AICDemo - AdaptiveIdentifierCompression |
Eternal ID of this version: | #1034372/13 |
Text MD5: | 861e05aa472803b034885dca430d0ecd |
Transpilation MD5: | ee159fd1b3968f57f1c4d51504654b28 |
Author: | stefan |
Category: | javax / gazelle 22 |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-02-11 20:33:15 |
Source code size: | 1948 bytes / 59 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 211 / 363 |
Version history: | 12 change(s) |
Referenced in: | [show references] |