forked from ProlificLabs/shakesearch_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
261 lines (224 loc) · 6.24 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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
stemmer "github.com/agonopol/go-stem"
)
type (
// ShakespeareDataRec holds all lines of Shakespear's works
ShakespeareDataRec struct {
ShakespeareLine []ShakespeareLineRec `json:"data"`
}
// ShakespeareLineRec hold individual lines of Shakespear's works
ShakespeareLineRec struct {
Type string `json:"type,omitempty"`
LineID int `json:"line_id,omitempty"`
PlayName string `json:"play_name,omitempty"`
SpeechNumber string `json:"speech_number,omitempty"`
LineNumber string `json:"line_number,omitempty"`
Speaker string `json:"speaker,omitempty"`
TextEntry string `json:"text_entry,omitempty"`
WordsList []string `json:"words_list,omitempty"`
}
// ResultRec hold the data from the database used to fill the template
ResultRec struct {
lineBefore string
hit string
lineAfter string
playName string
lineNumber string
speaker string
lineType string
score int
}
// ResultsToFront used for JSON object to frontend
ResultsToFront struct {
HTML string `json:"HTML"`
NumResults int `json:"numResults"`
}
)
var (
data ShakespeareDataRec
err error
)
func main() {
// INITIALIZATION
// Init logs
lf, err := os.OpenFile("shakespeare.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
err = errors.New("Failed to open/create log file")
log.Println(err)
}
log.SetOutput(lf)
defer lf.Close()
// Get input from Database
b, err := ioutil.ReadFile("shakeworks.json")
if err != nil {
err = errors.New("Failed to open database file")
log.Fatal(err)
}
err = json.Unmarshal([]byte(b), &data)
if err != nil {
err = errors.New("Failed to unmarshal database")
log.Fatal(err)
}
log.Println("Database loaded succussfully")
// Init http server
fs := http.FileServer(http.Dir("./static"))
http.Handle("/", fs)
http.HandleFunc("/search", handleSearch(data))
port := os.Getenv("PORT")
if port == "" {
port = "3001"
}
log.Printf("Listening on port %s...", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatal(err)
}
}
func handleSearch(data ShakespeareDataRec) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
query, ok := r.URL.Query()["q"]
if !ok || len(query[0]) < 1 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing search query in URL params"))
return
}
resultsToFront := data.Search(query[0])
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
err := enc.Encode(resultsToFront)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("encoding failure"))
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf.Bytes())
}
}
// Search main search function
func (s *ShakespeareDataRec) Search(query string) (resultsToFront ResultsToFront) {
var (
hits []ResultRec
results string
)
queryWords := getWordsSlice(query)
for i := range s.ShakespeareLine {
score := matchScore(s.ShakespeareLine[i].WordsList, queryWords)
if score > 0 {
hit := ResultRec{
lineBefore: s.ShakespeareLine[i-1].TextEntry,
hit: makeBold(s.ShakespeareLine[i].TextEntry, query),
lineAfter: s.ShakespeareLine[i+1].TextEntry,
playName: s.ShakespeareLine[i].PlayName,
lineNumber: s.ShakespeareLine[i].LineNumber,
speaker: s.ShakespeareLine[i].Speaker,
lineType: s.ShakespeareLine[i].Type,
score: score,
}
hits = append(hits, hit)
}
}
sortResults(hits)
for _, v := range hits {
results += v.FormatHTML(v)
}
resultsToFront = ResultsToFront{
HTML: results,
NumResults: len(hits),
}
return resultsToFront
}
func getWordsSlice(s string) (wordSlice []string) {
// Make a Regex to say we only want letters, numbers, and spaces
reg, err := regexp.Compile("[^a-zA-Z0-9\\s]+")
if err != nil {
log.Println(err)
}
processedString := reg.ReplaceAllString(s, "")
words := strings.Split(processedString, " ")
for _, v := range words {
wordSlice = append(wordSlice, string(stemmer.Stem([]byte(v))))
}
return wordSlice
}
func matchScore(data []string, query []string) (score int) {
for _, d := range data {
for _, q := range query {
if d == q {
score++
}
}
}
return score
}
func makeBold(line string, query string) string {
slice := strings.Split(line, " ")
for i := range slice {
if strings.Contains(
strings.ToLower(slice[i]),
strings.ToLower(query),
) {
slice[i] = fmt.Sprintf("<b>%v</b>", slice[i])
}
}
return strings.Join(slice, " ")
}
func sortResults(hits []ResultRec) {
var less func(i, j int) bool
less = func(i, j int) bool {
return hits[i].score > hits[j].score
}
sort.Slice(hits, less)
}
// FormatHTML formats the data in the needed HTML
func (r *ResultRec) FormatHTML(hit ResultRec) string {
text := fmt.Sprintf("%v %v %v", r.lineBefore, r.hit, r.lineAfter)
formatedResult := fmt.Sprintf(`<figure class="result">
<div class="result__content">
<div class="result__title">
<h2 class="result__heading">%v %v</h2>
<div class="result__tag result__tag--1">#%v</div>
<div class="result__tag result__tag--2">#%v</div>
</div>
<p class="result__description">%v</p>
</div>
<div class="result__work">
RANK %v
</div>
</figure>`, r.playName, r.lineNumber, r.lineType, r.speaker, text, r.score)
return formatedResult
}
// SearchSimple simple string match function
func (s *ShakespeareDataRec) SearchSimple(query string) string {
var results string
for i := range s.ShakespeareLine {
if strings.Contains(
strings.ToLower(s.ShakespeareLine[i].TextEntry),
strings.ToLower(query),
) {
hit := ResultRec{
lineBefore: s.ShakespeareLine[i-1].TextEntry,
hit: strings.Replace(s.ShakespeareLine[i].TextEntry, query, fmt.Sprintf("<b>%v</b>", query), -1),
lineAfter: s.ShakespeareLine[i+1].TextEntry,
playName: s.ShakespeareLine[i].PlayName,
lineNumber: s.ShakespeareLine[i].LineNumber,
speaker: s.ShakespeareLine[i].Speaker,
lineType: s.ShakespeareLine[i].Type,
}
results += hit.FormatHTML(hit)
}
}
return results
}