Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 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 | |
| 17 | package android.bordeaux.services; |
| 18 | |
| 19 | import android.bordeaux.services.ILearning_StochasticLinearRanker; |
| 20 | import android.bordeaux.services.StringFloat; |
| 21 | import android.content.Context; |
| 22 | import android.os.RemoteException; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | import java.util.HashMap; |
| 28 | import 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 | */ |
| 39 | public 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 | |
saberian | b019e89 | 2012-04-19 11:33:44 -0700 | [diff] [blame] | 56 | public boolean retrieveRanker() { |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 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 | |
saberian | b019e89 | 2012-04-19 11:33:44 -0700 | [diff] [blame] | 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 | |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 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 | |
saberian | b019e89 | 2012-04-19 11:33:44 -0700 | [diff] [blame] | 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 | } |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 127 | } |
| 128 | |
saberian | b019e89 | 2012-04-19 11:33:44 -0700 | [diff] [blame] | 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) { |
saberian | 984e52f | 2012-06-05 18:23:53 -0700 | [diff] [blame^] | 135 | Log.e(TAG,"Exception: Setting Parameter"); |
saberian | b019e89 | 2012-04-19 11:33:44 -0700 | [diff] [blame] | 136 | throw new RuntimeException(RANKER_NOTAVAILABLE); |
| 137 | } |
Wei Hua | 6b4eebc | 2012-03-09 10:24:16 -0800 | [diff] [blame] | 138 | } |
| 139 | } |