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_StochasticLinearRanker; 20import android.bordeaux.services.StringFloat; 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/** Ranker for the Learning framework. 31 * For training: call updateClassifier with a pair of samples. 32 * For ranking: call scoreSample to the score of the rank 33 * Data is represented as sparse key, value pair. And key is a String, value 34 * is a float. 35 * Note: since the actual ranker is running in a remote the service. 36 * Sometimes the connection may be lost or not established. 37 * 38 */ 39public class BordeauxRanker { 40 static final String TAG = "BordeauxRanker"; 41 static final String RANKER_NOTAVAILABLE = "Ranker not Available"; 42 private Context mContext; 43 private String mName; 44 private ILearning_StochasticLinearRanker mRanker; 45 private ArrayList<StringFloat> getArrayList(final HashMap<String, Float> sample) { 46 ArrayList<StringFloat> stringfloat_sample = new ArrayList<StringFloat>(); 47 for (Map.Entry<String, Float> x : sample.entrySet()) { 48 StringFloat v = new StringFloat(); 49 v.key = x.getKey(); 50 v.value = x.getValue(); 51 stringfloat_sample.add(v); 52 } 53 return stringfloat_sample; 54 } 55 56 public boolean retrieveRanker() { 57 if (mRanker == null) 58 mRanker = BordeauxManagerService.getRanker(mContext, mName); 59 // if classifier is not available, return false 60 if (mRanker == null) { 61 Log.e(TAG,"Ranker not available."); 62 return false; 63 } 64 return true; 65 } 66 67 public BordeauxRanker(Context context) { 68 mContext = context; 69 mName = "defaultRanker"; 70 mRanker = BordeauxManagerService.getRanker(context, mName); 71 } 72 73 public BordeauxRanker(Context context, String name) { 74 mContext = context; 75 mName = name; 76 mRanker = BordeauxManagerService.getRanker(context, mName); 77 } 78 79 // Update the ranker with two samples, sample1 has higher rank than 80 // sample2. 81 public boolean update(final HashMap<String, Float> sample1, 82 final HashMap<String, Float> sample2) { 83 if (!retrieveRanker()) 84 return false; 85 try { 86 mRanker.UpdateClassifier(getArrayList(sample1), getArrayList(sample2)); 87 } catch (RemoteException e) { 88 Log.e(TAG,"Exception: updateClassifier."); 89 return false; 90 } 91 return true; 92 } 93 94 public boolean reset() { 95 if (!retrieveRanker()){ 96 Log.e(TAG,"Exception: Ranker is not availible"); 97 return false; 98 } 99 try { 100 mRanker.ResetRanker(); 101 return true; 102 } catch (RemoteException e) { 103 } 104 return false; 105 } 106 107 public float scoreSample(final HashMap<String, Float> sample) { 108 if (!retrieveRanker()) 109 throw new RuntimeException(RANKER_NOTAVAILABLE); 110 try { 111 return mRanker.ScoreSample(getArrayList(sample)); 112 } catch (RemoteException e) { 113 Log.e(TAG,"Exception: scoring the sample."); 114 throw new RuntimeException(RANKER_NOTAVAILABLE); 115 } 116 } 117 118 public boolean setPriorWeight(final HashMap<String, Float> sample) { 119 if (!retrieveRanker()) 120 throw new RuntimeException(RANKER_NOTAVAILABLE); 121 try { 122 return mRanker.SetModelPriorWeight(getArrayList(sample)); 123 } catch (RemoteException e) { 124 Log.e(TAG,"Exception: set prior Weights"); 125 throw new RuntimeException(RANKER_NOTAVAILABLE); 126 } 127 } 128 129 public boolean setParameter(String key, String value) { 130 if (!retrieveRanker()) 131 throw new RuntimeException(RANKER_NOTAVAILABLE); 132 try { 133 return mRanker.SetModelParameter(key, value); 134 } catch (RemoteException e) { 135 Log.e(TAG,"Exception: Setting Parameter"); 136 throw new RuntimeException(RANKER_NOTAVAILABLE); 137 } 138 } 139} 140