This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
StringAnalytics.scala
executable file
·251 lines (231 loc) · 9.87 KB
/
StringAnalytics.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Copyright (c) 2014 Oculus Info Inc.
* http://www.oculusinfo.com/
*
* Released under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oculusinfo.tilegen.tiling.analytics
import java.io.{Serializable => JavaSerializable}
import java.lang.{Double => JavaDouble}
import java.util.{List => JavaList}
import org.apache.avro.util.Utf8
import org.json.{JSONArray, JSONObject}
import scala.collection.JavaConverters._
import com.oculusinfo.factory.util.Pair
/**
* Standard string score ordering
*
* @param baseAnalytic An analytic used to aggregate scores
* @param aggregationLimit An optional number of elements to keep when
* aggregating. If None, all elements are kept.
* @param order An optional function to specify the order of values. If not
* given, the order will be random.
*/
class StringScoreAnalytic[T]
(baseAnalytic: Analytic[T],
aggregationLimit: Option[Int] = None,
order: Option[((String, T), (String, T)) => Boolean] = None)
extends Analytic[Map[String, T]]
{
def aggregate (a: Map[String, T], b: Map[String, T]): Map[String, T] = {
val combination =
(a.keySet union b.keySet).map(key =>
// Should always have one or the other, since we took the union
// of keysets, so the error case where we try to reduce an
// empty list shouldn't happen
key -> List(a.get(key), b.get(key)).flatten.reduce(baseAnalytic.aggregate(_, _))
)
val sorted: Iterable[(String, T)] = order.map(fcn =>
combination.toList.sortWith(fcn)
).getOrElse(combination)
aggregationLimit.map(limit =>
sorted.take(limit)
).getOrElse(sorted).toMap
}
def defaultProcessedValue: Map[String, T] = Map[String, T]()
def defaultUnprocessedValue: Map[String, T] = Map[String, T]()
}
/**
* Extends the standard string score analytic into a binning analytic.
*
* @param baseAnalytic See StringScoreAnalytic
* @param aggregationLimit See StringScoreAnalytic
* @param order See StringScoreAnalytic
* @param storageLimit An optional maximum number of entries to store in each
* tile bin. If not given, all values are stored.
* @tparam T See StringScoreAnalytic
* @tparam JT The type as which the score is to be written to bins.
*/
class StringScoreBinningAnalytic[T, JT]
(baseAnalytic: BinningAnalytic[T, JT],
aggregationLimit: Option[Int] = None,
order: Option[((String, T), (String, T)) => Boolean] = None,
storageLimit: Option[Int] = None)
extends StringScoreAnalytic[T](baseAnalytic, aggregationLimit, order)
with BinningAnalytic[Map[String, T], JavaList[Pair[String, JT]]]
{
def finish (value: Map[String, T]): JavaList[Pair[String, JT]] = {
val valueSeq =
order
.map(fcn => value.toSeq.sortWith(fcn))
.getOrElse(value.toSeq)
.map(p => new Pair[String, JT](p._1, baseAnalytic.finish(p._2)))
storageLimit
.map(valueSeq.take(_))
.getOrElse(valueSeq)
.asJava
}
}
/**
* Extends the standard string score analytic into a tile analytic with scores keyed by string.
*
* @param analyticName The name by which the analytic value should be known in metadata
* @param baseAnalytic See StringScoreAnalytic
* @param stringName The name by which the string in each entry is known in the metadata to which this analytic is written
* @param scoreName The name by which the score in each entry is known in the metadata to which this analytic is written
* @param aggregationLimit See StringScoreAnalytic
* @param order See StringScoreAnalytic
* @tparam T See StringScoreAnalytic
*/
import TileAnalytic.Locations
class StringScoreTileAnalytic[T] (analyticName: Option[String],
baseAnalytic: TileAnalytic[T],
stringName: String = "string",
scoreName: String = "score",
writeLocations: Set[TileAnalytic.Locations.Value] = TileAnalytic.Locations.values,
aggregationLimit: Option[Int] = None,
order: Option[((String, T), (String, T)) => Boolean] = None,
storageLimit: Option[Int] = None)
extends StringScoreAnalytic[T](baseAnalytic, aggregationLimit, order)
with TileAnalytic[Map[String, T]]
{
def foo = TileAnalytic.Locations.values
def name = analyticName.getOrElse(baseAnalytic.name)
override def storableValue (value: Map[String, T], location: TileAnalytic.Locations.Value): Option[JSONObject] = {
if (writeLocations.contains(location)) {
val allValues = order.map(sorter=>value.toList.sortWith(sorter)).getOrElse(value.toList)
val topValues = storageLimit.map(limit => allValues.take(limit)).getOrElse(allValues)
val subRes = new JSONArray()
topValues.foreach { case (key, value) =>
baseAnalytic.storableValue(value, location).foreach{bsv =>
val entry = new JSONObject()
if (bsv.length() > 1) entry.put(scoreName, bsv)
else if (bsv.length == 1) entry.put(scoreName, bsv.get(JSONObject.getNames(bsv)(0)))
if (entry.length() > 0) {
entry.put(stringName, key)
subRes.put(entry)
}
}
}
if (subRes.length() > 0) {
val result = new JSONObject()
result.put(name, subRes)
Some(result)
} else None
} else None
}
}
/**
* Extends the standard string score analytic into a tile analytic with the top scoring strings, in order
*
* @param analyticName The name by which the analytic value should be known in metadata
* @param baseAnalytic See StringScoreAnalytic
* @param aggregationLimit See StringScoreAnalytic
* @param order See StringScoreAnalytic
* @tparam T See StringScoreAnalytic
*/
class OrderedStringTileAnalytic[T] (analyticName: Option[String],
baseAnalytic: TileAnalytic[T],
aggregationLimit: Option[Int] = None,
order: Option[((String, T), (String, T)) => Boolean] = None)
extends StringScoreAnalytic[T](baseAnalytic, aggregationLimit, order)
with TileAnalytic[Map[String, T]]
{
def name = analyticName.getOrElse(baseAnalytic.name)
override def storableValue (value: Map[String, T], location: TileAnalytic.Locations.Value): Option[JSONObject] = {
val values = order.map(sorter=>value.toList.sortWith(sorter)).getOrElse(value.toList)
val outputValues = new JSONArray()
values.foreach { case (key, value) => outputValues.put(key)}
if (outputValues.length()>0) {
val result = new JSONObject()
result.put(name, outputValues)
Some(result)
}
else None
}
}
/**
* Similar to a StringScoreAnalytic, but this analytic tracks fixed, rather
* than arbitrary, categories.
*
* @param categoryNames The names of the fixed categories this analytic will
* track
* @param baseAnalytic An analytic used to process the scores associated with
* each category.
* @tparam T The type of the score associated with each category
*/
class CategoryValueAnalytic[T] (categoryNames: Seq[String], baseAnalytic: Analytic[T])
extends Analytic[Seq[T]]
{
def aggregate (a: Seq[T], b: Seq[T]): Seq[T] =
a.zipAll(b, baseAnalytic.defaultUnprocessedValue, baseAnalytic.defaultUnprocessedValue)
.map{case (aa, bb) => baseAnalytic.aggregate(aa, bb)}
def defaultProcessedValue: Seq[T] = Seq[T]()
def defaultUnprocessedValue: Seq[T] = Seq[T]()
}
/**
* Extends the standard category value analytic into a binning analytic.
*
* @param categoryNames {@see CategoryValueAnalytic}
* @param baseAnalytic {@see CategoryValueAnalytic}
* @tparam T {@see CategoryValueAnalytic}
* @tparam JT The type as which the score is to be written to bins.
*/
class CategoryValueBinningAnalytic[T, JT] (categoryNames: Seq[String], baseAnalytic: BinningAnalytic[T, JT])
extends CategoryValueAnalytic[T](categoryNames, baseAnalytic)
with BinningAnalytic[Seq[T], JavaList[Pair[String, JT]]]
{
def finish (value: Seq[T]): JavaList[Pair[String, JT]] =
categoryNames.zip(value).map{case (c, v) =>
new Pair[String, JT](c, baseAnalytic.finish(v))
}.toSeq.asJava
}
class CategoryValueTileAnalytic[T] (analyticName: Option[String],
categoryNames: Seq[String],
baseAnalytic: TileAnalytic[T])
extends CategoryValueAnalytic[T](categoryNames, baseAnalytic)
with TileAnalytic[Seq[T]]
{
def name = analyticName.getOrElse(baseAnalytic.name)
override def storableValue (value: Seq[T], location: TileAnalytic.Locations.Value): Option[JSONObject] = {
val outputValues = new JSONArray()
value.map(t => baseAnalytic.storableValue(t, location).map(bt => outputValues.put(bt)))
if (outputValues.length()>0) {
val result = new JSONObject()
result.put(name, outputValues)
Some(result)
} else None
}
}
class StringAnalytic (analyticName: String) extends TileAnalytic[String] {
def name = analyticName
def aggregate (a: String, b: String): String = a+b
def defaultProcessedValue: String = ""
def defaultUnprocessedValue: String = ""
}