• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008-2009 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.gesture;
18 
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.TreeMap;
23 
24 /**
25  * An implementation of an instance-based learner
26  */
27 
28 class InstanceLearner extends Learner {
29     private static final Comparator<Prediction> sComparator = new Comparator<Prediction>() {
30         public int compare(Prediction object1, Prediction object2) {
31             double score1 = object1.score;
32             double score2 = object2.score;
33             if (score1 > score2) {
34                 return -1;
35             } else if (score1 < score2) {
36                 return 1;
37             } else {
38                 return 0;
39             }
40         }
41     };
42 
43     @Override
classify(int sequenceType, int orientationType, float[] vector)44     ArrayList<Prediction> classify(int sequenceType, int orientationType, float[] vector) {
45         ArrayList<Prediction> predictions = new ArrayList<Prediction>();
46         ArrayList<Instance> instances = getInstances();
47         int count = instances.size();
48         TreeMap<String, Double> label2score = new TreeMap<String, Double>();
49         for (int i = 0; i < count; i++) {
50             Instance sample = instances.get(i);
51             if (sample.vector.length != vector.length) {
52                 continue;
53             }
54             double distance;
55             if (sequenceType == GestureStore.SEQUENCE_SENSITIVE) {
56                 distance = GestureUtils.minimumCosineDistance(sample.vector, vector, orientationType);
57             } else {
58                 distance = GestureUtils.squaredEuclideanDistance(sample.vector, vector);
59             }
60             double weight;
61             if (distance == 0) {
62                 weight = Double.MAX_VALUE;
63             } else {
64                 weight = 1 / distance;
65             }
66             Double score = label2score.get(sample.label);
67             if (score == null || weight > score) {
68                 label2score.put(sample.label, weight);
69             }
70         }
71 
72 //        double sum = 0;
73         for (String name : label2score.keySet()) {
74             double score = label2score.get(name);
75 //            sum += score;
76             predictions.add(new Prediction(name, score));
77         }
78 
79         // normalize
80 //        for (Prediction prediction : predictions) {
81 //            prediction.score /= sum;
82 //        }
83 
84         Collections.sort(predictions, sComparator);
85 
86         return predictions;
87     }
88 }
89