-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingest.go
217 lines (193 loc) · 6.5 KB
/
ingest.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
package main
import (
"io"
"regexp"
"strconv"
"strings"
"github.com/knakk/kbp/marc"
"github.com/knakk/kbp/marc/marc21"
"github.com/knakk/kbp/rdf"
"github.com/knakk/kbp/rdf/memory"
)
type source uint
const (
sourceOria source = iota
sourceNasjonalbiblioteket
sourceGoogle
sourceLibraryOfCongress
sourceLibraryThing
sourceOpenLibrary
)
func ingestPublication(id rdf.NamedNode, input io.Reader, s source) (*memory.Graph, error) {
switch s {
case sourceOria:
return ingestOria(id, input)
default:
panic("ingestPublication: TODO")
}
}
func ingestOria(id rdf.NamedNode, input io.Reader) (*memory.Graph, error) {
rec, err := marc.NewDecoder(input, marc.MARCXML).Decode()
if err != nil {
return nil, err
}
g := memory.NewGraph()
// Publication class
g.Insert(rdf.Triple{id, rdf.RDFtype, rdf.NewNamedNode("Publication")})
// Publication ISBN and binding (soft/hardcover)
for _, f := range rec.DataFields(marc.Tag020) {
for _, sf := range f.Subfield('q') {
switch sf {
case "ib.", "ib":
g.Insert(rdf.Triple{id, rdf.NewNamedNode("hasBinding"), rdf.NewNamedNode("binding/hardback")})
case "h":
g.Insert(rdf.Triple{id, rdf.NewNamedNode("hasBinding"), rdf.NewNamedNode("binding/paperback")})
}
break
}
for _, sf := range f.Subfield('a') {
g.Insert(rdf.Triple{id, rdf.NewNamedNode("hasISBN"), rdf.NewStrLiteral(sf)})
break
}
break
}
// Publication number of pages
if numPages := marcField(rec, marc.Tag300, 'a'); numPages != "" {
g.Insert(rdf.Triple{id, rdf.NewNamedNode("hasNumPages"), rdf.NewTypedLiteral(cleanNumber(numPages), rdf.XSDint)})
}
title := marcField(rec, marc.Tag245, 'a')
var lang string
if cf, ok := rec.ControlField(marc.Tag008); ok {
lang = strings.TrimSpace(cf.GetPos(marc21.C008Language.Pos, marc21.C008Language.Length))
}
work := rdf.NewBlankNode("work")
g.Insert(
rdf.Triple{id, rdf.NewNamedNode("isPublicationOf"), work},
rdf.Triple{work, rdf.RDFtype, rdf.NewNamedNode("Work")})
if title != "" {
g.Insert(
rdf.Triple{id, rdf.NewNamedNode("hasMainTitle"), rdf.NewStrLiteral(title)})
if lang != "" {
g.Insert(
rdf.Triple{work, rdf.NewNamedNode("hasName"), rdf.NewLangLiteral(title, lang)},
rdf.Triple{work, rdf.NewNamedNode("hasLanguage"), rdf.NewNamedNode("lang/" + lang)})
} else {
g.Insert(rdf.Triple{work, rdf.NewNamedNode("hasName"), rdf.NewStrLiteral(title)})
}
}
// Is work a translation?
isTranslation := false
var origWork rdf.BlankNode
for _, f := range rec.DataFields(marc.Tag246) {
for _, s := range f.Subfield('i') {
if s == "Originaltittel" || s == "originaltittel" {
isTranslation = true
origWork = rdf.NewBlankNode("origWork")
g.Insert(
rdf.Triple{work, rdf.NewNamedNode("isTranslationOf"), origWork},
rdf.Triple{origWork, rdf.RDFtype, rdf.NewNamedNode("Work")})
if len(f.Subfield('a')) > 0 {
g.Insert(rdf.Triple{origWork, rdf.NewNamedNode("hasName"), rdf.NewStrLiteral(f.Subfield('a')[0])})
}
}
}
}
// Work main entry
for _, f := range rec.DataFields(marc.Tag100) {
contrib := rdf.NewBlankNode("mainEntryContrib")
agent := rdf.NewBlankNode("mainEntryAgent")
for _, s := range f.Subfield('a') {
g.Insert(
rdf.Triple{contrib, rdf.RDFtype, rdf.NewNamedNode("Contribution")},
rdf.Triple{contrib, rdf.NewNamedNode("hasRole"), rdf.NewNamedNode("role/author")},
rdf.Triple{contrib, rdf.NewNamedNode("hasAgent"), agent},
rdf.Triple{agent, rdf.RDFtype, rdf.NewNamedNode("Person")},
rdf.Triple{agent, rdf.NewNamedNode("hasName"), rdf.NewStrLiteral(reinvertName(s))})
}
for _, s := range f.Subfield('d') {
years := strings.Split(s, "-")
birthDate := rdf.NewBlankNode("mainEntryAgentBirthDate")
g.Insert(
rdf.Triple{agent, rdf.NewNamedNode("hasBirthDate"), birthDate},
rdf.Triple{birthDate, rdf.RDFtype, rdf.NewNamedNode("Date")},
rdf.Triple{birthDate, rdf.NewNamedNode("hasYear"), rdf.NewTypedLiteral(cleanNumber(years[0]), rdf.XSDint)})
if len(years) == 2 && len(years[1]) > 0 {
deathDate := rdf.NewBlankNode("mainEntryAgentDeathDate")
g.Insert(
rdf.Triple{agent, rdf.NewNamedNode("hasDeathDate"), deathDate},
rdf.Triple{deathDate, rdf.RDFtype, rdf.NewNamedNode("Date")},
rdf.Triple{deathDate, rdf.NewNamedNode("hasYear"), rdf.NewTypedLiteral(cleanNumber(years[1]), rdf.XSDint)},
)
}
}
if isTranslation {
g.Insert(rdf.Triple{origWork, rdf.NewNamedNode("hasContribution"), contrib})
} else {
g.Insert(rdf.Triple{work, rdf.NewNamedNode("hasContribution"), contrib})
}
}
// Other contributions
for i, f := range rec.DataFields(marc.Tag700) {
contrib := rdf.NewBlankNode("contrib" + strconv.Itoa(i))
agent := rdf.NewBlankNode("agent" + strconv.Itoa(i))
for _, s := range f.Subfield('a') {
g.Insert(
rdf.Triple{contrib, rdf.NewNamedNode("hasAgent"), agent},
rdf.Triple{agent, rdf.RDFtype, rdf.NewNamedNode("Person")},
rdf.Triple{agent, rdf.NewNamedNode("hasName"), rdf.NewStrLiteral(reinvertName(s))})
}
g.Insert(
rdf.Triple{work, rdf.NewNamedNode("hasContribution"), contrib},
rdf.Triple{contrib, rdf.RDFtype, rdf.NewNamedNode("Contribution")})
}
// Publication publisher and publish-year
for _, f := range rec.DataFields(marc.Tag260) {
for _, sf := range f.Subfield('c') {
g.Insert(rdf.Triple{id, rdf.NewNamedNode("hasPublishYear"), rdf.NewTypedLiteral(sf, rdf.XSDint)})
break
}
for _, sf := range f.Subfield('b') {
bNode := rdf.NewBlankNode("publisher")
g.Insert(
rdf.Triple{id, rdf.NewNamedNode("hasPublisher"), bNode},
rdf.Triple{bNode, rdf.RDFtype, rdf.NewNamedNode("Publisher")},
rdf.Triple{bNode, rdf.NewNamedNode("hasName"), rdf.NewStrLiteral(sf)})
break
}
break
}
// Work literary form
for _, s := range marcFields(rec, marc.Tag653, 'a') {
if s == "roman" {
g.Insert(
rdf.Triple{work, rdf.NewNamedNode("hasLiteraryForm"), rdf.NewNamedNode("form/novel")})
}
}
// TODO C008 Pos 33
return g, nil
}
func marcField(r *marc.Record, tag marc.DataTag, subfield rune) string {
for _, f := range r.DataFields(tag) {
for _, sf := range f.Subfield(subfield) {
return sf
}
}
return ""
}
func marcFields(r *marc.Record, tag marc.DataTag, subfield rune) (res []string) {
for _, f := range r.DataFields(tag) {
res = append(res, f.Subfield(subfield)...)
}
return res
}
var reDigits = regexp.MustCompile("[0-9]+")
func cleanNumber(s string) string {
return reDigits.FindString(s)
}
func reinvertName(s string) string {
if i := strings.Index(s, ","); i > 0 {
// TODO make it safe from index out of bounds panic
return s[i+2:] + " " + s[:i]
}
return s
}