Trains the classifier with the given document of the given type (i.e., class). A document can be a Document, Vector, dict, list or string. If no type is given, Document.type will be used instead.
(self, document, type=None)
| 1988 | return self._features.keys() |
| 1989 | |
| 1990 | def train(self, document, type=None): |
| 1991 | """ Trains the classifier with the given document of the given type (i.e., class). |
| 1992 | A document can be a Document, Vector, dict, list or string. |
| 1993 | If no type is given, Document.type will be used instead. |
| 1994 | """ |
| 1995 | # Calculate the probability of a class. |
| 1996 | # Calculate the probability of a feature. |
| 1997 | # Calculate the probability of a feature occuring in a class (= conditional probability). |
| 1998 | type, vector = self._vector(document, type=type) |
| 1999 | self._classes[type] = self._classes.get(type, 0) + 1 |
| 2000 | self._likelihood.setdefault(type, {}) |
| 2001 | for f, w in vector.iteritems(): |
| 2002 | if self.method == BERNOUILLI: |
| 2003 | w = 1 |
| 2004 | self._features[f] = self._features.get(f, 0) + 1 |
| 2005 | self._likelihood[type][f] = self._likelihood[type].get(f, 0) + w |
| 2006 | |
| 2007 | def classify(self, document, discrete=True): |
| 2008 | """ Returns the type with the highest probability for the given document. |
nothing calls this directly
no test coverage detected