-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (133 loc) · 5 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"os"
"log"
"fmt"
"net/http"
"io/ioutil"
"path/filepath"
"github.com/made2591/go-cpt/model/sequence"
"github.com/made2591/go-cpt/model/predictionTree"
"github.com/made2591/go-cpt/model/invertedIndexTable"
"github.com/made2591/go-cpt/model/compactPredictionTree"
"strings"
"encoding/json"
)
const maxUploadSize = 20 * 1024 * 1024 // 2 mb
const uploadPath = "./uploads"
type Prediction struct {
Sequence []string
Prediction []string
}
func local() {
trainingSequences := sequence.ReadCSVSequencesFile("./data/dummy.csv")
testingSequences := sequence.ReadCSVSequencesFile("./data/dumbo.csv")
trainingSequences = sequence.ReadCSVSequencesFile("./data/train.csv", 1, 11)
testingSequences = sequence.ReadCSVSequencesFile("./data/test.csv", 1, 11)
for _, seq := range trainingSequences {
fmt.Println(sequence.String(seq))
}
for _, seq := range testingSequences {
fmt.Println(sequence.String(seq))
}
invertedIndex := invertedIndexTable.NewInvertedIndexTable(trainingSequences)
predTree := predictionTree.NewPredictionTree("ROOT")
cpt := compactPredictionTree.NewCompactPredictionTree(invertedIndex, predTree, trainingSequences, testingSequences)
compactPredictionTree.InitCompactPredictionTree(cpt)
fmt.Println(predictionTree.String(cpt.PredictionTree))
predictions := compactPredictionTree.PredictionOverTestingSequence(cpt,5, 3)
for i := 0; i < len(testingSequences); i++ {
fmt.Println(testingSequences[i].Values)
fmt.Println(predictions[i])
}
}
func initcpt() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
trainingSequences := sequence.ReadCSVSequencesFile(strings.Join([]string{"./", uploadPath, "/train.csv"}, ""), 1, 11)
testingSequences := sequence.ReadCSVSequencesFile(strings.Join([]string{"./", uploadPath, "/test.csv"}, ""), 1, 11)
invertedIndex := invertedIndexTable.NewInvertedIndexTable(trainingSequences)
predTree := predictionTree.NewPredictionTree("ROOT")
cpt := compactPredictionTree.NewCompactPredictionTree(invertedIndex, predTree, trainingSequences, testingSequences)
compactPredictionTree.InitCompactPredictionTree(cpt)
fmt.Println(predictionTree.String(cpt.PredictionTree))
predictions := compactPredictionTree.PredictionOverTestingSequence(cpt, 5, 3)
jsonPredictions := []*Prediction{}
for i := 0; i < len(testingSequences); i++ {
jsonPredictions = append(jsonPredictions, &Prediction{Sequence: testingSequences[i].Values, Prediction: predictions[i]})
fmt.Println(testingSequences[i].Values)
fmt.Println(predictions[i])
}
js, err := json.Marshal(jsonPredictions)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
}
func uploadTrain() http.HandlerFunc {
return uploadFileHandler("train.csv")
}
func uploadTest() http.HandlerFunc {
return uploadFileHandler("test.csv")
}
func uploadFileHandler(position string) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// validate file size
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
if err := r.ParseMultipartForm(maxUploadSize); err != nil {
renderError(w, "FILE_TOO_BIG", http.StatusBadRequest)
return
}
// parse and validate file and post parameters
fileType := r.PostFormValue("type")
file, _, err := r.FormFile("uploadFile")
if err != nil {
renderError(w, "INVALID_FILE", http.StatusBadRequest)
return
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
renderError(w, "INVALID_FILE", http.StatusBadRequest)
return
}
// check file type, detectcontenttype only needs the first 512 bytes
filetype := http.DetectContentType(fileBytes)
switch filetype {
case "text/plain; charset=utf-8":
break
default:
renderError(w, "INVALID_FILE_TYPE", http.StatusBadRequest)
return
}
newPath := filepath.Join(uploadPath, position)
fmt.Printf("FileType: %s, File: %s\n", fileType, newPath)
// write file
newFile, err := os.Create(newPath)
if err != nil {
renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
}
defer newFile.Close() // idempotent, okay to call twice
if _, err := newFile.Write(fileBytes); err != nil || newFile.Close() != nil {
renderError(w, "CANT_WRITE_FILE", http.StatusInternalServerError)
return
}
w.Write([]byte("SUCCESS"))
})
}
func renderError(w http.ResponseWriter, message string, statusCode int) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(message))
}
func main() {
http.HandleFunc("/upload/train", uploadTrain())
http.HandleFunc("/upload/test", uploadTest())
http.HandleFunc("/initcpt", initcpt())
fs := http.FileServer(http.Dir(uploadPath))
http.Handle("/files/", http.StripPrefix("/files", fs))
log.Print("Server started on localhost:8080, use /upload/[train/test] for uploading train/test files and /files/{fileName} for downloading. Use /initcpt to start training and obtain predictions")
log.Fatal(http.ListenAndServe(":8080", nil))
}