From 5fa387c244c7851543b59ddbc86be7d2a741ba10 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Wed, 14 Jun 2017 20:20:50 +0200 Subject: [PATCH] reading file and converting to array of dataset --- .gitignore | 1 + errorsCheck.go | 9 +++++++++ euclideanDistance.go | 5 +++++ main.go | 8 ++++++++ readDataFile.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+) create mode 100644 .gitignore create mode 100644 errorsCheck.go create mode 100644 euclideanDistance.go create mode 100644 main.go create mode 100644 readDataFile.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0190e1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +datasets \ No newline at end of file diff --git a/errorsCheck.go b/errorsCheck.go new file mode 100644 index 0000000..9e83a92 --- /dev/null +++ b/errorsCheck.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func check(err error) { + if err != nil { + fmt.Println(err) + } +} diff --git a/euclideanDistance.go b/euclideanDistance.go new file mode 100644 index 0000000..75e9632 --- /dev/null +++ b/euclideanDistance.go @@ -0,0 +1,5 @@ +package main + +func euclideanDistance(dataset [][][]int) { + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..6f6265d --- /dev/null +++ b/main.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + dataset := readDataFile("datasets/poker/poker-hand-training-true.data", "\n", ",") + fmt.Println(dataset) +} diff --git a/readDataFile.go b/readDataFile.go new file mode 100644 index 0000000..49e9375 --- /dev/null +++ b/readDataFile.go @@ -0,0 +1,35 @@ +package main + +import ( + "io/ioutil" + "strconv" + "strings" +) + +func readDataFile(path string, lineSeparation string, valueSeparation string) [][][]int { + var dataset [][][]int + b, err := ioutil.ReadFile(path) + check(err) + str := string(b) + str = strings.Replace(str, "\r", "", -1) + lines := strings.Split(str, lineSeparation) + for _, v1 := range lines { + params := strings.Split(v1, valueSeparation) + var datasetLine [][]int + var datasetLineEntry []int + var lastLineValue []int + for k2, v2 := range params { + value, err := strconv.Atoi(v2) + check(err) + if k2 < len(params)-1 { + datasetLineEntry = append(datasetLineEntry, value) + } else { + lastLineValue = append(lastLineValue, value) + } + } + datasetLine = append(datasetLine, datasetLineEntry) + datasetLine = append(datasetLine, lastLineValue) + dataset = append(dataset, datasetLine) + } + return dataset +}