Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.mahout.classifier.bayes.algorithm;
 
 import java.util.List;
 import java.util.Map;
 
 
 public class CBayesAlgorithm implements Algorithm {
 
   @Override
   public ClassifierResult classifyDocument(String[] document,
       Datastore datastoreString defaultCategory)
       throws InvalidDatastoreException {
     ClassifierResult result = new ClassifierResult(defaultCategory);
     double max = .;
     Collection<Stringcategories = datastore.getKeys("labelWeight");
 
     for (String category : categories) {
       double prob = documentWeight(datastorecategorydocument);
       if (max < prob) {
         max = prob;
         result.setLabel(category);
       }
     }
     result.setScore(max);
     return result;
   }
 
   @Override
   public ClassifierResult[] classifyDocument(String[] document,
       Datastore datastoreString defaultCategoryint numResults)
       throws InvalidDatastoreException {
     Collection<Stringcategories = datastore.getKeys("labelWeight");
         new PriorityQueue<ClassifierResult>(numResultsnew ByScoreLabelResultComparator());
     for (String category : categories) {
       double prob = documentWeight(datastorecategorydocument);
       if (prob > 0.0) {
         pq.add(new ClassifierResult(categoryprob));
         if (pq.size() > numResults) {
           pq.remove();
         }
       }
     }
 
     if (pq.isEmpty()) {
       return new ClassifierResult[] { new ClassifierResult(defaultCategory, 0.0) };
     } else {
       List<ClassifierResultresult = new ArrayList<ClassifierResult>(pq.size());
       while (pq.isEmpty() == false) {
         result.add(pq.remove());
       }
       Collections.reverse(result);
       return result.toArray(new ClassifierResult[pq.size()]);
     }
   }
 
   @Override
   public double featureWeight(Datastore datastoreString labelString feature)
       throws InvalidDatastoreException {
 
     double result = datastore.getWeight("weight"featurelabel);
     double vocabCount = datastore.getWeight("sumWeight""vocabCount");
 
     double sigma_j = datastore.getWeight("weight"feature"sigma_j");
     double sigma_jSigma_k = datastore.getWeight("sumWeight""sigma_jSigma_k");
     double sigma_k = datastore.getWeight("labelWeight"label);
 
     double thetaNormalizer = datastore.getWeight("thetaNormalizer"label);
 
     double numerator = sigma_j - result + datastore.getWeight("params""alpha_i");
     double denominator = (sigma_jSigma_k - sigma_k + vocabCount);
     
    double weight = Math.log(numerator / denominator);
    //System.out.println(feature + " " + label+ "\t" +result + " " + vocabCount + " " + sigma_j + " " + sigma_k+ " " + sigma_jSigma_k+ " " + thetaNormalizer + " "+numerator + " " +denominator);
    
    result = weight / thetaNormalizer;
    
    return result;
  }
  public void initialize(Datastore datastorethrows InvalidDatastoreException {
    datastore.getWeight("weight""test""test");
    datastore.getWeight("labelWeight""test");
    datastore.getWeight("thetaNormalizer""test");
  }
  public double documentWeight(Datastore datastoreString label,
      String[] documentthrows InvalidDatastoreException {
    Map<Stringint[]> wordList = new HashMap<Stringint[]>(1000);
    for (String word : document) {
      int[] count = wordList.get(word);
      if (count == null) {
        count = new int[] { 0 };
        wordList.put(wordcount);
      }
      count[0]++;
    }
    double result = 0.0;
    for (Map.Entry<Stringint[]> entry : wordList.entrySet()) {
      String word = entry.getKey();
      int count = entry.getValue()[0];
      result += count * featureWeight(datastorelabelword);
    }
    return result;
  }
  public Collection<StringgetLabels(Datastore datastore)
      throws InvalidDatastoreException {
    return datastore.getKeys("labelWeight");
  }
New to GrepCode? Check out our FAQ X