1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.bordeaux.learning;
19
20import java.util.HashMap;
21import java.util.Map;
22import android.util.Log;
23
24/**
25 * A simple impelentation of histograms with sparse enteries using HashMap.
26 * User can push examples or extract probabilites from this histogram.
27 */
28public class predictorHist {
29    private HashMap<String, Integer> mCountHist;
30    private int mSampleCount;
31    String TAG = "PredicrtHist";
32
33    public predictorHist() {
34        mCountHist = new HashMap<String, Integer>();
35        mSampleCount = 0;
36    }
37
38    // reset histogram
39    public void ResetPredictorHist() {
40        mCountHist.clear();
41        mSampleCount = 0;
42    }
43
44    // getters
45    public final HashMap<String, Integer> getHist() {
46        return mCountHist;
47    }
48
49    public int getHistCounts() {
50        return mSampleCount;
51    }
52
53    //setter
54    public void set(HashMap<String, Integer> hist) {
55        ResetPredictorHist();
56        for (Map.Entry<String, Integer> x : hist.entrySet()) {
57            mCountHist.put(x.getKey(), x.getValue());
58            mSampleCount = mSampleCount + x.getValue();
59        }
60    }
61
62    /**
63     * pushes a new example to the histogram
64     */
65    public void pushSample( String fs) {
66        int histValue = 1;
67        if (mCountHist.get(fs) != null )
68            histValue = histValue + mCountHist.get(fs);
69        mCountHist.put(fs,histValue);
70        mSampleCount++;
71    }
72
73    /**
74     * return probabilty of an exmple using the histogram
75     */
76    public float getProbability(String fs) {
77        float res = 0;
78        if (mCountHist.get(fs) != null )
79            res = ((float) mCountHist.get(fs)) / ((float)mSampleCount);
80        return res;
81    }
82}
83