Yields the document vector, a dictionary of (word, relevance)-items from the document. The relevance is tf, or tf * idf if the document is part of a Model. The document vector is used to calculate similarity between two documents, for example in a clustering or c
(self)
| 542 | |
| 543 | @property |
| 544 | def vector(self): |
| 545 | """ Yields the document vector, a dictionary of (word, relevance)-items from the document. |
| 546 | The relevance is tf, or tf * idf if the document is part of a Model. |
| 547 | The document vector is used to calculate similarity between two documents, |
| 548 | for example in a clustering or classification algorithm. |
| 549 | """ |
| 550 | if not self._vector: |
| 551 | # See the Vector class below = a dict with extra functionality (copy, norm). |
| 552 | # Model.weight (Tf, TFIDF, BINARY or None) defines how weights are calculated. |
| 553 | # When a document is added/deleted from a model, the cached vector is deleted. |
| 554 | if getattr(self.model, "weight", TF) not in (TF, TFIDF, BINARY): |
| 555 | w, f = None, lambda w: float(self._terms[w]) |
| 556 | if getattr(self.model, "weight", TF) == BINARY: |
| 557 | w, f = BINARY, lambda w: int(self._terms[w] > 0) |
| 558 | if getattr(self.model, "weight", TF) == TF: |
| 559 | w, f = TF, self.tf_idf |
| 560 | if getattr(self.model, "weight", TF) == TFIDF: |
| 561 | w, f = TFIDF, self.tf_idf |
| 562 | self._vector = Vector(((w, f(w)) for w in self.terms), weight=w) |
| 563 | return self._vector |
| 564 | |
| 565 | def keywords(self, top=10, normalized=True): |
| 566 | """ Returns a sorted list of (relevance, word)-tuples that are top keywords in the document. |