/
io.go
116 lines (97 loc) · 2.69 KB
/
io.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Package io contains files for the interaction with the filesystem
package io
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gocarina/gocsv"
"github.com/lustigo/refingerprint/proc/data"
"github.com/sbinet/go-arff"
)
// ParseData reads the given Filename and parses the content to the Data Structure
func ParseData(path string) (*data.Data, error) {
c, err := ReadFile(path)
if err != nil {
return nil, fmt.Errorf("Could not parse Data: %v", err)
}
d := &data.Data{}
err = json.Unmarshal(c, &d)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal: %v", err)
}
return d, nil
}
// ReadFile reads the given Path
func ReadFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Could not open file: %v", err)
}
content, err := ioutil.ReadAll(f)
if err != nil {
f.Close()
return nil, fmt.Errorf("Could not read file: %v", err)
}
f.Close()
return content, nil
}
// GetFileName returns the FileName without extension
func GetFileName(path string) string {
base := filepath.Base(path)
splitted := strings.Split(base, ".")
if len(splitted) == 0 {
return ""
}
return splitted[0]
}
// WriteCSVFile writes the given Features to the given path as a CSV File
func WriteCSVFile(path string, data []*data.ProcessedFeatures) {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
fmt.Printf("Could not open file %v: %v\n", file, err)
return
}
defer file.Close()
err = gocsv.MarshalFile(data, file)
if err != nil {
fmt.Printf("Could not write to file %v: %v\n", file, err)
}
}
// WriteARFFFile writes the given Features to the given path as a ARFF File
func WriteARFFFile(path string, d []*data.ProcessedFeatures) {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
fmt.Printf("Could not open file %v: %v\n", file, err)
return
}
enc, err := arff.NewEncoder(file)
if err != nil {
fmt.Printf("Could not get an ARFF Encoder: %v\n", err)
file.Close()
return
}
enc.Header = data.GetARFFHeader()
for _, feature := range d {
if err = enc.Encode(feature); err != nil {
fmt.Printf("Could not encode %v: %v\n", feature, err)
}
}
file.Sync()
file.Close()
replaceInf(path)
}
// replaceInf replaces all the Inf strings in the given File path with 9223372036854775807 and saves it to disk
func replaceInf(path string) {
r, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("Could not open file: %v\n", err)
}
s := strings.ReplaceAll(string(r), "+Inf", "9223372036854775807")
err = ioutil.WriteFile(path, []byte(s), os.ModePerm)
if err != nil {
fmt.Printf("Could not write new content to file: %v\n", err)
}
}