Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 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 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 17 | package android.gesture; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 18 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.Collections; |
| 21 | import java.util.Comparator; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 22 | import java.util.TreeMap; |
| 23 | |
| 24 | /** |
| 25 | * An implementation of an instance-based learner |
| 26 | */ |
| 27 | |
| 28 | class InstanceLearner extends Learner { |
Romain Guy | 0e1ca57 | 2009-06-09 12:56:34 -0700 | [diff] [blame] | 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 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 43 | @Override |
Yang Li | 4758f12 | 2009-12-14 15:41:07 -0800 | [diff] [blame] | 44 | ArrayList<Prediction> classify(int sequenceType, int orientationType, float[] vector) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 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); |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 51 | if (sample.vector.length != vector.length) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 52 | continue; |
| 53 | } |
| 54 | double distance; |
Romain Guy | 0a63716 | 2009-05-29 14:43:54 -0700 | [diff] [blame] | 55 | if (sequenceType == GestureStore.SEQUENCE_SENSITIVE) { |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 56 | distance = GestureUtils.minimumCosineDistance(sample.vector, vector, orientationType); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 57 | } else { |
Romain Guy | 46c5312 | 2010-02-04 14:19:50 -0800 | [diff] [blame] | 58 | distance = GestureUtils.squaredEuclideanDistance(sample.vector, vector); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 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 | |
Romain Guy | 0e1ca57 | 2009-06-09 12:56:34 -0700 | [diff] [blame] | 72 | // double sum = 0; |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame] | 73 | for (String name : label2score.keySet()) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 74 | double score = label2score.get(name); |
Romain Guy | 0e1ca57 | 2009-06-09 12:56:34 -0700 | [diff] [blame] | 75 | // sum += score; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 76 | predictions.add(new Prediction(name, score)); |
| 77 | } |
| 78 | |
| 79 | // normalize |
Romain Guy | 0e1ca57 | 2009-06-09 12:56:34 -0700 | [diff] [blame] | 80 | // for (Prediction prediction : predictions) { |
| 81 | // prediction.score /= sum; |
| 82 | // } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 83 | |
Romain Guy | 0e1ca57 | 2009-06-09 12:56:34 -0700 | [diff] [blame] | 84 | Collections.sort(predictions, sComparator); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 85 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 86 | return predictions; |
| 87 | } |
| 88 | } |