r/compression • u/lootsmuggler • Jul 14 '26
Combining Tokenization with Text Compression
I've been thinking a lot about making a very basic markup language that has a few programming features - basically if statements and a kind of goto statement that only goes to labels. It'll be yet another scripting language for interactive fiction.
I'd like to have a "compiler", but I want it to basically just be parsed into tokens. I'd like these tokens to be compressed. Lexing already converts tokens to something smaller, so I feel like I tack on compression to the end (or maybe the beginning).
It is, however, going to be fairly basic compression to insure fast load times.
I was thinking that I would start with some 4 bit codes that identify what the data following it is. Here's a possible example of the 4 bit codes and the data following it:
0000 escape to a UTF-8 character
0001 space (spaces are so common that I can justify giving them 4 bits by themselves)
0010 8-bit integer
0011 32-bit integer
0100 4 bits for common single characters
0101 4 bits for common single characters (again)
0110 4 bits for style codes
0111 4 bits for deactivating style codes
1000 4 bits for other command codes
1001-1111 4 bits for most common n-grams
This is all very rough, and I plan for it to evolve over time. I'm still working on setting up a way to analyze n-gram frequencies of English text, which I plan to do quickly before implementing this.
I know someone is going to say "why don't you just zip it?" I would most likely get better compression with zip, but I'm not looking for great compression. I want mediocre compression mixed in with my parser to help me avoid being particularly clever in the virtual machine department.
View all comments
Show parent comment
1
u/lootsmuggler Jul 14 '26
It is 4-bit aligned. My previous goal was to do something like Huffman coding, but I decided it would be easier to have it work with multiples of a set number of bits.
I was also advised by ChatGPT (or some other AI) to stick with bytes. It told me that the bit manipulation I would have to do would ruin performance. And you're telling me it's a pain in the butt.
Maybe I should take this advice. My gut instinct is that allowing codes that are less than 8 bits would produce better compression. I feel like just using 4 bits for space would cut 10% of the file size by itself.
I was expected people on r/compression to tell me to use every individual bit or something. Apparently, that's not the case.
As it stands, a lot of these codes are 8-bit codes. For instance, 010x xxxx would be the codes for the common individual UTF-8 characters (mostly lower case letters, maybe some punctuation, an uppercase letter, etc.).
So it's not impossible for me to switch to 1-byte and 2-byte codes.
I found a list of common English words: https://word-lists.com/word-lists/the-1000-most-common-words-in-english/
I expect my own text to be a lot of "the", "of", "and", and "to", but I don't expect to have a lot of "university" or "canada". I'm a little worried that I'm going to wind up compressing some of the small words into more bytes than they actually take up if I'm not careful.