'Returns the probability that a document with the given word is of the given type.'

def posterior_probability(self, word, type):

    if (not self._pp):
        p1 = defaultdict(float)
        p2 = defaultdict((lambda : defaultdict(float)))
        p3 = defaultdict(float)
        p4 = defaultdict(float)
        for d in self.documents:
            p1[d.type] += 1
        for d in self.documents:
            for f in d.terms:
                p2[f][d.type] += (1 / p1[d.type])
                p3[f] += 1
        for t in p1:
            for f in p3:
                p4[(f, t)] = ((p1[t] * p2[f][t]) / p3[f])
        self._pp = p4
    return self._pp[(word, type)]
