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) {} }