-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
95 lines (81 loc) · 2.21 KB
/
dump.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
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
package bayes
import (
"bytes"
"encoding/json"
"github.com/gnames/bayes/ent/bayesdump"
ft "github.com/gnames/bayes/ent/feature"
)
// Dump serializes a Bayes object into a JSON format.
func (nb *bayes) Dump() ([]byte, error) {
return json.MarshalIndent(nb, "", " ")
}
// Load deserializes a JSON text into Bayes object. The function needs
// to know how to convert a string that represents a class to an object.
func (nb *bayes) Load(dump []byte) error {
r := bytes.NewReader(dump)
return json.NewDecoder(r).Decode(nb)
}
// MarshalJSON serializes a NaiveBayes object to JSON.
func (nb *bayes) MarshalJSON() ([]byte, error) {
res := nb.Inspect()
return json.Marshal(&res)
}
func (nb *bayes) Inspect() bayesdump.BayesDump {
ls := make([]string, len(nb.classes))
for i, v := range nb.classes {
ls[i] = string(v)
}
lfs := make(map[string]int)
for k, v := range nb.classCases {
lfs[string(k)] = v
}
ffs := make(map[string]map[string]map[string]int)
for fk, fv := range nb.featureCases {
if _, ok := ffs[string(fk.Name)]; !ok {
val1 := make(map[string]map[string]int)
ffs[string(fk.Name)] = val1
}
if _, ok := ffs[string(fk.Name)][string(fk.Value)]; !ok {
val2 := make(map[string]int)
ffs[string(fk.Name)][string(fk.Value)] = val2
}
for lk, v := range fv {
ffs[string(fk.Name)][string(fk.Value)][string(lk)] = v
}
}
return bayesdump.BayesDump{
Classes: ls,
CasesTotal: nb.casesTotal,
ClassCases: lfs,
FeatureCases: ffs,
}
}
// UnmarshalJSON deserializes JSON data to a NaiveBayes object.
func (nb *bayes) UnmarshalJSON(data []byte) error {
var res bayesdump.BayesDump
if err := json.Unmarshal(data, &res); err != nil {
return err
}
nb.classes = make([]ft.Class, len(res.Classes))
for i, v := range res.Classes {
nb.classes[i] = ft.Class(v)
}
nb.casesTotal = res.CasesTotal
for k, v := range res.ClassCases {
nb.classCases[ft.Class(k)] = v
}
for k1, v1 := range res.FeatureCases {
name := ft.Name(k1)
for k2, v2 := range v1 {
v := make(map[ft.Class]int)
f := ft.Feature{Name: name, Value: ft.Value(k2)}
nb.featureCases[f] = v
for k3, v3 := range v2 {
class := ft.Class(k3)
nb.featureCases[f][class] = v3
}
}
}
nb.featTotal()
return nil
}