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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
package skate
import (
"bytes"
"errors"
"reflect"
"regexp"
"runtime"
"strings"
"github.com/segmentio/encoding/json"
"github.com/tidwall/gjson"
"golang.org/x/text/unicode/norm"
)
var (
bTab = []byte("\t")
bNewline = []byte("\n")
ErrZeroFields = errors.New("zero fields")
ErrMissingFieldName = errors.New("missing field name")
wsReplacer = strings.NewReplacer("\t", " ", "\n", " ")
repeatedWs = regexp.MustCompile(`[ ]{2,}`)
nonWord = regexp.MustCompile(`[\W]+`)
SandcrawlerCharMap = map[string]string{
"\u00c6": "AE",
"\u00e6": "ae",
"\u00d0": "D",
"\u00f0": "d",
"\u00d8": "O",
"\u00f8": "o",
"\u00de": "Th",
"\u00fe": "th",
"\u00df": "s",
"\u0110": "D",
"\u0111": "d",
"\u0126": "H",
"\u0127": "h",
"\u0131": "i",
"\u0138": "k",
"\u0141": "L",
"\u0142": "l",
"\u014a": "N",
"\u014b": "n",
"\u0152": "Oe",
"\u0153": "oe",
"\u0166": "T",
"\u0167": "t",
"\u00b5": "u",
"c": "c",
"\u0192": "f",
"\u2202": "",
"\u0296": "",
"\u2211": "",
"\u220f": "",
"\u02c6": "",
"\u2603": "",
"\u02c7": "",
}
SandcrawlerPrefixRemove = []string{
"original article: ", "original article ", "article: ", "title: ",
}
// SandcrawlerPrefixRemove does not have:
// InCombiningDiacriticalMarks (assume it's in "M"),
// https://unicodebook.readthedocs.io/unicode.html,
// https://stackoverflow.com/q/5697171/89391,
// https://github.com/google/re2/wiki/Syntax.
SandcrawlerRemoveCharRegex = regexp.MustCompile("[\\s\\p{P}\\p{M}\u2000-\u206F\u2E00-\u2E7F’\u0060·“”‘’“”«»「」¿–±§_°ʖ©®¤=<>|+$^~≈√∫≤≥÷ƒ∆¬£¢∞¥◊€]")
)
// TitleDoc is a document with a title.
type TitleDoc struct {
Title string `json:"title"`
}
// ContainerNameDoc is a (ref) document with a container title.
type ContainerNameDoc struct {
Biblio struct {
ContainerName string `json:"container_name"`
} `json:"biblio"`
}
// PartialRef for ref docs, that do not have DOI or title. E.g. we found 49701699
// (NCVY), 36401044 (NCVYU), 29668363 (NCUY), and so on. Some examples: XXX.
type PartialRef struct {
ContainerName string `json:"container_name"`
Contribs []struct {
// XXX: Need a way to sensibly compare sets of author names.
RawName string `json:"raw_name"`
} `json:"contribs"`
Volume string `json:"volume"`
Unstructured string `json:"unstructured"`
Year string `json:"release_year"`
}
// cdxSummary is an ad-hoc CDX summary format, created currently by skate-cdx-lookup.
type cdxSummary struct {
Line string `json:"line"`
NumRows int64 `json:"numRows"`
Summary struct {
Delta int64 `json:"delta"`
Last string `json:"last"`
Ok string `json:"ok"`
} `json:"summary"`
}
// Mapper maps a blob to an arbitrary number of fields, e.g. for (key,
// doc). We want fields, but we do not want to bake in TSV into each function.
type Mapper func([]byte) ([][]byte, error)
// AsTSV serializes the result of a field mapper as TSV. This is a slim
// adapter, e.g. to parallel.Processor, which expects this function signature.
// A newline will be appended, if not there already.
//
// Anecdotally a parallelized implementation of a mapper can process around 300MiB/s.
func (f Mapper) AsTSV(p []byte) ([]byte, error) {
var (
fields [][]byte
err error
b []byte
)
if fields, err = f(p); err != nil {
return nil, err
}
if len(fields) == 0 {
return nil, nil
}
b = bytes.Join(fields, bTab)
if len(b) > 0 && !bytes.HasSuffix(b, bNewline) {
b = append(b, bNewline...)
}
return b, nil
}
// WithPrefix is a "mapper middleware", adding a given prefix to the first field.
func WithPrefix(f Mapper, prefix string) Mapper {
return func(p []byte) ([][]byte, error) {
fields, err := f(p)
if err != nil {
return fields, err
}
if len(fields) == 0 {
return nil, ErrZeroFields
}
fields[0] = append([]byte(prefix+":"), fields[0]...)
return fields, err
}
}
// WithBestEffort will not fail on an error.
func WithBestEffort(f Mapper) Mapper {
return func(p []byte) ([][]byte, error) {
if fields, err := f(p); err != nil {
return nil, nil
} else {
return fields, err
}
}
}
// WithSkipOnEmpty ignores results where the value at a given field is empty.
// One indexed.
func WithSkipOnEmpty(f Mapper, index int) Mapper {
return func(p []byte) ([][]byte, error) {
fields, err := f(p)
if err != nil {
return nil, err
}
if index < len(fields) && len(fields[index]) == 0 {
return nil, nil
}
return fields, err
}
}
// NameOf returns name of value, e.g. the name of a function.
func NameOf(f interface{}) string {
v := reflect.ValueOf(f)
if v.Kind() == reflect.Func {
if rf := runtime.FuncForPC(v.Pointer()); rf != nil {
return rf.Name()
}
}
return v.String()
}
// Identifier returns just the input again.
func Identity(p []byte) ([][]byte, error) {
return [][]byte{p}, nil
}
// CreateFixedMapper extract the value from a given fixed json key, e.g.
// "biblio.doi" and the like. Returns a function that maps doc to (value, doc).
func CreateFixedMapper(field string) Mapper {
if len(field) > 0 && field[0] == '.' {
// gjson is not jq, we do not use a leading dot, so remove it, if
// accidentally used here
field = field[1:]
}
f := func(p []byte) ([][]byte, error) {
result := gjson.GetBytes(p, field)
// A subtle bug can emerge here: By default we use tab as separator. If
// the value extracted ends with the separator (e.g. tab), then we get
// an invalid row. Hence, trim all space.
key := []byte(strings.TrimSpace(result.String()))
return [][]byte{key, p}, nil
}
return f
}
// MapperTitle extracts (title, doc).
func MapperTitle(p []byte) ([][]byte, error) {
var (
doc TitleDoc
key []byte
)
if err := json.Unmarshal(p, &doc); err != nil {
return nil, err
} else {
key = []byte(wsReplacer.Replace(strings.TrimSpace(doc.Title)))
}
return [][]byte{key, p}, nil
}
// MapperTitleNormalized extracts (title normalized, doc).
func MapperTitleNormalized(p []byte) (fields [][]byte, err error) {
if fields, err = MapperTitle(p); err != nil {
return nil, err
}
key := string(fields[0])
key = wsReplacer.Replace(strings.TrimSpace(key))
key = strings.ToLower(key)
key = repeatedWs.ReplaceAllString(key, " ")
key = nonWord.ReplaceAllString(key, "")
fields[0] = []byte(key)
return fields, nil
}
// MapperTitleNormalized extracts (title nysiis, doc).
func MapperTitleNysiis(p []byte) (fields [][]byte, err error) {
if fields, err = MapperTitle(p); err != nil {
return nil, err
}
key := string(fields[0])
key = wsReplacer.Replace(strings.TrimSpace(key))
key = NYSIIS(key)
fields[0] = []byte(key)
return fields, nil
}
// MapperTitleSandcrawler extracts (title sandcrawler, doc).
func MapperTitleSandcrawler(p []byte) (fields [][]byte, err error) {
if fields, err = MapperTitle(p); err != nil {
return nil, err
}
key := string(fields[0])
key = sandcrawlerSlugify(wsReplacer.Replace(strings.TrimSpace(key)))
fields[0] = []byte(key)
return fields, nil
}
// MapperContainerName extracts (container_name, doc).
func MapperContainerName(p []byte) ([][]byte, error) {
var (
doc PartialRef
key []byte
)
if err := json.Unmarshal(p, &doc); err != nil {
return nil, err
} else {
key = []byte(wsReplacer.Replace(strings.TrimSpace(doc.ContainerName)))
}
return [][]byte{key, p}, nil
}
// MapperContainerNameSandcrawler extracts (container_name, doc).
func MapperContainerNameSandcrawler(p []byte) (fields [][]byte, err error) {
if fields, err = MapperContainerName(p); err != nil {
return nil, err
}
key := string(fields[0])
key = sandcrawlerSlugify(wsReplacer.Replace(strings.TrimSpace(key)))
fields[0] = []byte(key)
return fields, nil
}
// MapperURLFromRef extracts the (url, doc).
func MapperURLFromRef(p []byte) (fields [][]byte, err error) {
var ref Ref
if err = json.Unmarshal(p, &ref); err != nil {
return nil, err
}
fields = [][]byte{
[]byte(ref.Biblio.Url),
p,
}
return fields, nil
}
// MapperIdentURLFromRef extracts the (work ident, release ident, url, doc).
// Previously: parallel -j 16 --block 100M --pipe "jq -rc '[.work_ident,
// .release_ident, .biblio.url?] | @tsv'" ...
// This implementation seems slightly faster than jq and parallel.
func MapperIdentURLFromRef(p []byte) (fields [][]byte, err error) {
var ref Ref
if err = json.Unmarshal(p, &ref); err != nil {
return nil, err
}
fields = [][]byte{
[]byte(ref.WorkIdent),
[]byte(ref.ReleaseIdent),
[]byte(ref.Biblio.Url),
p,
}
return fields, nil
}
// MapperReleaseContainerName extracts a normalized container name.
func MapperReleaseContainerName(p []byte) (fields [][]byte, err error) {
var (
doc Release
key []byte
w string
)
if err := json.Unmarshal(p, &doc); err != nil {
return nil, err
}
if doc.Container.Name != "" {
w = doc.Container.Name
} else if doc.ContainerName != "" {
w = doc.ContainerName
} else {
w = doc.Extra.ContainerName
}
key = []byte(sandcrawlerSlugify(wsReplacer.Replace(strings.TrimSpace(w))))
return [][]byte{key, p}, nil
}
// MapperReleaseResolvedContainerName extracts slug container name from
// resolved container names.
func MapperReleaseResolvedContainerName(p []byte) (fields [][]byte, err error) {
var (
doc Release
key []byte
)
if err := json.Unmarshal(p, &doc); err != nil {
return nil, err
}
if doc.Extra.Skate.ResolvedContainerName == "" {
return nil, nil
}
key = []byte(sandcrawlerSlugify(wsReplacer.Replace(strings.TrimSpace(doc.Extra.Skate.ResolvedContainerName))))
return [][]byte{key, p}, nil
}
func MapperOpenLibraryReleaseNormalizedISBN(p []byte) (fields [][]byte, err error) {
var (
doc Release
key []byte
isbn13 string
)
if err := json.Unmarshal(p, &doc); err != nil {
return nil, err
}
// There can be 10 and 13 variants in the data, we always want 13.
for _, isbn := range doc.ExtIDs.ISBN {
parsed := ParseIsbn(isbn)
if len(parsed) > 0 {
isbn13 = parsed[0]
break
}
}
if isbn13 == "" {
return nil, nil
} else {
key = []byte(isbn13)
}
return [][]byte{key, p}, nil
}
func MapperCdxSummary(p []byte) (fields [][]byte, err error) {
var (
cdx cdxSummary
)
if err := json.Unmarshal(p, &cdx); err != nil {
return nil, err
}
return [][]byte{[]byte(cdx.Line), p}, nil
}
// MapperPartial works on partial documents.
func MapperPartial(p []byte) (fields [][]byte, err error) {
// TODO: Group by some normlized container name or identifier.
return nil, nil
}
// MapperBrefWork maps bref for comparison with other datasets,
// such as COCI (which uses DOI).
func MapperBrefWork(p []byte) (fields [][]byte, err error) {
var bref BiblioRef
if err := json.Unmarshal(p, &bref); err != nil {
return nil, err
}
return [][]byte{[]byte(bref.SourceWorkIdent), p}, nil
}
// MapperReleaseWork maps release to work ident.
func MapperReleaseWork(p []byte) (fields [][]byte, err error) {
var release Release
if err := json.Unmarshal(p, &release); err != nil {
return nil, err
}
return [][]byte{[]byte(release.WorkID), p}, nil
}
// MapperBrefIdentifierTable generates an id table from biblioref.
func MapperBrefIdentifierTable(p []byte) (field [][]byte, err error) {
var bref struct {
SourceReleaseIdent string `json:"source_release_ident,omitempty"`
TargetReleaseIdent string `json:"target_release_ident,omitempty"`
SourceDOI string `json:"source_doi,omitempty"`
TargetDOI string `json:"target_doi,omitempty"`
}
// There is an issue in the data, just skip over it for the moment.
// 2021/08/05 12:15:00 json: expected ',' after object field value but found '"': "}
if err := json.Unmarshal(p, &bref); err != nil {
return nil, err
}
if bref.SourceDOI == "" || bref.TargetDOI == "" {
return nil, nil
}
return [][]byte{
[]byte(bref.SourceReleaseIdent),
[]byte(bref.TargetReleaseIdent),
[]byte(bref.SourceDOI),
[]byte(bref.TargetDOI),
}, nil
}
// sandcrawlerSlugify normalizes a string.
func sandcrawlerSlugify(s string) string {
slug := strings.ToLower(strings.TrimSpace(s))
for _, prefix := range SandcrawlerPrefixRemove {
if strings.HasPrefix(slug, prefix) {
slug = slug[:len(prefix)]
}
}
slug = strings.ReplaceAll(slug, "'", "'")
for k, v := range SandcrawlerCharMap {
slug = strings.ReplaceAll(slug, k, v)
}
if len(slug) == 0 {
return slug
}
slug = norm.NFKD.String(slug)
slug = SandcrawlerRemoveCharRegex.ReplaceAllString(slug, "")
return strings.ToLower(slug)
}
|