/
buildsearchlist.go
412 lines (337 loc) · 10.8 KB
/
buildsearchlist.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// HipparchiaGoServer
// Copyright: E Gunderson 2022
// License: GNU GENERAL PUBLIC LICENSE 3
// (see LICENSE in the top level directory of the distribution)
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type SearchIncExl struct {
// the first are for internal use
AuGenres []string
WkGenres []string
AuLocations []string
WkLocations []string
Authors []string
Works []string
Passages []string // "lt0474_FROM_36136_TO_36151"
// the next are for output to the browser
MappedPsgByName map[string]string // "lt0474_FROM_36136_TO_36151": "Cicero, Pro Caelio, section 1
MappedAuthByName map[string]string
MappedWkByName map[string]string
// "val.Interface().([]string)" assertion in makeselections.go means we have to insist on the slices
ListedPBN []string
ListedABN []string
ListedWBN []string
}
func (i *SearchIncExl) isEmpty() bool {
l := len(i.AuGenres) + len(i.WkGenres) + len(i.AuLocations) + len(i.WkLocations) + len(i.Authors)
l += len(i.Works) + len(i.Passages)
if l > 0 {
return false
} else {
return true
}
}
func (i *SearchIncExl) CountItems() int {
l := len(i.AuGenres) + len(i.WkGenres) + len(i.AuLocations) + len(i.WkLocations) + len(i.Authors)
l += len(i.Works) + len(i.Passages)
return l
}
func (i *SearchIncExl) BuildAuByName() {
bn := make(map[string]string, len(i.MappedAuthByName))
for _, a := range i.Authors {
bn[a] = AllAuthors[a].Cleaname
}
i.MappedAuthByName = bn
var nn []string
for _, v := range bn {
nn = append(nn, v)
}
sort.Slice(nn, func(i, j int) bool { return nn[i] < nn[j] })
i.ListedABN = nn
}
func (i *SearchIncExl) BuildWkByName() {
const (
TMPL = `%s, <i>%s</i>`
)
bn := make(map[string]string, len(i.MappedWkByName))
for _, w := range i.Works {
ws := AllWorks[w]
au := ws.MyAu().Name
ti := ws.Title
bn[w] = fmt.Sprintf(TMPL, au, ti)
}
i.MappedWkByName = bn
var nn []string
for _, v := range bn {
nn = append(nn, v)
}
sort.Slice(nn, func(i, j int) bool { return nn[i] < nn[j] })
i.ListedWBN = nn
}
func (i *SearchIncExl) BuildPsgByName() {
var nn []string
for _, v := range i.MappedPsgByName {
nn = append(nn, v)
}
sort.Slice(nn, func(i, j int) bool { return nn[i] < nn[j] })
i.ListedPBN = nn
}
type ProcessedList struct {
Inc SearchIncExl
Excl SearchIncExl
Size int
}
// SessionIntoSearchlist - converts the stored set of selections into a calculated pair of SearchIncExl w/ Authors, Works, Passages
func SessionIntoSearchlist(s ServerSession) ProcessedList {
// https://medium.com/scum-gazeta/golang-simple-optimization-notes-70bc64673980
var inc SearchIncExl
var exc SearchIncExl
// note that we do all the initial stuff by adding WORKS to the list individually
// [a] trim mappers by active corpora
var activeauthors []string
var activeworks []string
for k, v := range s.ActiveCorp {
if v {
activeauthors = append(activeauthors, AuCorpusMap[k]...)
activeworks = append(activeworks, WkCorpusMap[k]...)
}
}
sessincl := s.Inclusions
sessexl := s.Exclusions
// retain in unmodified form
inc.Passages = sessincl.Passages
exc.Passages = sessexl.Passages
// [b] build the inclusion list: inc.Works is the core searchlist
if !sessincl.isEmpty() {
// you only want *some* things
// [b1] author genres to include
for _, g := range sessincl.AuGenres {
for _, a := range activeauthors {
if strings.Contains(AllAuthors[a].Genres, g) {
inc.Works = append(inc.Works, AllAuthors[a].WorkList...)
}
}
}
// [b2] work genres to include
for _, g := range sessincl.WkGenres {
for _, w := range activeworks {
if AllWorks[w].Genre == g {
inc.Works = append(inc.Works, AllWorks[w].UID)
}
}
}
// [b3] author locations to include
for _, l := range sessincl.AuLocations {
for _, a := range activeauthors {
if AllAuthors[a].Location == l {
inc.Works = append(inc.Works, AllAuthors[a].WorkList...)
}
}
}
// [b4] work locations to include
for _, l := range sessincl.WkLocations {
for _, w := range activeworks {
if AllWorks[w].Prov == l {
inc.Works = append(inc.Works, AllWorks[w].UID)
}
}
}
// a tricky spot: when/how to apply prunebydate()
// if you want to be able to seek 5th BCE oratory and Plutarch, then you need to let auselections take precedence
// accordingly we will do classes and genres first, then trim by date, then add inc individual choices
// [b5] prune by date
inc.Works = prunebydate(inc.Works, s)
// [b6] add all works of the authors registerselection
for _, au := range sessincl.Authors {
// this should be superfluous, but...
_, remains := AllAuthors[au]
if remains {
inc.Works = append(inc.Works, AllAuthors[au].WorkList...)
}
}
// [b7] add the individual works registerselection
for _, wk := range sessincl.Works {
// this should be superfluous, but...
_, remains := AllWorks[wk]
if remains {
inc.Works = append(inc.Works, wk)
}
}
// [b8] add the individual passages registerselection
inc.Passages = append(inc.Passages, sessincl.Passages...)
} else {
// you want everything. well, maybe everything...
for _, w := range activeworks {
inc.Works = append(inc.Works, AllWorks[w].UID)
}
// but maybe the only restriction is time...
inc.Works = prunebydate(inc.Works, s)
}
// [c] subtract the exclusions from the searchlist
// [c1] do we allow spuria (varia and incerta already lost via prunebydate)
// note that the following will kill explicitly registerselection spuria: basically a logic bug, but not a priority...
if !s.SpuriaOK {
var trimmed []string
for _, w := range inc.Works {
if AllWorks[w[0:10]].Authentic {
trimmed = append(trimmed, w)
}
}
inc.Works = trimmed
}
// [c2] walk through the exclusions categories; note that excluded passages are handled via the querybuilder
if !sessexl.isEmpty() {
// [c2a] the authors
blacklist := sessexl.Authors
// [c2c] the author genres
for _, g := range sessexl.AuGenres {
for _, a := range activeauthors {
if strings.Contains(AllAuthors[a].Genres, g) {
blacklist = append(blacklist, AllAuthors[a].UID)
}
}
}
// [c2c] the author locations
for _, l := range sessexl.AuLocations {
for _, a := range activeauthors {
if AllAuthors[a].Location == l {
blacklist = append(blacklist, AllAuthors[a].UID)
}
}
}
blacklist = unique(blacklist)
// [c2d] all works of all excluded authors are themselves excluded
// we are now moving over from AuUIDs to WkUIDS...
for _, b := range blacklist {
exc.Works = append(exc.Works, AllAuthors[b].WorkList...)
}
// [c2e] + the plain old work exclusions
exc.Works = append(exc.Works, sessexl.Works...)
// [c2f] works excluded by genre
for _, l := range sessexl.WkGenres {
for _, w := range activeworks {
if AllWorks[w].Genre == l {
exc.Works = append(exc.Works, AllWorks[w].UID)
}
}
}
// [c2g] works excluded by provenance
for _, l := range sessexl.WkLocations {
for _, w := range activeworks {
if AllWorks[w].Prov == l {
exc.Works = append(exc.Works, AllWorks[w].UID)
}
}
}
inc.Works = setsubtraction(inc.Works, exc.Works)
}
// this is the moment when you know the total # of locations searched: worth recording
sl := len(inc.Works)
// now we lose that info in the name of making the search quicker...
wp := calculatewholeauthorsearches(inc.Works)
inc.Authors = wp[0]
pruner := wp[1]
prunemap := make(map[string]bool)
for _, p := range pruner {
prunemap[p] = true
}
// still need to clean the whole authors out of inc.Works
var trim []string
for _, i := range inc.Works {
if _, ok := prunemap[i]; !ok {
trim = append(trim, i)
}
}
inc.Works = trim
inc.Passages = unique(inc.Passages)
inc.Works = unique(inc.Works)
inc.Authors = unique(inc.Authors)
exc.Passages = unique(exc.Passages)
exc.Works = unique(exc.Works)
exc.Authors = unique(exc.Authors)
var proc ProcessedList
proc.Inc = inc
proc.Excl = exc
proc.Size = sl
// fmt.Println(fmt.Sprintf("SessionIntoSearchlist(): proc is\n\t%s\n", proc.Inc))
return proc
}
// prunebydate - drop items from searchlist if they are not inside the valid date range
func prunebydate(searchlist []string, s ServerSession) []string {
// 'varia' and 'incerta' have special dates: incerta = 2500; varia = 2000
if s.Earliest == MINDATESTR && s.Latest == MAXDATESTR && s.VariaOK && s.IncertaOK {
// no work for us to do...
return searchlist
}
earliest, _ := strconv.Atoi(s.Earliest)
latest, _ := strconv.Atoi(s.Latest)
e := int64(earliest)
l := int64(latest)
// should have already been validated elsewhere...
if e > l {
e = l
}
// [b5a] first prune the bad dates; nb: the inscriptions have lots of work dates; the gl and lt works don't
var trimmed []string
for _, uid := range searchlist {
cda := AllAuthors[AllWorks[uid].AuID()].ConvDate
cdb := AllWorks[uid].ConvDate
if (cda >= e && cda <= l) || (cdb >= e && cdb <= l) {
trimmed = append(trimmed, uid)
// msg(fmt.Sprintf("added: %s w/ date of %d", uid, cd), 1)
}
}
// [b5b] add back in any varia and/or incerta as needed
if s.VariaOK {
for _, uid := range searchlist {
cda := AllAuthors[AllWorks[uid].AuID()].ConvDate
cdb := AllWorks[uid].ConvDate
if (cda == INCERTADATE || cda == VARIADATE) && cdb == VARIADATE {
trimmed = append(trimmed, uid)
}
}
}
if s.IncertaOK {
for _, uid := range searchlist {
cda := AllAuthors[AllWorks[uid].AuID()].ConvDate
cdb := AllWorks[uid].ConvDate
if (cda == INCERTADATE || cda == VARIADATE) && cdb == INCERTADATE {
trimmed = append(trimmed, uid)
}
}
}
searchlist = trimmed
return searchlist
}
// calculatewholeauthorsearches - find all authors where 100% of works are requested in the searchlist
func calculatewholeauthorsearches(sl []string) [2][]string {
// we have applied all of our inclusions and exclusions by this point, and we might well be sitting on a pile of authorsandworks
// that is really a pile of full author dbs. for example, imagine we have not excluded anything from 'Cicero'
//
// there is no reason to search that DB work by work since that just means doing a series of "WHERE" SearchMap
// instead of a single, faster search of the whole thing: hits are turned into full citations via the info contained in the
// hit itself and there is no need to derive the work from the item name sent to the dispatcher
//
// this function will figure out if the list of work uids contains all the works for an author and can accordingly be collapsed
//start := time.Now()
//previous := time.Now()
var wholes []string
var pruner []string
members := make(map[string]int)
for _, s := range sl {
// count the works
members[s[0:6]] += 1
}
for k, v := range members {
if len(AllAuthors[k].WorkList) == v {
wholes = append(wholes, k)
pruner = append(pruner, AllAuthors[k].WorkList...)
}
}
return [2][]string{wholes, pruner}
}