-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmapping.go
More file actions
308 lines (261 loc) · 9.37 KB
/
mapping.go
File metadata and controls
308 lines (261 loc) · 9.37 KB
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
package boogie
import (
"bytes"
"encoding/gob"
"github.com/hscells/cui2vec"
"github.com/hscells/groove/analysis"
"github.com/hscells/groove/combinator"
"github.com/hscells/groove/eval"
"github.com/hscells/groove/learning"
"github.com/hscells/groove/output"
"github.com/hscells/groove/preprocess"
"github.com/hscells/groove/query"
"github.com/hscells/groove/rank"
"github.com/hscells/groove/stats"
"github.com/hscells/merging"
"github.com/hscells/quickumlsrest"
"github.com/hscells/transmute/pipeline"
"github.com/hscells/trecresults"
"io/ioutil"
"log"
"os"
"strings"
)
var (
querySourceMapping = map[string]query.QueriesSource{}
statisticSourceMapping = map[string]stats.StatisticsSource{}
preprocessorMapping = map[string]preprocess.QueryProcessor{}
transformationMappingBoolean = map[string]preprocess.BooleanTransformation{}
transformationMappingElasticsearch = map[string]preprocess.ElasticsearchTransformation{}
measurementMapping = map[string]analysis.Measurement{}
measurementFormatters = map[string]output.MeasurementFormatter{}
evaluationMapping = map[string]eval.Evaluator{}
evaluationFormatters = map[string]output.EvaluationFormatter{}
rewriteTransformationMapping = map[string]learning.Transformation{}
modelMapping = map[string]learning.Model{}
scorers = map[string]rank.Scorer{}
mergers = map[string]merging.Merger{}
)
func RegisterScorer(name string, scorer rank.Scorer) {
scorers[name] = scorer
}
func RegisterMerger(name string, merger merging.Merger) {
mergers[name] = merger
}
// RegisterQuerySource registers a query source.
func RegisterQuerySource(name string, source query.QueriesSource) {
querySourceMapping[name] = source
}
// RegisterStatisticSource registers a statistic source.
func RegisterStatisticSource(name string, source stats.StatisticsSource) {
statisticSourceMapping[name] = source
}
// RegisterPreprocessor registers a preprocessor.
func RegisterPreprocessor(name string, preprocess preprocess.QueryProcessor) {
preprocessorMapping[name] = preprocess
}
// RegisterTransformationBoolean registers a Boolean query transformation.
func RegisterTransformationBoolean(name string, transformation preprocess.BooleanTransformation) {
transformationMappingBoolean[name] = transformation
}
// RegisterTransformationElasticsearch registers an Elasticsearch transformation.
func RegisterTransformationElasticsearch(name string, transformation preprocess.ElasticsearchTransformation) {
transformationMappingElasticsearch[name] = transformation
}
// RegisterMeasurement registers a measurement.
func RegisterMeasurement(name string, measurement analysis.Measurement) {
measurementMapping[name] = measurement
}
// RegisterMeasurementFormatter registers an output formatter.
func RegisterMeasurementFormatter(name string, formatter output.MeasurementFormatter) {
measurementFormatters[name] = formatter
}
// RegisterEvaluator registers a measurement.
func RegisterEvaluator(name string, evaluator eval.Evaluator) {
evaluationMapping[name] = evaluator
}
// RegisterEvaluationFormatter registers an output formatter.
func RegisterEvaluationFormatter(name string, formatter output.EvaluationFormatter) {
evaluationFormatters[name] = formatter
}
// RegisterRewriteTransformation registers a rewrite transformation.
func RegisterRewriteTransformation(name string, transformation learning.Transformation) {
rewriteTransformationMapping[name] = transformation
}
func RegisterCui2VecTransformation(dsl Pipeline) error {
if len(dsl.Utilities.CUI2vec) > 0 && len(dsl.Utilities.CUIMapping) > 0 && len(dsl.Utilities.QuickUMLSCache) > 0 {
var (
embeddings cui2vec.Embeddings
err error
)
f, err := os.OpenFile(dsl.Utilities.CUI2vec, os.O_RDONLY, 0644)
if err != nil {
return err
}
// First, load the embeddings file.
// If the file is a csv, then we can try loading it as such.
if strings.Contains(dsl.Utilities.CUI2vec, ".csv") {
embeddings, err = cui2vec.NewUncompressedEmbeddings(f, dsl.Utilities.CUI2vecSkip, ',')
if err != nil {
return err
}
} else { // Otherwise, we assume the file is a binary file.
embeddings, err = cui2vec.NewPrecomputedEmbeddings(f)
if err != nil {
return err
}
}
// Secondly, load the file that will perform the mapping from CUI->string.
mapping, err := cui2vec.LoadCUIMapping(dsl.Utilities.CUIMapping)
if err != nil {
return err
}
// Lastly, load the file that contains cached cui similarity mappings.
f, err = os.OpenFile(dsl.Utilities.QuickUMLSCache, os.O_RDONLY, os.ModePerm)
if err != nil {
return err
}
var cache quickumlsrest.Cache
err = gob.NewDecoder(f).Decode(&cache)
if err != nil {
return err
}
// Finally, register a client that will communicate to the QuickUMLS REST API.
//quickumls := quickumlsrest.NewClient(dsl.Utilities.QuickUMLSRest)
RegisterRewriteTransformation("cui2vec_expansion", learning.Newcui2vecExpansionTransformer(embeddings, mapping, cache))
}
return nil
}
// RegisterQueryChainCandidateSelector registers a query chain candidate selector.
func RegisterModel(name string, model learning.Model) {
modelMapping[name] = model
}
// NewOracleQueryChainCandidateSelector creates a new oracle query chain candidate selector.
func NewOracleQueryChainCandidateSelector(source string, qrels string) learning.OracleQueryChainCandidateSelector {
b, err := ioutil.ReadFile(qrels)
if err != nil {
panic(err)
}
q, err := trecresults.QrelsFromReader(bytes.NewReader(b))
if err != nil {
panic(err)
}
if ss, ok := statisticSourceMapping[source]; ok {
// TODO the cache should be able to be configured.
return learning.NewOracleQueryChainCandidateSelector(ss, q, combinator.NewMapQueryCache())
}
log.Fatal("could not create oracle query chain candidate selector")
return learning.OracleQueryChainCandidateSelector{}
}
// NewKeywordQuerySource creates a "keyword query" query source.
func NewKeywordQuerySource(options map[string]interface{}) query.QueriesSource {
fields := []string{"text"}
if optionFields, ok := options["fields"].([]interface{}); ok {
fields = make([]string, len(optionFields))
for i, f := range optionFields {
fields[i] = f.(string)
}
}
return query.NewKeywordQuerySource(fields...)
}
// NewTransmuteQuerySource creates a new transmute query source for PubMed/Medline queries.
func NewTransmuteQuerySource(p pipeline.TransmutePipeline, options map[string]interface{}) query.QueriesSource {
if _, ok := options["mapping"]; ok {
if mapping, ok := options["mapping"].(map[string][]string); ok {
p.Options.FieldMapping = mapping
}
}
return query.NewTransmuteQuerySource(p)
}
// NewElasticsearchStatisticsSource attempts to create an Elasticsearch statistics source from a configuration mapping.
// It also tries to set some defaults for fields in case some are not specified, but they will not be sensible.
func NewElasticsearchStatisticsSource(config map[string]interface{}) (*stats.ElasticsearchStatisticsSource, error) {
var esHosts []string
documentType := "doc"
index := "index"
scroll := false
if hosts, ok := config["hosts"]; ok {
for _, host := range hosts.([]interface{}) {
esHosts = append(esHosts, host.(string))
}
} else {
esHosts = []string{"http://localhost:9200"}
}
var searchOptions stats.SearchOptions
if search, ok := config["search"].(map[string]interface{}); ok {
if size, ok := search["size"].(float64); ok {
searchOptions.Size = int(size)
} else {
searchOptions.Size = 1000
}
if runName, ok := search["run_name"].(string); ok {
searchOptions.RunName = runName
} else {
searchOptions.RunName = "run"
}
}
params := map[string]float64{"k": 10, "lambda": 0.5}
if p, ok := config["params"].(map[string]float64); ok {
params = p
}
if d, ok := config["document_type"]; ok {
documentType = d.(string)
}
if i, ok := config["index"]; ok {
index = i.(string)
}
if s, ok := config["scroll"]; ok {
scroll = s.(bool)
}
return stats.NewElasticsearchStatisticsSource(
stats.ElasticsearchDocumentType(documentType),
stats.ElasticsearchIndex(index),
stats.ElasticsearchHosts(esHosts...),
stats.ElasticsearchParameters(params),
stats.ElasticsearchSearchOptions(searchOptions),
stats.ElasticsearchScroll(scroll))
}
func NewEntrezStatisticsSource(config map[string]interface{}, options ...func(source *stats.EntrezStatisticsSource)) (stats.EntrezStatisticsSource, error) {
var (
tool, email, key string
rank = false
)
if d, ok := config["tool"]; ok {
tool = d.(string)
}
if d, ok := config["email"]; ok {
email = d.(string)
}
if d, ok := config["key"]; ok {
key = d.(string)
}
if d, ok := config["rank"]; ok {
rank = d.(bool)
}
var searchOptions stats.SearchOptions
if search, ok := config["search"].(map[string]interface{}); ok {
if size, ok := search["size"].(float64); ok {
searchOptions.Size = int(size)
} else {
searchOptions.Size = 1000
}
if runName, ok := search["run_name"].(string); ok {
searchOptions.RunName = runName
} else {
searchOptions.RunName = "run"
}
}
e, err := stats.NewEntrezStatisticsSource(
stats.EntrezAPIKey(key),
stats.EntrezEmail(email),
stats.EntrezTool(tool),
stats.EntrezOptions(searchOptions),
stats.EntrezRank(rank))
if err != nil {
return e, nil
}
for _, option := range options {
option(&e)
}
return e, nil
}