public void addFeature(String label, String feature) { if (!featureMap.containsKey(new FeatureKey(label, feature))) { featureMap.put(new FeatureKey(label, feature), new FeatureValue()); } else { this.updateCount(label, feature); } if (!featureCount.containsKey(feature)) { featureCount.put(feature, 1); } else { featureCount.put(feature, featureCount.get(feature) + 1); } }
// calculate p(y|x)=sum_i(alpha_i*f_i(x,y))/sum_y(sum_i(alpha_i*f_i(x,y))). public Map<String, Double> predict(List<String> x) { Map<String, Double> probMap = new HashMap<String, Double>(); double sum = 0; for (String label : labelSet) { double prob = 0; for (String feature : x) { prob += features.getModelWeight(label, feature); } prob = Math.exp(prob); probMap.put(label, prob); sum += prob; } for (String label : probMap.keySet()) { probMap.put(label, probMap.get(label) / sum); } return probMap; }