/
formulator.go
336 lines (290 loc) · 8.63 KB
/
formulator.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
// Package formulation provides a library for automatically formulating queries.
package formulation
import (
"fmt"
"github.com/hscells/cqr"
"github.com/hscells/groove/eval"
"github.com/hscells/groove/pipeline"
"github.com/hscells/groove/stats"
"github.com/hscells/guru"
"github.com/hscells/trecresults"
"github.com/olivere/elastic/v7"
"strconv"
)
// Formulator formulates queries to some specification.
type Formulator interface {
Formulate(query pipeline.Query) ([]cqr.CommonQueryRepresentation, []pipeline.SupplementalData, error)
Method() string
}
// ConceptualFormulator formulates queries using the title or string of a systematic review.
type ConceptualFormulator struct {
LogicComposer
EntityExtractor
EntityExpander
KeywordMapper
s stats.EntrezStatisticsSource
FeedbackDocs []int
postProcessing []PostProcess
}
// ObjectiveFormulator formulates queries according to the objective approach.
// This implementation writes files to disk as a side effect swhich can be later be used for analysis.
type ObjectiveFormulator struct {
seed int
Folder, Pubdates, SemTypes, MetaMapURL string
query pipeline.Query
s stats.EntrezStatisticsSource
qrels trecresults.QrelsFile
MeSHK []int
DevK, PopK []float64
minDocs int
elastic *elastic.Client
st map[string]guru.SemType
population BackgroundCollection
splitter Splitter
analyser TermAnalyser
analyserName string
postProcessing []PostProcess
optimisation eval.Evaluator
}
type ObjectiveOption func(o *ObjectiveFormulator)
func ObjectiveGrid(devK, popK []float64, meshK []int) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.DevK = devK
o.PopK = popK
o.MeSHK = meshK
}
}
func ObjectiveSplitter(spitter Splitter) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.splitter = spitter
}
}
func ObjectiveAnalyser(analyser TermAnalyser, name string) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.analyser = analyser
o.analyserName = name
}
}
func ObjectivePostProcessing(processes ...PostProcess) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.postProcessing = processes
}
}
func ObjectiveMinDocs(docs int) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.minDocs = docs
}
}
func ObjectiveOptimisation(optimisation eval.Evaluator) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.optimisation = optimisation
}
}
func ObjectiveQrels(rels trecresults.QrelsFile) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.qrels = rels
}
}
func ObjectivePopulation(population BackgroundCollection) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.population = population
}
}
func ObjectiveQuery(query pipeline.Query) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.query = query
}
}
func ObjectiveSeed(seed int) ObjectiveOption {
return func(o *ObjectiveFormulator) {
o.seed = seed
}
}
func NewObjectiveFormulator(s stats.EntrezStatisticsSource, esClient *elastic.Client, qrels trecresults.QrelsFile, population BackgroundCollection, folder, pubdates, semTypes, metamapURL string, optimisation eval.Evaluator, options ...ObjectiveOption) *ObjectiveFormulator {
t := guru.LoadSemTypes(guru.SEMTYPES)
x := make(map[string]guru.SemType)
for _, v := range t {
x[v.TUI] = v
}
o := &ObjectiveFormulator{
s: s,
qrels: qrels,
population: population,
Folder: folder,
Pubdates: pubdates,
SemTypes: semTypes,
MetaMapURL: metamapURL,
splitter: RandomSplitter(1000),
analyser: TermFrequencyAnalyser,
optimisation: optimisation,
minDocs: 50,
st: x,
elastic: esClient,
//DevK: []float64{0.20},
//PopK: []float64{0.02},
//MeSHK: []int{20},
DevK: []float64{0.15, 0.20, 0.25, 0.30},
PopK: []float64{0.001, 0.01, 0.02},
MeSHK: []int{1, 5, 10, 15, 20},
}
for _, option := range options {
option(o)
}
return o
}
func (o ObjectiveFormulator) Derive() (cqr.CommonQueryRepresentation, cqr.CommonQueryRepresentation, []guru.MedlineDocument, []guru.MedlineDocument, []guru.MedlineDocument, error) {
// Identify the relevant studies using relevance assessments.
var docs []int
var nonrel []*trecresults.Qrel
for _, rel := range o.qrels.Qrels[o.query.Topic] {
if rel.Score > 0 {
v, err := strconv.Atoi(rel.DocId)
if err != nil {
panic(err)
}
docs = append(docs, v)
} else {
nonrel = append(nonrel, rel)
}
}
if len(docs) >= o.minDocs {
docs = docs[:o.minDocs]
}
// Fetch those relevant documents.
test, err := fetchDocuments(docs, o.s)
if err != nil {
panic(err)
}
fmt.Printf("%d documents in test set\n", len(test))
// Split the 'test' set into dev, val, and unseen.
dev, val, unseen := o.splitter.Split(test)
fmt.Printf("%d dev, %d val, %d unseen\n", len(dev), len(val), len(unseen))
// Perform 'term frequency analysis' on the development set.
devTerms, err := o.analyser(dev)
if err != nil {
panic(err)
}
q1, q2, err := o.derive(devTerms, dev, val, o.population, o.optimisation)
if err != nil {
return nil, nil, nil, nil, nil, err
}
// Post-Processing.
for _, postProcessor := range o.postProcessing {
q1, err = postProcessor(q1)
if err != nil {
return nil, nil, nil, nil, nil, err
}
q2, err = postProcessor(q2)
if err != nil {
return nil, nil, nil, nil, nil, err
}
}
return q1, q2, unseen, dev, val, nil
}
// Formulate returns two queries: one with MeSH terms and one without. It also returns the set of unseen documents for evaluation later.
func (o ObjectiveFormulator) Formulate(query pipeline.Query) ([]cqr.CommonQueryRepresentation, []pipeline.SupplementalData, error) {
o.query = query
q1, q2, unseen, dev, val, err := o.Derive()
if err != nil {
return nil, nil, err
}
resNoMesh, err := o.s.Execute(pipeline.NewQuery("objective_nomesh", o.Topic(), q1), o.s.SearchOptions())
if err != nil {
return nil, nil, err
}
resMesh, err := o.s.Execute(pipeline.NewQuery("objective_mesh", o.Topic(), q2), o.s.SearchOptions())
if err != nil {
return nil, nil, err
}
sup := pipeline.SupplementalData{
Name: "objective",
Data: []pipeline.Data{
{
Name: "unseen.qrels",
Value: MakeQrels(unseen, o.Topic()),
},
{
Name: "dev.qrels",
Value: MakeQrels(dev, o.Topic()),
},
{
Name: "val.qrels",
Value: MakeQrels(val, o.Topic()),
},
{
Name: "without_mesh.res",
Value: resNoMesh,
},
{
Name: "with_mesh.res",
Value: resMesh,
},
},
}
return []cqr.CommonQueryRepresentation{q1, q2}, []pipeline.SupplementalData{sup}, nil
}
func (o ObjectiveFormulator) Method() string {
return "objective" + o.optimisation.Name()
}
func (o ObjectiveFormulator) Topic() string {
return o.query.Topic
}
func NewConceptualFormulator(logicComposer LogicComposer, entityExtractor EntityExtractor, entityExpander EntityExpander, keywordMapper KeywordMapper, rf []int, e stats.EntrezStatisticsSource, postProcessing ...PostProcess) *ConceptualFormulator {
return &ConceptualFormulator{
LogicComposer: logicComposer,
EntityExtractor: entityExtractor,
EntityExpander: entityExpander,
KeywordMapper: keywordMapper,
postProcessing: postProcessing,
FeedbackDocs: rf,
s: e,
}
}
func (t ConceptualFormulator) Formulate(query pipeline.Query) ([]cqr.CommonQueryRepresentation, []pipeline.SupplementalData, error) {
// Query Logic Composition.
q, err := t.LogicComposer.Compose(query)
if err != nil {
return nil, nil, err
}
// Entity Extraction.
if t.EntityExtractor != nil {
q, err = t.EntityExtractor.Extract(q)
if err != nil {
return nil, nil, err
}
}
// Entity Expansion.
if t.EntityExpander != nil {
q, err = EntityExpansion(q, t.EntityExpander)
if err != nil {
return nil, nil, err
}
}
// Relevance Feedback.
if len(t.FeedbackDocs) > 0 {
docs, err := t.s.Fetch(t.FeedbackDocs)
if err != nil {
return nil, nil, err
}
q, err = RelevanceFeedback(q, docs, t.EntityExtractor.(MetaMapEntityExtractor).client)
if err != nil {
return nil, nil, err
}
}
// Entities to Keywords Mapping.
q, err = MapKeywords(q, t.KeywordMapper)
if err != nil {
return nil, nil, err
}
// Post-Processing.
for _, postProcessor := range t.postProcessing {
q, err = postProcessor(q)
if err != nil {
return nil, nil, err
}
}
return []cqr.CommonQueryRepresentation{q}, nil, nil
}
func (t ConceptualFormulator) Method() string {
return "conceptual"
}