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

93
LINES

< > BotCompany Repo | #1033064 // RecognitionByCompression_v1

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

Libraryless. Click here for Pure Java version (13580L/86K).

/*
(up to date version of the text is on discord)

"Recognition By Compression"

Components:

-INPUT DATA - e. g. an image to compress
-It is a SEARCH for the best COMPRESSOR of the INPUT DATA
-a CURRENT BEST COMPRESSED VERSION of the input data [possibly updated when improvements are made]
-a COMPRESSED VERSION of data x is a PIECE OF CODE computing x
-a COMPRESSED LOSSY VERSION of data x is a PIECE OF CODE computing y
     with diff(x, y) small
-SEARCH STRATEGIES (generators of ever improving scored compressed versions for some input data)
-Compression SCORERS (let's say simply by the size of the compressed version; this is for losslesss compressors)
   (LOSSY compressions are difficult to score, TBD)

Later:
-SEMANTIC LINKERS that make sense of a compressed version of an image 
*/

sclass RecognitionByCompression_v1 extends Meta {
  interface CompressionRegime<A> {
    byte[] decompressorToBytes(A decompressor);
    A decompressorFromBytes(byte[] compressed);
    byte[] runDecompressor(A a);
    
    default byte[] decompress(byte[] compressed) {
      ret runDecompressor(decompressorFromBytes(compressed));
    }
  }
  
  class CompressionRegime_GZEval implements CompressionRegime<S> {
    public byte[] decompressorToBytes(S decompressor) {
      ret gzipBytes(toUtf8(decompressor));
    }
    
    public S decompressorFromBytes(byte[] compressed) {
      ret fromUtf8(gunzipBytes(compressed));
    }
    
    public byte[] runDecompressor(S code) {
      ret toByteArray(dm_javaEval(code));
    }
  }
  
  class CompressorSearch<A> {
    class Submission {
      void setDecompressor(A decompressor) {
        this.decompressor_cache = decompressor;
      }
      
      simplyCached byte[] compressed() {
        ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache);
      }
      
      simplyCached A decompressor() {
        ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache);
      }
      
      int compressedSize() { ret l(compressed()); }
      
      simplyCached double score() {
        ret doubleRatio(inputDataSize(), compressedSize());
      }
      
      simplyCached Bool correct() {
        ret byteArraysEqual(regime.runDecompressor(decompressor()), inputData);
      }
    }
    
    CompressionRegime<A> regime;
    byte[] inputData;
    new Best<Submission> best;
    
    *(CompressionRegime<A> *regime) {}
    *(CompressionRegime<A> *regime, byte[] *inputData) {}
    
    int inputDataSize() { ret l(inputData); }
    
    // can submit concurrently!
    void submit(A compression) {
      if (compression == null) ret;
      new Submission s;
      s.setDecompressor(compression);
      if (s.score() > best.score())
        if (!s.correct())
          warn("Compressor didn't verify");
        else
          best.put(s, s.score());
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): bhatertpkbcr, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033064
Snippet name: RecognitionByCompression_v1
Eternal ID of this version: #1033064/23
Text MD5: 7bd00f6c97a4427d2ea7d89c2d805891
Transpilation MD5: 9c2bf0840d4f8d80b91a66438c6d52c7
Author: stefan
Category: javax / compression /pattern recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-12 23:11:43
Source code size: 2945 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 182 / 268
Version history: 22 change(s)
Referenced in: [show references]