"Priming" or "Training" a compression algorithm to be used for compression/decompression?

compression

Solution

What you are describing, in the parlance of most compression algorithms, would be a preset dictionary for the compressor.

I can't speak for all compression libraries, but zlib definitely supports this -- in the exact way you're imagining -- via the `deflateSetDictionary()` and `inflateSetDictionary()` functions. See the zlib manual for details.

Problem

I'm trying to work out if there is a compression algorithm that can be trained beforehand, where you can use the trained data to compress and decompress data. I don't know exactly how compression algorithms work, but I have an inkling that this is possible. For example, if I compress these lines independently, it wouldn't compress very well. ``` banana: 1, tree: 2, frog: 3 banana: 7, tree: 9, elephant: 10 ``` If I train the compression algorithm with 100's of sample lines beforehand, it would compress very well because it already has a way of mapping "banana" into a code/lookup value. Pseudocode to help explain my question: ``` # Compressing side rip = Rip() trained = rip.train(data) # once off send_trained_data_to_clients(trained) compressed = rip.compress(data) # And on the other end rip = Rip() rip.load_train_data(train) data = rip.decompress(compressed) ``` Is there a common (i.e. has libraries for popular languages) compression algorithm that let's me do this?

Original source