-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
272 lines (242 loc) · 7.13 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
262
263
264
265
266
267
268
269
270
271
272
// span-tag takes an intermediate schema file and a configuration forest of
// filters for various tags and runs all filters on every record of the input
// to produce a stream of tagged records.
//
// TODO(miku): Allow to skip label attachment by inspecting a SOLR index on the
// fly. Calculate label attachments for record, query index for doi or similar
// id, if the preferred source is already in the index, drop the label. If the
// unpreferred source is indexed, we cannot currently update the index, so just
// emit a warning and do not change anything.
//
// $ span-tag -c '{"DE-15": {"any": {}}}' < input.ldj > output.ldj
//
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime"
"runtime/pprof"
"strings"
log "github.com/sirupsen/logrus"
"github.com/miku/span"
"github.com/miku/span/filter"
"github.com/miku/span/formats/finc"
"github.com/miku/span/parallel"
"github.com/miku/span/solrutil"
)
var (
config = flag.String("c", "", "JSON config file for filters")
version = flag.Bool("v", false, "show version")
size = flag.Int("b", 20000, "batch size")
numWorkers = flag.Int("w", runtime.NumCPU(), "number of workers")
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to file")
unfreeze = flag.String("unfreeze", "", "unfreeze filterconfig from a frozen file")
verbose = flag.Bool("verbose", false, "verbose output")
server = flag.String("server", "", "if not empty, query SOLR to deduplicate on-the-fly")
prefs = flag.String("prefs", "85 55 89 60 50 105 34 101 53 49 28 48 121", "most preferred source id first, for deduplication")
ignoreSameIdentifier = flag.Bool("isi", false, "when doing deduplication, ignore matches in index with the same id")
)
// SelectResponse with reduced fields.
type SelectResponse struct {
Response struct {
Docs []struct {
ID string `json:"id"`
Institution []string `json:"institution"`
SourceID string `json:"source_id"`
} `json:"docs"`
NumFound int64 `json:"numFound"`
Start int64 `json:"start"`
} `json:"response"`
ResponseHeader struct {
Params struct {
Q string `json:"q"`
Wt string `json:"wt"`
} `json:"params"`
QTime int64
Status int64 `json:"status"`
} `json:"responseHeader"`
}
// stringSliceContains returns true, if a given string is contained in a slice.
func stringSliceContains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}
// preferencePosition returns the position of a given preference as int.
// Smaller means preferred. If there is no match, return some higher number
// (low prio).
func preferencePosition(sid string) int {
fields := strings.Fields(*prefs)
for pos, v := range fields {
v = strings.TrimSpace(v)
if v == sid {
return pos
}
}
return 1000 // Or anything higher than the number of sources.
}
// DroppableLabels returns a list of labels, that can be dropped with regard to
// an index. If document has no DOI, there is nothing to return.
func DroppableLabels(is finc.IntermediateSchema) (labels []string, err error) {
doi := strings.TrimSpace(is.DOI)
if doi == "" {
return
}
link := fmt.Sprintf(`%s/select?df=allfields&wt=json&q="%s"`, *server, url.QueryEscape(doi))
if *verbose {
log.Printf("[%s] fetching: %s", is.ID, link)
}
resp, err := http.Get(link)
if err != nil {
return labels, err
}
defer resp.Body.Close()
var sr SelectResponse
// Keep response for debugging.
var buf bytes.Buffer
tee := io.TeeReader(resp.Body, &buf)
if err := json.NewDecoder(tee).Decode(&sr); err != nil {
log.Printf("[%s] failed link: %s", is.ID, link)
log.Printf("[%s] failed response: %s", is.ID, buf.String())
return labels, err
}
// Ignored merely counts the number of docs, that had the same id in the index, for logging.
var ignored int
for _, label := range is.Labels {
// For each label (ISIL), see, whether any match in SOLR has the same
// label (ISIL) as well.
for _, doc := range sr.Response.Docs {
if *ignoreSameIdentifier && doc.ID == is.ID {
ignored++
continue
}
if !stringSliceContains(doc.Institution, label) {
continue
}
// The document (is) might be already in the index (same or other source).
if preferencePosition(is.SourceID) >= preferencePosition(doc.SourceID) {
// The prio position of the document is higher (means: lower prio). We may drop this label.
labels = append(labels, label)
break
} else {
log.Printf("%s (%s) has lower prio in index, but we cannot update index docs yet, skipping", is.ID, doi)
}
}
}
if ignored > 0 && *verbose {
log.Printf("[%s] ignored %d docs", is.ID, ignored)
}
return labels, nil
}
// removeEach returns a new slice with element not in a drop list.
func removeEach(ss []string, drop []string) (result []string) {
for _, s := range ss {
if !stringSliceContains(drop, s) {
result = append(result, s)
}
}
return
}
func main() {
flag.Parse()
if *version {
fmt.Println(span.AppVersion)
os.Exit(0)
}
if *config == "" && *unfreeze == "" {
log.Fatal("config file required")
}
if *cpuProfile != "" {
file, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(file)
defer pprof.StopCPUProfile()
}
if *server != "" {
*server = solrutil.PrependHTTP(*server)
}
// The configuration forest.
var tagger filter.Tagger
if *unfreeze != "" {
dir, filterconfig, err := span.UnfreezeFilterConfig(*unfreeze)
if err != nil {
log.Fatal(err)
}
log.Printf("[span-tag] unfrooze filterconfig to: %s", filterconfig)
defer os.RemoveAll(dir)
*config = filterconfig
}
// Test, if we are given JSON directly.
err := json.Unmarshal([]byte(*config), &tagger)
if err != nil {
// Fallback to parse config file.
f, err := os.Open(*config)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&tagger); err != nil {
log.Fatal(err)
}
}
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
var reader io.Reader = os.Stdin
if flag.NArg() > 0 {
var files []io.Reader
for _, filename := range flag.Args() {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
files = append(files, f)
}
reader = io.MultiReader(files...)
}
p := parallel.NewProcessor(bufio.NewReader(reader), w, func(_ int64, b []byte) ([]byte, error) {
var is finc.IntermediateSchema
if err := json.Unmarshal(b, &is); err != nil {
return b, err
}
tagged := tagger.Tag(is)
// Deduplicate against a SOLR.
if *server != "" {
droppable, err := DroppableLabels(tagged)
if err != nil {
log.Fatal(err)
}
if len(droppable) > 0 {
before := len(tagged.Labels)
tagged.Labels = removeEach(tagged.Labels, droppable)
if *verbose {
log.Printf("[%s] from %d to %d labels: %s",
is.ID, before, len(tagged.Labels), tagged.Labels)
}
}
}
bb, err := json.Marshal(tagged)
if err != nil {
return bb, err
}
bb = append(bb, '\n')
return bb, nil
})
p.NumWorkers = *numWorkers
p.BatchSize = *size
if err := p.Run(); err != nil {
log.Fatal(err)
}
}