Bladeren bron

Abstract classification aggregation

master
Chris Smith 14 jaren geleden
bovenliggende
commit
b21fcfbf28

+ 4
- 94
code/ActivityRecorder/src/uk/co/md87/android/activityrecorder/RecorderService.java Bestand weergeven

@@ -22,12 +22,12 @@ import java.io.IOException;
22 22
 import java.io.InputStream;
23 23
 import java.io.ObjectInputStream;
24 24
 import java.util.ArrayList;
25
-import java.util.HashMap;
26 25
 import java.util.List;
27 26
 import java.util.Map;
28 27
 
29 28
 import uk.co.md87.android.activityrecorder.rpc.ActivityRecorderBinder;
30 29
 import uk.co.md87.android.activityrecorder.rpc.Classification;
30
+import uk.co.md87.android.common.Aggregator;
31 31
 
32 32
 /**
33 33
  *
@@ -35,47 +35,7 @@ import uk.co.md87.android.activityrecorder.rpc.Classification;
35 35
  */
36 36
 public class RecorderService extends Service {
37 37
 
38
-    static final double DELTA = 0.25;
39
-    static final double THRESHOLD = 0.5;
40
-
41
-    private final HashMap<String, HashMap<String, Double>> scores
42
-            = new HashMap<String, HashMap<String, Double>>() {{
43
-        put("", new HashMap<String, Double>() {{
44
-            put("null", 0.5d);
45
-            put("CLASSIFIED", 0.5d);
46
-        }});
47
-
48
-        put("CLASSIFIED", new HashMap<String, Double>() {{
49
-            put("null", 0.2d);
50
-            put("DANCING", 0.2d);
51
-            put("WALKING", 0.2d);
52
-            put("VEHICLE", 0.2d);
53
-            put("IDLE", 0.2d);
54
-        }});
55
-
56
-        put("CLASSIFIED/WALKING", new HashMap<String, Double>() {{
57
-            put("null", 0.5d);
58
-            put("STAIRS", 0.5d);
59
-        }});
60
-
61
-        put("CLASSIFIED/VEHICLE", new HashMap<String, Double>() {{
62
-            put("null", 0.333d);
63
-            put("CAR", 0.333d);
64
-            put("BUS", 0.333d);
65
-        }});
66
-
67
-        put("CLASSIFIED/IDLE", new HashMap<String, Double>() {{
68
-            put("null", 0.333d);
69
-            put("STANDING", 0.333d);
70
-            put("SITTING", 0.333d);
71
-        }});
72
-
73
-        put("CLASSIFIED/WALKING/STAIRS", new HashMap<String, Double>() {{
74
-            put("null", 0.333d);
75
-            put("UP", 0.333d);
76
-            put("DOWN", 0.333d);
77
-        }});
78
-    }};
38
+    private final Aggregator aggregator = new Aggregator();
79 39
 
80 40
     private final ActivityRecorderBinder.Stub binder = new ActivityRecorderBinder.Stub() {
81 41
 
@@ -230,26 +190,9 @@ public class RecorderService extends Service {
230 190
     }
231 191
 
232 192
     void updateScores(final String classification) {
233
-        String path = "";
193
+        aggregator.addClassification(classification);
234 194
 
235
-        for (String part : classification.split("/")) {
236
-            if (!scores.containsKey(path)) {
237
-                throw new RuntimeException("Path not found: " + path
238
-                        + " (classification: " + classification + ")");
239
-            }
240
-
241
-            updateScores(scores.get(path), part);
242
-            path = path + (path.length() == 0 ? "" : "/") + part;
243
-        }
244
-
245
-        if (scores.containsKey(path)) {
246
-            // This classification has children which we're not using
247
-            // e.g. we've received CLASSIFIED/WALKING, but we're not walking
248
-            //      up or down stairs
249
-            updateScores(scores.get(path), "null");
250
-        }
251
-
252
-        final String best = getClassification();
195
+        final String best = aggregator.getClassification();
253 196
 
254 197
         if (!classifications.isEmpty() && best.equals(classifications
255 198
                     .get(classifications.size() - 1).getClassification())) {
@@ -259,39 +202,6 @@ public class RecorderService extends Service {
259 202
         }
260 203
     }
261 204
 
262
-    String getClassification() {
263
-        String path = "";
264
-
265
-        do {
266
-            final Map<String, Double> map = scores.get(path);
267
-            double best = THRESHOLD;
268
-            String bestPath = "null";
269
-
270
-            for (Map.Entry<String, Double> entry : map.entrySet()) {
271
-                if (entry.getValue() >= best) {
272
-                    best = entry.getValue();
273
-                    bestPath = entry.getKey();
274
-                }
275
-            }
276
-
277
-            path = path + (path.length() == 0 ? "" : "/") + bestPath;
278
-        } while (scores.containsKey(path));
279
-
280
-        return path.replaceAll("(^CLASSIFIED)?/?null$", "");
281
-    }
282
-
283
-    void updateScores(final Map<String, Double> map, final String target) {
284
-        for (Map.Entry<String, Double> entry : map.entrySet()) {
285
-            //Log.d(getClass().getName(), "Score for " + entry.getKey() + " was: " + entry.getValue());
286
-            entry.setValue(entry.getValue() * (1 - DELTA));
287
-
288
-            if (entry.getKey().equals(target)) {
289
-                entry.setValue(entry.getValue() + DELTA);
290
-            }
291
-            //Log.d(getClass().getName(), "Score for " + entry.getKey() + " is now: " + entry.getValue());
292
-        }
293
-    }
294
-
295 205
     @Override
296 206
     public void onDestroy() {
297 207
         super.onDestroy();

+ 113
- 0
code/Common/Aggregator.java Bestand weergeven

@@ -0,0 +1,113 @@
1
+/*
2
+ * To change this template, choose Tools | Templates
3
+ * and open the template in the editor.
4
+ */
5
+
6
+package uk.co.md87.android.common;
7
+
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+
11
+/**
12
+ *
13
+ * @author chris
14
+ */
15
+public class Aggregator {
16
+
17
+    static final double DELTA = 0.25;
18
+    static final double THRESHOLD = 0.5;
19
+
20
+    private final HashMap<String, HashMap<String, Double>> scores
21
+            = new HashMap<String, HashMap<String, Double>>() {{
22
+        put("", new HashMap<String, Double>() {{
23
+            put("null", 0.5d);
24
+            put("CLASSIFIED", 0.5d);
25
+        }});
26
+
27
+        put("CLASSIFIED", new HashMap<String, Double>() {{
28
+            put("null", 0.2d);
29
+            put("DANCING", 0.2d);
30
+            put("WALKING", 0.2d);
31
+            put("VEHICLE", 0.2d);
32
+            put("IDLE", 0.2d);
33
+        }});
34
+
35
+        put("CLASSIFIED/WALKING", new HashMap<String, Double>() {{
36
+            put("null", 0.5d);
37
+            put("STAIRS", 0.5d);
38
+        }});
39
+
40
+        put("CLASSIFIED/VEHICLE", new HashMap<String, Double>() {{
41
+            put("null", 0.333d);
42
+            put("CAR", 0.333d);
43
+            put("BUS", 0.333d);
44
+        }});
45
+
46
+        put("CLASSIFIED/IDLE", new HashMap<String, Double>() {{
47
+            put("null", 0.333d);
48
+            put("STANDING", 0.333d);
49
+            put("SITTING", 0.333d);
50
+        }});
51
+
52
+        put("CLASSIFIED/WALKING/STAIRS", new HashMap<String, Double>() {{
53
+            put("null", 0.333d);
54
+            put("UP", 0.333d);
55
+            put("DOWN", 0.333d);
56
+        }});
57
+    }};
58
+
59
+    public void addClassification(final String classification) {
60
+        String path = "";
61
+
62
+        for (String part : classification.split("/")) {
63
+            if (!scores.containsKey(path)) {
64
+                throw new RuntimeException("Path not found: " + path
65
+                        + " (classification: " + classification + ")");
66
+            }
67
+
68
+            updateScores(scores.get(path), part);
69
+            path = path + (path.length() == 0 ? "" : "/") + part;
70
+        }
71
+
72
+        if (scores.containsKey(path)) {
73
+            // This classification has children which we're not using
74
+            // e.g. we've received CLASSIFIED/WALKING, but we're not walking
75
+            //      up or down stairs
76
+            updateScores(scores.get(path), "null");
77
+        }
78
+    }
79
+
80
+    void updateScores(final Map<String, Double> map, final String target) {
81
+        for (Map.Entry<String, Double> entry : map.entrySet()) {
82
+            //Log.d(getClass().getName(), "Score for " + entry.getKey() + " was: " + entry.getValue());
83
+            entry.setValue(entry.getValue() * (1 - DELTA));
84
+
85
+            if (entry.getKey().equals(target)) {
86
+                entry.setValue(entry.getValue() + DELTA);
87
+            }
88
+            //Log.d(getClass().getName(), "Score for " + entry.getKey() + " is now: " + entry.getValue());
89
+        }
90
+    }
91
+
92
+    public String getClassification() {
93
+        String path = "";
94
+
95
+        do {
96
+            final Map<String, Double> map = scores.get(path);
97
+            double best = THRESHOLD;
98
+            String bestPath = "null";
99
+
100
+            for (Map.Entry<String, Double> entry : map.entrySet()) {
101
+                if (entry.getValue() >= best) {
102
+                    best = entry.getValue();
103
+                    bestPath = entry.getKey();
104
+                }
105
+            }
106
+
107
+            path = path + (path.length() == 0 ? "" : "/") + bestPath;
108
+        } while (scores.containsKey(path));
109
+
110
+        return path.replaceAll("(^CLASSIFIED)?/?null$", "");
111
+    }
112
+
113
+}

Laden…
Annuleren
Opslaan