Show
Ignore:
Timestamp:
08/21/07 19:12:16 (11 months ago)
Author:
Tarek Ziad?? <tarek@…>
Message:

fixes

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • classifier/classifier.py

    r159 r162  
    3131        self.backend = backend 
    3232        self.tokenizer = tokenizer 
     33        self._learnt = True 
     34        self._probs = None 
     35 
    3336        if options is None: 
    3437            self.options = {'lang': self.language} 
     
    4245        wich means: store words in categories 
    4346        """ 
     47        self._learnt = True 
    4448        self.backend.add_category(name=category) 
    4549        data = self.tokenizer.transform(data, self.options) 
     
    5256        wich means: remove words from categories 
    5357        """ 
     58        self._learnt = True 
    5459        data = self.tokenizer.transform(data, self.options) 
    5560        for element in data: 
    56             self.backend.delWord(element, category) 
     61            self.backend.del_word(element, category) 
    5762 
    5863    def guess(self, data): 
     
    6267 
    6368        # XXX this will be cached 
    64         probabilities = self._buildWordProbabilities() 
     69        if self._learnt: 
     70            self._probs = self._buildWordProbabilities() 
     71        else: 
     72            if self._probs is None: 
     73                self._probs = self._buildWordProbabilities() 
     74 
     75        probabilities = self._probs 
     76        self._learnt = False 
    6577 
    6678        res = {} 
     
    124136    def _buildWordProbabilities(self, language=None): 
    125137        probs = {} 
     138        corpus_size = self.corpusSize(language) 
     139        words = list(self.backend.list_words(language, complete=True)) 
    126140        for cat in self.backend.list_categories(): 
    127             probs[cat] = self._buildCategoryWordProbabilities(cat, language) 
     141 
     142            probs[cat] = self._buildCategoryWordProbabilities(cat, language, 
     143                                                              corpus_size, words) 
    128144        return probs 
    129145 
    130     def _buildCategoryWordProbabilities(self, category, language=None): 
     146    def _buildCategoryWordProbabilities(self, category, language=None, 
     147                                        corpus_size=None, words=None): 
    131148        """Merges corpora and computes probabilities 
    132149 
    133150        XXX to be cached later (invalidation on word adding) 
    134151        """ 
     152        if corpus_size is None: 
     153            corpus_size = self.corpusSize(language) 
     154        if words is None: 
     155            words = self.backend.list_words(language, complete=True) 
    135156        if language is None: 
    136157            language = self.language 
    137         corpus_size = self.corpusSize(language) 
     158 
    138159        category_size = float(self.categorySize(category, language)) 
    139160        them_count = float(max(corpus_size - category_size, 1)) 
    140161        probabilities = {} 
    141         words = self.backend.list_words(language, complete=True) 
    142162 
    143163        for word in words: 
    144             if category not in word[1][1].keys(): 
     164            the_word = word[1][1] 
     165 
     166            if category not in the_word.keys(): 
    145167                continue 
    146168 
     
    149171                continue 
    150172 
    151             cat_word_count = float(word[1][1][category]) 
    152             other_count = cat_word_count - word_count 
     173            cat_word_count = float(the_word[category]) 
     174            other_count = word_count - cat_word_count 
    153175 
    154176            if category_size == 0: