Home | History | Annotate | Download | only in services
      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.learning.StochasticLinearRanker;
     20 import android.bordeaux.services.IBordeauxLearner.ModelChangeCallback;
     21 import android.os.IBinder;
     22 import android.util.Log;
     23 import java.util.List;
     24 import java.util.ArrayList;
     25 import java.io.*;
     26 import java.lang.ClassNotFoundException;
     27 import java.util.Arrays;
     28 import java.util.ArrayList;
     29 import java.util.List;
     30 import java.util.Scanner;
     31 import java.io.ByteArrayOutputStream;
     32 import java.util.HashMap;
     33 import java.util.Map;
     34 
     35 public class Learning_StochasticLinearRanker extends ILearning_StochasticLinearRanker.Stub
     36         implements IBordeauxLearner {
     37 
     38     private final String TAG = "ILearning_StochasticLinearRanker";
     39     private StochasticLinearRankerWithPrior mLearningSlRanker = null;
     40     private ModelChangeCallback modelChangeCallback = null;
     41 
     42     public Learning_StochasticLinearRanker(){
     43     }
     44 
     45     public void ResetRanker(){
     46         if (mLearningSlRanker == null)
     47             mLearningSlRanker = new StochasticLinearRankerWithPrior();
     48         mLearningSlRanker.resetRanker();
     49     }
     50 
     51     public boolean UpdateClassifier(List<StringFloat> sample_1, List<StringFloat> sample_2){
     52         ArrayList<StringFloat> temp_1 = (ArrayList<StringFloat>)sample_1;
     53         String[] keys_1 = new String[temp_1.size()];
     54         float[] values_1 = new float[temp_1.size()];
     55         for (int i = 0; i < temp_1.size(); i++){
     56             keys_1[i] = temp_1.get(i).key;
     57             values_1[i] = temp_1.get(i).value;
     58         }
     59         ArrayList<StringFloat> temp_2 = (ArrayList<StringFloat>)sample_2;
     60         String[] keys_2 = new String[temp_2.size()];
     61         float[] values_2 = new float[temp_2.size()];
     62         for (int i = 0; i < temp_2.size(); i++){
     63             keys_2[i] = temp_2.get(i).key;
     64             values_2[i] = temp_2.get(i).value;
     65         }
     66         if (mLearningSlRanker == null)
     67             mLearningSlRanker = new StochasticLinearRankerWithPrior();
     68         boolean res = mLearningSlRanker.updateClassifier(keys_1,values_1,keys_2,values_2);
     69         if (res && modelChangeCallback != null) {
     70             modelChangeCallback.modelChanged(this);
     71         }
     72         return res;
     73     }
     74 
     75     public float ScoreSample(List<StringFloat> sample) {
     76         ArrayList<StringFloat> temp = (ArrayList<StringFloat>)sample;
     77         String[] keys = new String[temp.size()];
     78         float[] values = new float[temp.size()];
     79         for (int i = 0; i < temp.size(); i++){
     80             keys[i] = temp.get(i).key;
     81             values[i] = temp.get(i).value;
     82         }
     83         if (mLearningSlRanker == null)
     84             mLearningSlRanker = new StochasticLinearRankerWithPrior();
     85         return mLearningSlRanker.scoreSample(keys,values);
     86     }
     87 
     88     public boolean SetModelPriorWeight(List<StringFloat> sample) {
     89         ArrayList<StringFloat> temp = (ArrayList<StringFloat>)sample;
     90         HashMap<String, Float> weights = new HashMap<String, Float>();
     91         for (int i = 0; i < temp.size(); i++)
     92             weights.put(temp.get(i).key, temp.get(i).value);
     93         if (mLearningSlRanker == null)
     94             mLearningSlRanker = new StochasticLinearRankerWithPrior();
     95         return mLearningSlRanker.setModelPriorWeights(weights);
     96     }
     97 
     98     public boolean SetModelParameter(String key, String value) {
     99         if (mLearningSlRanker == null)
    100             mLearningSlRanker = new StochasticLinearRankerWithPrior();
    101         return mLearningSlRanker.setModelParameter(key,value);
    102     }
    103 
    104     // Beginning of the IBordeauxLearner Interface implementation
    105     public byte [] getModel() {
    106         if (mLearningSlRanker == null)
    107             mLearningSlRanker = new StochasticLinearRankerWithPrior();
    108         StochasticLinearRankerWithPrior.Model model = mLearningSlRanker.getModel();
    109         try {
    110             ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    111             ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
    112             objStream.writeObject(model);
    113             //return byteStream.toByteArray();
    114             byte[] bytes = byteStream.toByteArray();
    115             return bytes;
    116         } catch (IOException e) {
    117             throw new RuntimeException("Can't get model");
    118         }
    119     }
    120 
    121     public boolean setModel(final byte [] modelData) {
    122         try {
    123             ByteArrayInputStream input = new ByteArrayInputStream(modelData);
    124             ObjectInputStream objStream = new ObjectInputStream(input);
    125             StochasticLinearRankerWithPrior.Model model =
    126                     (StochasticLinearRankerWithPrior.Model) objStream.readObject();
    127             if (mLearningSlRanker == null)
    128                 mLearningSlRanker = new StochasticLinearRankerWithPrior();
    129             boolean res = mLearningSlRanker.loadModel(model);
    130             return res;
    131         } catch (IOException e) {
    132             throw new RuntimeException("Can't load model");
    133         } catch (ClassNotFoundException e) {
    134             throw new RuntimeException("Learning class not found");
    135         }
    136     }
    137 
    138     public IBinder getBinder() {
    139         return this;
    140     }
    141 
    142     public void setModelChangeCallback(ModelChangeCallback callback) {
    143         modelChangeCallback = callback;
    144     }
    145     // End of IBordeauxLearner Interface implemenation
    146 }
    147