-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathimport.go
282 lines (245 loc) · 5.29 KB
/
import.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package punksranking
import (
"encoding/csv"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
)
var attributes = make(map[string]int)
var attIdToPunkId = make(map[string]int)
// 38%
func parsePercent(s string) (result float64) {
if len(s) == 0 {
return
}
if i := strings.Index(s, "%"); i != -1 {
s = s[:i]
}
result, _ = strconv.ParseFloat(s, 64)
return
}
func fixString(s string) string {
if s == "(blank)" {
return ""
}
return s
}
func fixInt(s string) int {
if result, err := strconv.Atoi(s); err != nil {
return 0
} else {
return result
}
}
func Zap() {
statements["delete_punk_att"].Exec()
statements["delete"].Exec()
statements["delete_att"].Exec()
}
func addAttributes(record ...string) {
for i := range record {
key := fixString(record[i])
if _, exists := attributes[key]; exists {
attributes[key]++
} else if key != "" {
attributes[key] = 1
}
}
}
// Calculate calculates all the scores
func Calculate() error {
_, err := statements["calculate"].Exec()
return err
}
// Link links the punks to attributes by linking
func Link() error {
/*
_, err := statements["insert_punk_attributes_type"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_skin"].Exec()
if err != nil {
return err
}
*/
_, err := statements["insert_punk_attributes_att1"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att2"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att3"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att4"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att5"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att6"].Exec()
if err != nil {
return err
}
_, err = statements["insert_punk_attributes_att7"].Exec()
if err != nil {
return err
}
/*
_, err = statements["insert_punk_attribute_attributes"].Exec()
if err != nil {
return err
}
*/
if _, err = db.Exec(
`CREATE TEMPORARY TABLE new_tbl select count(*) as total , category from punks group by category;`,
); err != nil {
return err
}
if _, err = db.Exec(
`UPDATE punks SET category_score = (select total from new_tbl where category = punks.category) / 100 ;`,
); err != nil {
return err
}
if _, err = db.Exec(
`DROP TEMPORARY TABLE new_tbl;`,
); err != nil {
return err
}
return err
}
// ImportAttr inserts the attributes
func ImportAttr() error {
id := 1
if len(attributes) > 0 {
for key, val := range attributes {
_, err := statements["insert_att"].Exec(
id,
key,
val,
)
if err != nil {
log.Print("insert_att err", id, key, val)
return err
}
attIdToPunkId[key] = id
id++
}
}
if _, err := statements["insert_count_att"].Exec(); err != nil {
log.Print("insert_count_att insert err")
return err
}
return nil
}
func Import(file string) error {
fin, err := os.Open(file)
if err != nil {
return err
}
defer fin.Close()
r := csv.NewReader(fin)
count := 0
for ; ; count++ {
record, err := r.Read()
if count == 0 {
// skip headers
continue
}
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
return err
}
category := buildCategory(record)
record[1] = ""
addAttributes(record[6:12]...) // att1 to att7
//addAttributes(record[3]) // skin
//addAttributes(record[3:4]...) // TSkin
addAttributes(category) // synthetic attribute
_, err = statements["insert"].Exec(
record[0], // id
fixString(record[1]), // sex (not used, already in the type field)
record[2], // type
record[3], // skin
record[4], // type_skin
record[5], // slots
fixString(record[6]), // arr1
fixString(record[7]), // arr2
fixString(record[8]), // att3
fixString(record[9]), // att4
fixString(record[10]), // arr5
fixString(record[11]), // att6
fixString(record[12]), // att7
parsePercent(record[13]), // type_rare
fixInt(record[14]), // att_count
parsePercent(record[15]), // att1_score
parsePercent(record[16]), // att2_score
parsePercent(record[17]), // att3_score
parsePercent(record[18]), // att4_score
parsePercent(record[19]), // att5_score
parsePercent(record[10]), // att6_score
parsePercent(record[20]), // att7_score
parsePercent(record[21]), // min
parsePercent(record[22]), // avg
parsePercent(record[23]), // rank
buildCategory(record),
)
if err != nil {
return err
}
fmt.Println(record)
}
return nil
}
func buildCategory(record []string) string {
human := "x"
nonhumanType := "xx"
skin := "xxx"
sex := "x"
// fix up skin
if record[2] == "Alien" {
// record[3] = ""
nonhumanType = "AL"
} else if record[2] == "Ape" {
//record[3] = ""
nonhumanType = "AP"
} else if record[2] == "Zombie" {
// record[3] = ""
nonhumanType = "ZO"
} else {
human = "H"
skin = record[3]
if record[1] == "Girl" {
sex = "F"
} else {
sex = "M"
}
}
// CATHMxx
switch record[3] {
case "Albino":
skin = "Alb"
case "Light":
skin = "lit"
case "Mid":
skin = "Mid"
case "Dark":
skin = "Drk"
}
if nonhumanType == "" {
log.Print("nuill")
}
return "CAT" + human + sex + skin + nonhumanType
}