/
FloatToStringTransform.java
75 lines (64 loc) · 2.41 KB
/
FloatToStringTransform.java
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
package com.airbnb.aerosolve.core.transforms;
import com.airbnb.aerosolve.core.FeatureVector;
import com.airbnb.aerosolve.core.util.Util;
import com.typesafe.config.Config;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Similar to MoveFloatToStringAndFloat, however, just move defined float value into String Feature
* not using bucket. This is used when there are certain number of incorrect data,
* i.e. x = 0 doesn't mean it is worse than x = 0.00001, it just somewhere in the pipeline
* make null = 0, so before we fixed the pipeline, convert it to string feature.
*/
@Slf4j
public class FloatToStringTransform implements Transform {
private String fieldName;
private Collection<String> keys;
private Set<Double> values;
private String stringOutputName;
@Override
public void configure(Config config, String key) {
fieldName = config.getString(key + ".field1");
if (config.hasPath(key + ".keys")) {
keys = config.getStringList(key + ".keys");
}
values = new HashSet<>(config.getDoubleList(key + ".values"));
stringOutputName = config.getString(key + ".string_output");
}
@Override
public void doTransform(FeatureVector featureVector) {
Map<String, Map<String, Double>> floatFeatures = featureVector.floatFeatures;
if (floatFeatures == null || floatFeatures.isEmpty()) {
return;
}
Map<String, Double> input = floatFeatures.get(fieldName);
if (input == null || input.isEmpty()) {
return;
}
Util.optionallyCreateStringFeatures(featureVector);
Map<String, Set<String>> stringFeatures = featureVector.getStringFeatures();
Set<String> stringOutput = Util.getOrCreateStringFeature(stringOutputName, stringFeatures);
Collection<String> localKeys = (keys == null)? input.keySet() : keys;
log.debug("k {} {}", localKeys, input);
for (String key : localKeys) {
moveFloatToStringAndFloat(
input, key, values, stringOutput);
}
}
private void moveFloatToStringAndFloat(
Map<String, Double> input,
String key, Set<Double> values,
Set<String> stringOutput) {
if (input.containsKey(key)) {
Double inputFloatValue = input.get(key);
if (values.contains(inputFloatValue)) {
String movedFloat = key + "=" + inputFloatValue;
stringOutput.add(movedFloat);
input.remove(key);
}
}
}
}