def generate_ngram(text, n=3):
    """
    Generate n-gram frequency table for given text.
    """
    occurences = ngram = dict()
    for i in range(len(text) - n):
        try:
            cur = text[i:i+n]
            if cur in occurences:
                occurences[cur] += 1
            else:
                occurences[cur] = 1
        except IndexError:
            pass

    for (key,value) in occurences.items():
        ngram[key] = float(value) / (len(text) - n + 1)

    return ngram