forked from asafschers/goscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_predicate.go
60 lines (52 loc) · 1.55 KB
/
simple_predicate.go
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
package goscore
import (
"strconv"
)
// SimplePredicate - PMML simple predicate
type SimplePredicate struct {
Field string `xml:"field,attr"`
Operator string `xml:"operator,attr"`
Value string `xml:"value,attr"`
}
// True - Evaluates to true if features input is true for SimplePredicate
func (p SimplePredicate) True(features map[string]interface{}) bool {
if p.Operator == "isMissing" {
featureValue, exists := features[p.Field]
return featureValue == "" || featureValue == nil || !exists
}
switch featureValue := features[p.Field].(type) {
case int:
return numericTrue(p, float64(featureValue))
case float64:
return numericTrue(p, featureValue)
case string:
if p.Operator == "equal" {
return p.Value == features[p.Field]
}
numericFeatureValue, err := strconv.ParseFloat(featureValue, 64)
if err == nil {
return numericTrue(p, numericFeatureValue)
}
case bool:
if p.Operator == "equal" {
predicateBool, _ := strconv.ParseBool(p.Value)
return predicateBool == features[p.Field]
}
}
return false
}
func numericTrue(p SimplePredicate, featureValue float64) bool {
predicateValue, _ := strconv.ParseFloat(p.Value, 64)
if p.Operator == "equal" {
return featureValue == predicateValue
} else if p.Operator == "lessThan" {
return featureValue < predicateValue
} else if p.Operator == "lessOrEqual" {
return featureValue <= predicateValue
} else if p.Operator == "greaterThan" {
return featureValue > predicateValue
} else if p.Operator == "greaterOrEqual" {
return featureValue >= predicateValue
}
return false
}