aboutsummaryrefslogtreecommitdiffstats
path: root/skate/cmd/skate-reduce/main.go
blob: 2ff7de48b89107e4f3ef5d182adccdcb87bb3ea6 (plain)
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
// skate-reduce takes prepared inputs (e.g. from skate-map or skate-cluster)
// and applies various verification and conversion functions. The output will
// often be a stream of biblioref schema docs.
//
// Support various "modes", e.g. exact, verify, ref, bref, wiki. Each mode may
// work on one or two files, and may need extra args.
//
// * exact     | takes two (key, doc) TSV files (one for releases, one for refs) and
//             | will emit biblioref docs relating *one* element from releases with *all*
//             | elements from ref; this is for "doi", "pmid" and other id matches, where no
//             | further checks are necessary. The match reason, e.g. "doi" needs to be
//             | supplied.
//             |
//             | $ skate-reduce -m exact -r doi -F a.tsv -L b.tsv
//             |
//             |
// * fuzzy     | takes two (key, doc) TSV files (one for release, one for refs),
//             | runs verification within a group and will emit biblioref.
//             |
//             | $ skate-reduce -m fuzzy -F a.tsv -L b.tsv
//             |
//             |
// * ref       | takes a single file with clusters containing releases and refs and
//             | will emit verification results (deprecated).
//             |
//             | $ skate-reduce -m ref < a.ndj
//             |
//             |
// * bref      | same as ref, but generate a biblioref file as output (deprecated).
//             |
//             | $ skate-reduce -m bref < a.ndj
//             |
//             |
// * wiki      | zippy mode for releases and wikipedia inputs.
//             |
//             | $ skate-reduce -m wiki -L a.ndj -W b.ndj
//             |
//             |
// * oledt     | zippy mode for releases and OL inputs, dumps table for debugging.
//             |
//             | $ skate-reduce -m oled -F a.ndj -O b.ndj
//             |
//             |
// * oled      | zippy mode for releases and OL inputs, emit biblioref.
//             |
//             | $ skate-reduce -m oled -F a.ndj -O b.ndj
//             |
//             |
// * rere      | zippy mode for OL release and ref (as release) inputs, emit biblioref.
//             |
//             | $ skate-reduce -m rere -O a.ndj -F b.ndj
//             |
//             |
// * unmatched | join matched and unmatched reference data; do deduplicate on the fly
//             |
//             | $ skate-reduce -m unmatched -B a.ndj -F b.ndj
//             |
//             |
// * wb        | join raw refs with urls with ad-hoc wayback json schema noting last timestamp
//             |
//             | $ skate-reduce -m wb -F a.ndj -C b.ndj
//
package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"runtime"

	"git.archive.org/martin/cgraph/skate"
	"git.archive.org/martin/cgraph/skate/parallel"
	"git.archive.org/martin/cgraph/skate/xio"
	gzip "github.com/klauspost/compress/gzip"
)

var (
	numWorkers = flag.Int("w", runtime.NumCPU(), "number of workers")
	batchSize  = flag.Int("b", 10000, "batch size")
	mode       = flag.String("m", "ref", "mode, e.g. exact, fuzzy, ref, bref, wiki, oled, oledt, unmatched")
	quite      = flag.Bool("q", false, "be quiet")
	logFile    = flag.String("log", "", "log filename (stderr, if empty; logfile will be gzip compressed)")

	// Possible inputs -- we could switch to a subcommand cli parser?
	bref        = flag.String("B", "", "path to bref file")
	refs        = flag.String("F", "", "path to refs input")
	releases    = flag.String("L", "", "path to release input")
	wiki        = flag.String("W", "", "path to wiki input")
	openLibrary = flag.String("O", "", "path to open library input") // XXX: Too generic name.
	cdxAdhoc    = flag.String("C", "", "path to cdx adhoc schema")

	// Extra args.
	reason    = flag.String("r", "", "reason for match: doi, pmid, pmcid, arxiv, unknown")
	reasonMap = map[string]skate.MatchResult{
		"doi":     skate.MatchResult{Status: skate.StatusExact, Reason: skate.ReasonDOI},
		"pmid":    skate.MatchResult{Status: skate.StatusExact, Reason: skate.ReasonPMID},
		"pmcid":   skate.MatchResult{Status: skate.StatusExact, Reason: skate.ReasonPMCID},
		"arxiv":   skate.MatchResult{Status: skate.StatusExact, Reason: skate.ReasonArxiv},
		"unknown": skate.MatchResult{Status: skate.StatusUnknown, Reason: skate.ReasonUnknown},
		"isbn":    skate.MatchResult{Status: skate.StatusExact, Reason: skate.ReasonISBN},
	}

	help = `Reduce modes

TODO: table of modes and inputs, or specific link to docs
`
)

func main() {
	flag.Usage = func() {
		fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Println()
		fmt.Println(help)
	}
	flag.Parse()
	if *logFile != "" {
		f, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			log.Fatal(err)
		}
		gzw := gzip.NewWriter(f)
		defer gzw.Flush()
		defer f.Close()
		log.SetOutput(gzw)
	}
	if *quite {
		log.SetOutput(ioutil.Discard)
	}
	bw := bufio.NewWriter(os.Stdout)
	defer bw.Flush()
	switch *mode {
	case "exact":
		l, f, err := xio.OpenTwo(*releases, *refs)
		if err != nil {
			log.Fatal(err)
		}
		r, ok := reasonMap[*reason]
		if !ok {
			log.Fatalf("unknown reason: %v", *reason)
		}
		if err := skate.ZippyExact(l, f, r, bw); err != nil {
			log.Fatal(err)
		}
	case "fuzzy":
		l, f, err := xio.OpenTwo(*releases, *refs)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyVerifyRefs(l, f, bw); err != nil {
			log.Fatal(err)
		}
	case "ref":
		pp := parallel.NewProcessor(os.Stdin, os.Stdout, skate.RefClusterVerify)
		pp.NumWorkers = *numWorkers
		pp.BatchSize = *batchSize
		if err := pp.Run(); err != nil {
			log.Fatal(err)
		}
	case "bref":
		pp := parallel.NewProcessor(os.Stdin, os.Stdout, skate.RefClusterToBiblioRef)
		pp.NumWorkers = *numWorkers
		pp.BatchSize = *batchSize
		if err := pp.Run(); err != nil {
			log.Fatal(err)
		}
	case "wiki":
		l, w, err := xio.OpenTwo(*releases, *wiki)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyExactWiki(l, w, reasonMap["doi"], bw); err != nil {
			log.Fatal(err)
		}
	case "oledt":
		o, f, err := xio.OpenTwo(*openLibrary, *refs)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyVerifyRefsOpenLibraryTable(o, f, bw); err != nil {
			log.Fatal(err)
		}
	case "oled":
		o, f, err := xio.OpenTwo(*openLibrary, *refs)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyVerifyRefsOpenLibrary(o, f, bw); err != nil {
			log.Fatal(err)
		}
	case "rere":
		o, f, err := xio.OpenTwo(*releases, *refs)
		if err != nil {
			log.Fatal(err)
		}
		r, ok := reasonMap[*reason]
		if !ok {
			log.Fatalf("unknown reason: %v", *reason)
		}
		if err := skate.ZippyExactReleases(o, f, r, bw); err != nil {
			log.Fatal(err)
		}
	case "unmatched":
		b, f, err := xio.OpenTwo(*bref, *refs)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyBrefAugment(b, f, bw); err != nil {
			log.Fatal(err)
		}
	case "wb":
		f, c, err := xio.OpenTwo(*refs, *cdxAdhoc)
		if err != nil {
			log.Fatal(err)
		}
		if err := skate.ZippyWayback(f, c, bw); err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatalf("invalid mode")
	}
}