Given a document not in the model, returns a vector in LSA concept space. This happes automatically in Model.cosine_similarity(), but it must be done explicitly for Classifier.classify() input.
(self, document)
| 1350 | return len(self.u) |
| 1351 | |
| 1352 | def transform(self, document): |
| 1353 | """ Given a document not in the model, returns a vector in LSA concept space. |
| 1354 | This happes automatically in Model.cosine_similarity(), |
| 1355 | but it must be done explicitly for Classifier.classify() input. |
| 1356 | """ |
| 1357 | if document.id in self.u: |
| 1358 | return self.u[document.id] |
| 1359 | if document.id in _lsa_transform_cache: |
| 1360 | return _lsa_transform_cache[document.id] |
| 1361 | import numpy |
| 1362 | v = self.model.vector(document) |
| 1363 | v = [v[self._terms[i]] for i in range(len(v))] |
| 1364 | v = numpy.dot(numpy.dot(numpy.linalg.inv(numpy.diag(self.sigma)), self.vt), v) |
| 1365 | v = _lsa_transform_cache[document.id] = Vector(enumerate(v)) |
| 1366 | return v |
| 1367 | |
| 1368 | # LSA cache for Model.vector_space_search() shouldn't be stored with Model.save() |
| 1369 | # (so it is a global instead of a property of the LSA class). |