'Returns the chi-squared p-value (0.0-1.0) for the given feature.
When p < 0.05, the feature is biased to a class (document type),
i.e., it is a significant predictor for that class.'

def chi_squared(self, word):

    if (not self._x2):
        from pattern.metrics import chi2
        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[d.type][f] += 1
                p3[f] += 1
        for f in p3:
            p4[f] = chi2(observed=[[p2[t][f] for t in p2], [(p1[t] - p2[t][f]) for t in p2]])[1]
        self._x2 = p4
    return self._x2[word]
