‹ BackHN Continuity

Thread

Can gzip be a language model?

414 points · 165 comments · networked

  1. jll29 · · focus · HN ↗
    Yes: you can classify a test file by topic with gzip as follows:

      gzip -9 sports.txt   testfile.txt
    
      gzip -9 politics.txt testfile.txt
    
      gzip -9 business.txt testfile.txt
    
    (ass. sports.txt politics.txt and business.txt are text docs pertaining from the sports, politics and business domains, respectively, and have equal size)

    The test file belongs to the topic with the smallest size *.gz file.

    Witten's group at Waikato uni were perhaps the first to work on this.

    Also check out the Hutter prize if you are interested in this.

    1. stingraycharles · · focus · HN ↗
      Back in the day - maybe two decades ago - I implemented language detection like this.

      I seeded gzip compressors’ dictionaries with Wikipedia articles in different languages.

      I would then try to use said dictionaries on any random text, and the one that was best able to compress it, was the correct language.

      Absolutely totally not the best approach, but very fast and super simple to implement.

      1. ape4 · · focus · HN ↗
        Or maybe make a list of the most used 1000 words in each language. And see which list has the most occurrences.
        1. wongarsu · · focus · HN ↗
          That requires you to decide what a "word" is, which is not trivial (if you think that ignoring punctuation gets you to a clean "letters surrounded by spaces" you will get lots of issues with various Asian languages)

          Also some languages have a lot of prefixes and suffixes on their verbs or even nouns, which dilutes your list of 1000 words by just adding the same common words over and over again with different suffixes designating grammatical tense, grammatical gender, etc.

          The gzip version sounds more general and more obviously correct

          1. thesz · · focus · HN ↗
            Byte Pair Encoding [1] will be different for different languages. Application of the per-language BPEs to the input text will produce encodings with different lengths.

            [1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Byte-pair_encoding" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Byte-pair_encoding

            It naturally takes care of common prefixes and suffixes.

            It is easy and fast to apply using radix tree or with finite automata. Even without radix tree, it is possible to have processing speed in the range of hundredths of thousands of bytes per second.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.