-
Notifications
You must be signed in to change notification settings - Fork 7
/
record.go
317 lines (285 loc) · 7.56 KB
/
record.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
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
package ceeol
import (
"encoding/xml"
"fmt"
"regexp"
"strings"
"time"
"github.com/miku/span"
"github.com/miku/span/formats/finc"
"github.com/shantanubhadoria/go-roman/roman"
)
var (
// identifierRegexp, since ID is buried in 856.u link.
identifierRegexp = regexp.MustCompile("id=([0-9]*)")
// volumeIssueRegexp for "Vol. 6, no. 4 (2014)-" and the like.
volumeIssueRegexp = regexp.MustCompile(`Vol.[\s]*([MDCLXVI0-9]*),[\s]*no.[\s]*([0-9]*)[\s]*\(([0-9]*\))-`)
)
// Record for MARC-XML data, Ceeol style.
type Record struct {
XMLName xml.Name `xml:"record"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Doc string `xml:"doc,attr"`
Xalan string `xml:"xalan,attr"`
Xsi string `xml:"xsi,attr"`
SchemaLocation string `xml:"schemaLocation,attr"`
Leader struct {
Text string `xml:",chardata"`
} `xml:"leader"`
Controlfield []struct {
Text string `xml:",chardata"`
Tag string `xml:"tag,attr"`
} `xml:"controlfield"`
Datafield []struct {
Text string `xml:",chardata"`
Ind2 string `xml:"ind2,attr"`
Ind1 string `xml:"ind1,attr"`
Tag string `xml:"tag,attr"`
Subfield []struct {
Text string `xml:",chardata"`
Code string `xml:"code,attr"`
} `xml:"subfield"`
} `xml:"datafield"`
}
func (r Record) MustGetControlField(tag string) string {
v, err := r.GetControlField(tag)
if err != nil {
panic(err)
}
return v
}
func (r Record) GetControlField(tag string) (string, error) {
for _, f := range r.Controlfield {
if f.Tag == tag {
return f.Text, nil
}
}
return "", fmt.Errorf("undefined tag: %s", tag)
}
func (r Record) MustGetFirstDataField(spec string) string {
value, err := r.GetFirstDataField(spec)
if err != nil {
panic(err)
}
return value
}
func (r Record) GetFirstDataField(spec string) (string, error) {
values, err := r.GetDataFields(spec)
if err != nil {
return "", err
}
if len(values) == 0 {
return "", nil
}
return values[0], nil
}
func (r Record) MustGetDataFields(spec string) []string {
result, err := r.GetDataFields(spec)
if err != nil {
panic(err)
}
return result
}
func (r Record) GetDataFields(spec string) (result []string, err error) {
parts := strings.Split(spec, ".")
if len(parts) != 2 {
return result, fmt.Errorf("spec must be of the form tag.subfield, like 245.a")
}
tag, subfield := parts[0], parts[1]
for _, f := range r.Datafield {
if f.Tag == tag {
for _, sf := range f.Subfield {
if sf.Code == subfield {
result = append(result, sf.Text)
}
}
}
}
return result, nil
}
// ID returns the identifier, the identifier might be hidden in the URL (e.g.
// <marc:subfield
// code="u">https://www.ceeol.com/search/book-detail?id=279462</marc:subfield>),
// was <UniqueID>279462</UniqueID>.
func (r Record) ID() (string, error) {
values, err := r.GetDataFields("856.u")
if err != nil {
return "", err
}
for _, v := range values {
matches := identifierRegexp.FindStringSubmatch(v)
if len(matches) < 2 {
return "", fmt.Errorf("value without id: %v", v)
}
id := matches[1]
return id, nil
}
return "", fmt.Errorf("missing identifier")
}
// Title returns some title.
func (r Record) Title() (string, error) {
title, err := r.GetFirstDataField("245.a")
if err != nil {
return "", err
}
subtitle, err := r.GetFirstDataField("245.b")
if err != nil {
return "", err
}
title = strings.TrimSpace(title)
subtitle = strings.TrimSpace(subtitle)
if subtitle == "" {
return title, nil
}
return fmt.Sprintf("%s: %s", title, subtitle), nil
}
// RecordFormat returns "book", "article" or "unknown".
func (r Record) RecordFormat() string {
v, _ := r.GetDataFields("022.a")
if len(v) > 0 {
return "article"
}
v, _ = r.GetDataFields("020.a")
if len(v) > 0 {
return "book"
}
return "unknown"
}
// ISSN returns a list of ISSN.
func (r Record) ISSN() (values []string) {
values, _ = r.GetDataFields("022.a")
return values
}
// ISBN returns a list of ISBN.
func (r Record) ISBN() (values []string) {
values, _ = r.GetDataFields("020.a")
return values
}
func (r Record) Abstract() string {
values, _ := r.GetDataFields("520.a")
return strings.Join(values, "\n")
}
func (r Record) Authors() (authors []finc.Author) {
values, _ := r.GetDataFields("100.a")
for _, value := range values {
authors = append(authors, finc.Author{Name: value})
}
values, _ = r.GetDataFields("700.a")
for _, value := range values {
authors = append(authors, finc.Author{Name: value})
}
return authors
}
func (r Record) SubjectHeadings() []string {
values, _ := r.GetDataFields("650.a")
return values
}
func (r Record) Publisher() []string {
values, _ := r.GetDataFields("260.b")
return values
}
func (r Record) Links() []string {
values, _ := r.GetDataFields("856.u")
return values
}
func (r Record) PublicationYear() string {
value, _ := r.GetFirstDataField("260.c")
return value
}
func (r Record) Languages() (langs []string) {
values, _ := r.GetDataFields("041.a")
for _, v := range values {
if len(v) == 3 {
langs = append(langs, v)
continue
}
parts := strings.Split(v, "/")
for _, p := range parts {
if len(p) == 3 {
langs = append(langs, p)
continue
}
if li := span.LanguageIdentifier(p); li != "" {
langs = append(langs, li)
}
}
}
return
}
// Volume tries to parse volume from "Vol. 6, no. 4 (2014)-" and similar.
func (r Record) Volume() string {
value, _ := r.GetFirstDataField("362.a")
matches := volumeIssueRegexp.FindStringSubmatch(value)
if len(matches) == 4 {
if v, err := roman.ToIndoArabic(matches[1]); err != nil {
return matches[1]
} else {
return fmt.Sprintf("%d", v)
}
}
return ""
}
// Issue tries to parse the issue from "Vol. 6, no. 4 (2014)-" and similar.
func (r Record) Issue() string {
value, _ := r.GetFirstDataField("362.a")
matches := volumeIssueRegexp.FindStringSubmatch(value)
if len(matches) == 4 {
if v, err := roman.ToIndoArabic(matches[2]); err != nil {
return matches[2]
} else {
return fmt.Sprintf("%d", v)
}
}
return ""
}
func (r Record) Places() (places []string) {
values, _ := r.GetDataFields("260.a")
for _, v := range values {
places = append(places, strings.TrimSpace(strings.Replace(v, "[1] :", "", -1)))
}
return places
}
// ToIntermediateSchema converts CEEOL marcxml data into intermediate schema.
func (r Record) ToIntermediateSchema() (*finc.IntermediateSchema, error) {
output := finc.NewIntermediateSchema()
v, err := r.ID()
if err != nil {
return output, span.Skip{Reason: err.Error()}
}
output.RecordID = v
output.ID = fmt.Sprintf("ai-53-%s", output.RecordID)
v, err = r.Title()
if err != nil {
return output, span.Skip{
Reason: fmt.Sprintf("missing title: %s", output.RecordID)}
}
switch r.RecordFormat() {
case "book":
output.BookTitle = v
output.Genre = "book" // XXX: Is it?
output.Format = "Book" // XXX: Is it?
output.RefType = "EBOOK" // XXX: Check.
default:
output.ArticleTitle = v
output.Genre = Genre
output.Format = Format
output.RefType = DefaultRefType
}
output.SourceID = SourceIdentifier
output.MegaCollections = []string{Collection, TechnicalCollectionID}
output.ISSN = r.ISSN()
output.ISBN = r.ISBN()
output.Abstract = r.Abstract()
output.Authors = r.Authors()
output.Subjects = r.SubjectHeadings()
output.Publishers = r.Publisher()
output.URL = r.Links()
output.RawDate = fmt.Sprintf("%s-01-01", r.PublicationYear())
output.Date, err = time.Parse("2006-01-02", fmt.Sprintf("%s-01-01", r.PublicationYear()))
output.Languages = r.Languages()
output.Volume = r.Volume()
output.Issue = r.Issue()
output.Places = r.Places()
return output, nil
}