-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
285 lines (244 loc) · 6.72 KB
/
server.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
283
284
package main
import (
"io/ioutil"
"strconv"
"strings"
"fmt"
"net/http"
"encoding/json"
"math"
)
const (
dbServer = "localhost"
dbName = "ngrams"
collecName = "words"
)
const (
cleanRaw = false
)
func main() {
if cleanRaw {
ProcessRaw()
return
}
http.HandleFunc("/viz", indexHandler)
http.HandleFunc("/viz/viz.js", vizScriptHandler)
http.HandleFunc("/viz/scattergrams.css", cssFileHandler)
http.HandleFunc("/data/", dataHandlerGen())
fmt.Println("Starting http server...")
err := http.ListenAndServe("0.0.0.0:8888", nil)
if err != nil {
fmt.Println(err)
return
}
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
file_name := "index.html"
file_data, _ := ioutil.ReadFile(file_name)
_, _ = w.Write(file_data)
}
func vizScriptHandler(w http.ResponseWriter, req *http.Request) {
file_name := "viz.js"
file_data, _ := ioutil.ReadFile(file_name)
w.Header().Set("Content-Type", "text/javascript")
_, _ = w.Write(file_data)
}
func cssFileHandler(w http.ResponseWriter, req *http.Request) {
file_name := "scattergrams.css"
file_data, _ := ioutil.ReadFile(file_name)
w.Header().Set("Content-Type", "text/css")
_, _ = w.Write(file_data)
}
type wordState struct {
words []*Word
wordMap map[string]*Word
data []*XYonly
xName string
yName string
otherName string
year string
weights Weights
maxes Weights
scored []*Word
scores []float32
}
func NewState(jsonWords string) *wordState {
words := UnmarshalJsonList(jsonWords)
wordMap := wordMapFor(words)
maxes := getMaxWeights()
st := &wordState{
words:words,
wordMap:wordMap,
maxes:maxes,
data:make([]*XYonly, 0),
}
return st
}
func (st *wordState) Rescore(params []string) {
st.year = params[0]
st.updateWeights(params)
fmt.Println("rescoring words")
// get score calcing function
scorer := WeightedScoreGenerator(st.year, st.weights, st.maxes)
// generate scores for words if possible
fmt.Println("wordslen=", len(st.words))
st.scored, st.scores = GetScores(st.words, scorer)
st.buildAndSort()
}
func (st *wordState) updateWeights(text []string) {
length, _ := strconv.ParseFloat(text[1], 32);
count, _ := strconv.ParseFloat(text[2], 32);
pages, _ := strconv.ParseFloat(text[3], 32);
books, _ := strconv.ParseFloat(text[4], 32);
pageden, _ := strconv.ParseFloat(text[5], 32);
temp, _ := strconv.ParseFloat(text[6], 32);
bookden, _ := strconv.ParseFloat(text[7],32);
tot := float32(math.Abs(length) + math.Abs(count) + math.Abs(pages) + math.Abs(books) + math.Abs(pageden) + math.Abs(temp) + math.Abs(bookden))
if tot == 0 {
tot = 1
}
st.weights.Length = float32(length) / tot
st.weights.Count = float32(count) / tot
st.weights.Pages = float32(pages) / tot
st.weights.Books = float32(books) / tot
st.weights.PageDen = float32(pageden) / tot
st.weights.Temp = float32(temp) / tot
st.weights.BookDen = float32(bookden) / tot
fmt.Println("updated weights: ", length, count, pages, books, pageden, temp, bookden)
}
func (st *wordState) BestYearForFollowed(names []string) int {
wordData := []*Word{}
for _, name := range names {
wordData = append(wordData, st.wordMap[name])
}
var bestScore float32 = 0
bestYear := 1700
for y := 1700; y <= 2008; y++ {
scrFunc := WeightedScoreGenerator(strconv.Itoa(y), st.weights, st.maxes)
_, followScores := GetScores(wordData, scrFunc)
tot := sum(followScores)
if tot > bestScore {
bestScore = tot
bestYear = y
}
}
return bestYear
}
func (st *wordState) DataFor(params []string) []*XYonly {
fmt.Println("1datalen=", len(st.data))
if st.xName != params[0] || st.yName != params[1] || st.otherName != params[2] {
fmt.Println("updating axes")
st.xName = params[0]
st.yName = params[1]
st.otherName = params[2]
st.buildAndSort()
}
fmt.Println("2datalen=", len(st.data))
return st.data
}
func (st *wordState) buildAndSort() {
fmt.Println("3datalen=", len(st.data))
fmt.Println("3scoredlen=", len(st.scored))
xFunc := paramFuncFor(st.xName, st.year)
yFunc := paramFuncFor(st.yName, st.year)
otherFunc := paramFuncFor(st.otherName, st.year)
fmt.Println("building and sorting xy structs")
// convert to XYonly structs
st.data = BuildXY(st.scored, st.scores, xFunc, yFunc, otherFunc)
fmt.Println("4datalen=", len(st.data))
// sort it
st.data = TreeToXYonly(XYonlyToTree(st.data, func(a, b interface{}) bool {
return a.(*XYonly).S <= b.(*XYonly).S
}))
fmt.Println("5datalen=", len(st.data))
}
func dataHandlerGen() func(http.ResponseWriter, *http.Request) {
st := NewState(jsonWords)
return func(w http.ResponseWriter, req *http.Request) {
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
fmt.Println("Recovered in 'handler'", r)
}
}()
path := req.URL.Path
rangeText := strings.Split(path, "/")
if rangeText[2] == "reweight" {
st.Rescore(rangeText[3:])
return
} else if rangeText[2] == "follow" {
wordNames := strings.Split(rangeText[3], ",")
bestYear := st.BestYearForFollowed(wordNames)
w.Write([]byte(strconv.Itoa(bestYear)))
return
}
axesAndParams := rangeText[2:5]
data := st.DataFor(axesAndParams)
lower, err := strconv.Atoi(rangeText[5])
if err != nil {
panic(err)
}
numWanted, err := strconv.Atoi(rangeText[6])
if err != nil {
panic(err)
}
if numWanted > len(data) {
numWanted = len(data)
}
marshaled, err := json.Marshal(data[lower:lower + numWanted])
if err != nil {
fmt.Println("rangeText: ", rangeText)
fmt.Println(err)
panic(err)
}
_, _ = w.Write(marshaled)
fmt.Println("request filled.")
}
}
func getMaxWeights() Weights {
w := Weights{}
w.Length = 12 * 1.333333
w.Count = 1e7 * .15256 * 430
w.Pages = 1e7 * 2.7316
w.Books = 1e5 * 6.6948 * 0.162
w.PageDen = 17 * 1.05334 * 1.123
w.Temp = 1
w.BookDen = w.PageDen / 200 //TODO: WHAT IS A GOOD MAX WEIGHT FOR BOOKDENSITY??
return w
}
func wordMapFor(wlist []*Word) map[string]*Word {
words := map[string]*Word{}
for _, w := range wlist {
words[w.T] = w
}
return words
}
func sum(scores []float32) float32 {
var tot float32 = 0
for _, val := range scores {
tot += val
}
return tot
}
func paramFuncFor(name , year string) func(*Word) float32 {
var f func(*Word) float32
switch name {
case "pden":
f = Pden(year)
case "bks":
f = Bk(year)
case "cnt":
f = Cnt(year)
case "tmp":
f = Tmp(year)
case "wlen":
f = Wlen(year)
case "bden":
f = Bden(year)
case "pgs":
f = Pg(year)
default:
panic("Invalid var name '" + name + "'")
}
return f
}