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
|
// skate-from-unstructured tries to parse various pieces of information from
// the unstructured field in refs.
package main
import (
"flag"
"log"
"os"
"regexp"
"runtime"
"strings"
"git.archive.org/martin/cgraph/skate"
"git.archive.org/martin/cgraph/skate/parallel"
json "github.com/segmentio/encoding/json"
)
var (
numWorkers = flag.Int("w", runtime.NumCPU(), "number of workers")
batchSize = flag.Int("b", 100000, "batch size")
bytesNewline = []byte("\n")
PatDOI = regexp.MustCompile(`10[.][0-9]{1,8}/[^ ]*[\w]`)
PatDOINoHyphen = regexp.MustCompile(`10[.][0-9]{1,8}/[^ -]*[\w]`)
PatArxivPDF = regexp.MustCompile(`http://arxiv.org/pdf/([0-9]{4,4}[.][0-9]{1,8})(v[0-9]{1,2})?(.pdf)?`)
PatArxivAbs = regexp.MustCompile(`http://arxiv.org/abs/([0-9]{4,4}[.][0-9]{1,8})(v[0-9]{1,2})?(.pdf)?`)
)
func main() {
pp := parallel.NewProcessor(os.Stdin, os.Stdout, func(p []byte) ([]byte, error) {
var ref skate.Ref
if err := json.Unmarshal(p, &ref); err != nil {
return nil, err
}
if err := parseUnstructured(&ref); err != nil {
return nil, err
}
return skate.JsonMarshalLine(&ref)
})
pp.NumWorkers = *numWorkers
pp.BatchSize = *batchSize
if err := pp.Run(); err != nil {
log.Fatal(err)
}
}
// parseUnstructured will in-place augment missing DOI, arxiv id and so on.
func parseUnstructured(ref *skate.Ref) error {
uns := ref.Biblio.Unstructured
var (
v string
vs []string
)
// Handle things like: 10.1111/j.1550-7408.1968.tb02138.x-BIB5|cit5,
// 10.1111/j.1558-5646.1997.tb02431.x-BIB0008|evo02431-cit-0008, ...
if strings.Contains(strings.ToLower(ref.Key), "-bib") && ref.Biblio.DOI == "" {
parts := strings.Split(strings.ToLower(ref.Key), "-bib")
ref.Biblio.DOI = parts[0]
}
// DOI
v = PatDOI.FindString(uns)
if v != "" && ref.Biblio.DOI == "" {
ref.Biblio.DOI = v
}
// DOI in Key
v = PatDOINoHyphen.FindString(ref.Key)
if v != "" && ref.Biblio.DOI == "" {
ref.Biblio.DOI = v
}
// DOI in URL
prefixes := []string{
"http://doi.org/",
"https://doi.org/",
"http://dx.doi.org/",
"https://dx.doi.org/",
}
for _, prefix := range prefixes {
if ref.Biblio.DOI != "" && strings.HasPrefix(ref.Biblio.Url, prefix) {
ref.Biblio.DOI = strings.Replace(ref.Biblio.Url, prefix, "", -1)
}
}
v = PatDOINoHyphen.FindString(ref.Key)
if v != "" && ref.Biblio.DOI == "" {
ref.Biblio.DOI = v
}
// Arxiv
vs = PatArxivPDF.FindStringSubmatch(uns)
if len(vs) != 0 && ref.Biblio.ArxivId == "" {
ref.Biblio.ArxivId = vs[1]
} else {
vs = PatArxivAbs.FindStringSubmatch(uns)
if len(vs) != 0 && ref.Biblio.ArxivId == "" {
ref.Biblio.ArxivId = vs[1]
}
}
return nil
}
|