コード例 #1
0
 // A simple algorithm with majority voting
 Counter<std::string, double> fast_scores(const Example& example) {
     Counter<std::string, double> res;
     for (const auto& it: _word_counter._counter2) {
         const auto& label = it.first;
         const auto& prob_wc = it.second;
         for (const auto& word: example._words) {
             if (prob_wc.contains(word)) {
                 res.add(label, 1);
             }
         }
     }
     return res;
 }
コード例 #2
0
 Counter<std::string, double> scores(const Example& example) {
     Counter<std::string, double> res;
     bool is_any_words_matched = false;
     for (const auto& cls: _class_counter._counter) {
         const auto& label = cls.first;
         double prior = static_cast<double>(cls.second);
         res.add(label, log(prior + _class_smoothing));
         double log_denom = log(_word_totals.get(label) + _vocab_size * _word_smoothing);
         double log_prob = 0.0;
         auto prob_wc = _word_counter.get(label);
         for (const auto& word: example._words) {
             if (prob_wc.contains(word)) {
                 is_any_words_matched = true;
             }
             log_prob += log(prob_wc.get_or_else(word, 0) + _word_smoothing) - log_denom;
         }
         res.add(label, log_prob);
     }
     if (is_any_words_matched) {
         return res;
     } else {
         return Counter<std::string, double>();
     }
 }