# -*- coding: utf-8 -*- import zlib import cPickle import os.path class Entropy: def __init__(self): self.entro = [] def register(self, name, corpus): """ register a text as corpus for a language or author. may also be a function or whatever you need to handle the result. """ corpus = str(corpus) ziplen = len(zlib.compress(corpus)) print name, ziplen self.entro.append((name, corpus, ziplen)) def guess(self, part): """ is a text that will be compared with the registered corpora and the function will return what you defined as in the registration process. """ what = None diff = 0 part = str(part) for name, corpus, ziplen in self.entro: nz = len(zlib.compress(corpus+part)) - ziplen if diff==0 or nz