class CharGramEmbedder:

    def __init__(self, char_model, n=3):
        self.model = char_model
        self.n = n

    def similarity(self, w1, w2):

        grams1 = {w1[i:i+self.n] for i in range(len(w1)-self.n+1)}
        grams2 = {w2[i:i+self.n] for i in range(len(w2)-self.n+1)}

        if not grams1 or not grams2:
            return 0

        intersection = len(grams1 & grams2)
        union = len(grams1 | grams2)

        return intersection / union