1/* 2 * Copyright (C) 2012 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 17package android.bordeaux.services; 18 19import android.bordeaux.services.ILearning_MulticlassPA; 20import android.bordeaux.services.IntFloat; 21import android.content.Context; 22import android.os.RemoteException; 23import android.util.Log; 24 25import java.util.ArrayList; 26import java.util.List; 27import java.util.HashMap; 28import java.util.Map; 29 30/** Classifier for the Learning framework. 31 * For training: call trainOneSample 32 * For classifying: call classify 33 * Data is represented as sparse key, value pair. And key is an integer, value 34 * is a float. Class label(target) for the training data is an integer. 35 * Note: since the actual classifier is running in a remote the service. 36 * Sometimes the connection may be lost or not established. 37 * 38 */ 39public class BordeauxClassifier { 40 static final String TAG = "BordeauxClassifier"; 41 private Context mContext; 42 private String mName; 43 private ILearning_MulticlassPA mClassifier; 44 private ArrayList<IntFloat> getArrayList(final HashMap<Integer, Float> sample) { 45 ArrayList<IntFloat> intfloat_sample = new ArrayList<IntFloat>(); 46 for (Map.Entry<Integer, Float> x : sample.entrySet()) { 47 IntFloat v = new IntFloat(); 48 v.index = x.getKey(); 49 v.value = x.getValue(); 50 intfloat_sample.add(v); 51 } 52 return intfloat_sample; 53 } 54 55 private boolean retrieveClassifier() { 56 if (mClassifier == null) 57 mClassifier = BordeauxManagerService.getClassifier(mContext, mName); 58 // if classifier is not available, return false 59 if (mClassifier == null) { 60 Log.i(TAG,"Classifier not available."); 61 return false; 62 } 63 return true; 64 } 65 66 public BordeauxClassifier(Context context) { 67 mContext = context; 68 mName = "defaultClassifier"; 69 mClassifier = BordeauxManagerService.getClassifier(context, mName); 70 } 71 72 public BordeauxClassifier(Context context, String name) { 73 mContext = context; 74 mName = name; 75 mClassifier = BordeauxManagerService.getClassifier(context, mName); 76 } 77 78 public boolean update(final HashMap<Integer, Float> sample, int target) { 79 if (!retrieveClassifier()) 80 return false; 81 try { 82 mClassifier.TrainOneSample(getArrayList(sample), target); 83 } catch (RemoteException e) { 84 Log.e(TAG,"Exception: training one sample."); 85 return false; 86 } 87 return true; 88 } 89 90 public int classify(final HashMap<Integer, Float> sample) { 91 // if classifier is not available return -1 as an indication of fail. 92 if (!retrieveClassifier()) 93 return -1; 94 try { 95 return mClassifier.Classify(getArrayList(sample)); 96 } catch (RemoteException e) { 97 Log.e(TAG,"Exception: classify the sample."); 98 // return an invalid number. 99 // TODO: throw exception. 100 return -1; 101 } 102 } 103} 104