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

1  
/*
2  
(up to date version of the text is on discord)
3  
4  
"Recognition By Compression"
5  
6  
Components:
7  
8  
-INPUT DATA - e. g. an image to compress
9  
-It is a SEARCH for the best COMPRESSOR of the INPUT DATA
10  
-a CURRENT BEST COMPRESSED VERSION of the input data [possibly updated when improvements are made]
11  
-a COMPRESSED VERSION of data x is a PIECE OF CODE computing x
12  
-a COMPRESSED LOSSY VERSION of data x is a PIECE OF CODE computing y
13  
     with diff(x, y) small
14  
-SEARCH STRATEGIES (generators of ever improving scored compressed versions for some input data)
15  
-Compression SCORERS (let's say simply by the size of the compressed version; this is for losslesss compressors)
16  
   (LOSSY compressions are difficult to score, TBD)
17  
18  
Later:
19  
-SEMANTIC LINKERS that make sense of a compressed version of an image 
20  
*/
21  
22  
sclass RecognitionByCompression_v1 extends Meta {
23  
  interface CompressionRegime<A> {
24  
    byte[] decompressorToBytes(A decompressor);
25  
    A decompressorFromBytes(byte[] compressed);
26  
    byte[] runDecompressor(A a);
27  
    
28  
    default byte[] decompress(byte[] compressed) {
29  
      ret runDecompressor(decompressorFromBytes(compressed));
30  
    }
31  
  }
32  
  
33  
  class CompressionRegime_GZEval implements CompressionRegime<S> {
34  
    public byte[] decompressorToBytes(S decompressor) {
35  
      ret gzipBytes(toUtf8(decompressor));
36  
    }
37  
    
38  
    public S decompressorFromBytes(byte[] compressed) {
39  
      ret fromUtf8(gunzipBytes(compressed));
40  
    }
41  
    
42  
    public byte[] runDecompressor(S code) {
43  
      ret toByteArray(dm_javaEval(code));
44  
    }
45  
  }
46  
  
47  
  class CompressorSearch<A> {
48  
    class Submission {
49  
      void setDecompressor(A decompressor) {
50  
        this.decompressor_cache = decompressor;
51  
      }
52  
      
53  
      simplyCached byte[] compressed() {
54  
        ret decompressor_cache == null ?: regime.decompressorToBytes(decompressor_cache);
55  
      }
56  
      
57  
      simplyCached A decompressor() {
58  
        ret compressed_cache == null ?: regime.decompressorFromBytes(compressed_cache);
59  
      }
60  
      
61  
      int compressedSize() { ret l(compressed()); }
62  
      
63  
      simplyCached double score() {
64  
        ret doubleRatio(inputDataSize(), compressedSize());
65  
      }
66  
      
67  
      simplyCached Bool correct() {
68  
        ret byteArraysEqual(regime.runDecompressor(decompressor()), inputData);
69  
      }
70  
    }
71  
    
72  
    CompressionRegime<A> regime;
73  
    byte[] inputData;
74  
    new Best<Submission> best;
75  
    
76  
    *(CompressionRegime<A> *regime) {}
77  
    *(CompressionRegime<A> *regime, byte[] *inputData) {}
78  
    
79  
    int inputDataSize() { ret l(inputData); }
80  
    
81  
    // can submit concurrently!
82  
    void submit(A compression) {
83  
      if (compression == null) ret;
84  
      new Submission s;
85  
      s.setDecompressor(compression);
86  
      if (s.score() > best.score())
87  
        if (!s.correct())
88  
          warn("Compressor didn't verify");
89  
        else
90  
          best.put(s, s.score());
91  
    }
92  
  }
93  
}

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: 184 / 272
Version history: 22 change(s)
Referenced in: [show references]