from collections import defaultdict

class CharNgramTrainer:

    def __init__(self, n=3):
        self.n = n
        self.chargrams = defaultdict(int)

    def update(self, word):
        for i in range(len(word)-self.n+1):
            gram = word[i:i+self.n]
            self.chargrams[gram] += 1

    def finalize(self):
        return dict(self.chargrams)