| 766 | KMEANS, HIERARCHICAL, ALL = "k-means", "hierarchical", "all" |
| 767 | |
| 768 | class Model(object): |
| 769 | |
| 770 | def __init__(self, documents=[], weight=TFIDF): |
| 771 | """ A model is a bag-of-word representation of a corpus of documents, |
| 772 | where each document vector is a bag of (word, relevance)-items. |
| 773 | Vectors can then be compared for similarity using a distance metric. |
| 774 | The weighting scheme can be: relative TF, TFIDF (default), BINARY, None, |
| 775 | where None means that the original weights are used. |
| 776 | """ |
| 777 | self.description = "" # Description of the dataset: author e-mail, etc. |
| 778 | self._documents = readonlylist() # List of documents (read-only). |
| 779 | self._index = {} # Document.name => Document |
| 780 | self._df = {} # Cache of document frequency per word. |
| 781 | self._cos = {} # Cache of ((d1.id, d2.id), relevance)-items (cosine similarity). |
| 782 | self._ig = {} # Cache of (word, information gain)-items. |
| 783 | self._vector = None # Cache of model vector with all the features in the model. |
| 784 | self._lsa = None # LSA matrix with reduced dimensionality. |
| 785 | self._weight = weight # Weight used in Document.vector (TF, TFIDF, BINARY or None). |
| 786 | self._update() |
| 787 | self.extend(documents) |
| 788 | |
| 789 | @property |
| 790 | def documents(self): |
| 791 | return self._documents |
| 792 | |
| 793 | @property |
| 794 | def terms(self): |
| 795 | return self.vector.keys() |
| 796 | |
| 797 | features = words = terms |
| 798 | |
| 799 | @property |
| 800 | def classes(self): |
| 801 | return list(set(d.type for d in self.documents)) |
| 802 | |
| 803 | labels = classes |
| 804 | |
| 805 | def _get_lsa(self): |
| 806 | return self._lsa |
| 807 | def _set_lsa(self, v=None): |
| 808 | self._lsa = v |
| 809 | self._update() |
| 810 | |
| 811 | lsa = property(_get_lsa, _set_lsa) |
| 812 | |
| 813 | def _get_weight(self): |
| 814 | return self._weight |
| 815 | def _set_weight(self, w): |
| 816 | self._weight = w |
| 817 | self._update() # Clear the cache. |
| 818 | |
| 819 | weight = property(_get_weight, _set_weight) |
| 820 | |
| 821 | @classmethod |
| 822 | def load(cls, path): |
| 823 | """ Loads the model from a pickle file created with Model.save(). |
| 824 | """ |
| 825 | return cPickle.load(open(path)) |
no outgoing calls
no test coverage detected
searching dependent graphs…