forked from asafschers/goscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
49 lines (43 loc) · 1.35 KB
/
tree.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
package goscore
import (
"encoding/xml"
"errors"
"strconv"
)
type truePredicate struct{}
type dummyMiningSchema struct{}
// Node - PMML tree node
type Node struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Content []byte `xml:",innerxml"`
Nodes []Node `xml:",any"`
True truePredicate `xml:"True"`
DummyMiningSchema dummyMiningSchema `xml:"MiningSchema"`
SimplePredicate SimplePredicate `xml:"SimplePredicate"`
SimpleSetPredicate SimpleSetPredicate `xml:"SimpleSetPredicate"`
}
// TraverseTree - traverses Node predicates with features and returns score by terminal node
func (n Node) TraverseTree(features map[string]interface{}) (score float64, err error) {
curr := n.Nodes[0]
for len(curr.Nodes) > 0 {
prevID := curr.Attrs[0].Value
curr = step(curr, features)
if prevID == curr.Attrs[0].Value {
break
}
}
if len(curr.Attrs) < 2 {
return -1, errors.New("Terminal node without score, Node id: " + curr.Attrs[0].Value)
}
return strconv.ParseFloat(curr.Attrs[1].Value, 64)
}
func step(curr Node, features map[string]interface{}) Node {
for _, node := range curr.Nodes {
if node.XMLName.Local == "True" || node.SimplePredicate.True(features) || node.SimpleSetPredicate.True(features) {
curr = node
break
}
}
return curr
}