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

59
LINES

< > BotCompany Repo | #1034372 // AICDemo - AdaptiveIdentifierCompression

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (14669L/89K).

sclass AICDemo {
  settable S snippetID = #1034022;
  
  swappable S makeInputText() { ret loadSnippet_cached(snippetID); }
  settable S inputText;
  settable S compressedText;
  settable S decompressedText;
  
  AdaptiveIdentifierCompression aicComp;
  AdaptiveIdentifierCompression aicDecomp;
  LPairS tokenByToken; // input/compressed tokens as pairs
  bool decompressedOK;
  
  S compress() {
    if (inputText == null) inputText = makeInputText();
    aicComp = new AdaptiveIdentifierCompression;
    adapt(aicComp);
    tokenByToken = new L;
    
    new StringBuilder buf;
    for (t : javaTok(inputText)) {
      S encoded = aicComp.encode(t);
      tokenByToken.add(pair(t, encoded));
      buf.append(encoded);
    }
    compressedText = str(buf);
    
    print("Compressed " + nChars(inputText) + " => " + nChars(compressedText) + " = " + doubleRatio(l(compressedText), l(inputText)));
    ret compressedText;
  }
  
  S decompress() {
    aicDecomp = new AdaptiveIdentifierCompression;
    adapt(aicDecomp);
    new StringBuilder buf;
    for (t : javaTok(compressedText))
      buf.append(aicDecomp.decode(t));
    decompressedText = str(buf);
    
    print("Deompressed " + nChars(compressedText) + " => " + nChars(decompressedText));
    
    if (inputText != null) {
      int correct = lCommonPrefix(inputText, decompressedText);
      
      print("Decompressed text correct for " + nChars(correct) + " out of " + nChars(inputText));
      
      decompressedOK = correct == l(inputText) && correct == l(decompressedText);
      if (decompressedOK)
        print("Decompressed OK");
      else
        print("First wrong words:\n" +
          "     " + shorten(substring(inputText, correct)) + "\n" +
          "  vs " + shorten(substring(decompressedText, correct)));
    }
    ret decompressedText;
  }
  
  swappable void adapt(AdaptiveIdentifierCompression aic) {}
}

Author comment

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: 209 / 359
Version history: 12 change(s)
Referenced in: [show references]