'Returns the document frequency for the given word or feature.
Returns 0 if there are no documents in the model (e.g. no word frequency).
df = number of documents containing the word / number of documents.
The more occurences of the word across the model, the higher its df weight.'

def document_frequency(self, word):

    if (len(self.documents) == 0):
        return 0.0
    if (len(self._df) == 0):
        df = self._df
        for d in self.documents:
            for (w, f) in d.terms.items():
                if (f != 0):
                    df[w] = (((w in df) and (df[w] + 1)) or 1.0)
        for w in df:
            df[w] /= float(len(self.documents))
    return self._df.get(word, 0.0)
