Class: BibTeX::Entry::RDFConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/bibtex/entry/rdf_converter.rb

Constant Summary collapse

DEFAULT_REMOVE_FROM_FALLBACK =
%w(
  bdsk-file-1
  bdsk-file-2
  bdsk-file-3
  bdsk-file-4
).map(&:intern).freeze
BIBO_TYPES =
Hash[*%w{
  article        Article
  book           Book
  booklet        Book
  collection     Collection
  conference     AcademicArticle
  inbook         BookSection
  incollection   BookSection
  inproceedings  AcademicArticle
  journal        Journal
  manual         Manual
  mastersthesis  Thesis
  online         Website
  patent         Patent
  periodical     Periodical
  phdthesis      Thesis
  proceedings    Proceedings
  standard       Standard
  techreport     Report
  thesis         Thesis
  unpublished    Manuscript
}.map(&:intern)].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bibtex, graph = RDF::Graph.new, agent = {}) ⇒ RDFConverter

Returns a new instance of RDFConverter.

Parameters:



41
42
43
44
45
# File 'lib/bibtex/entry/rdf_converter.rb', line 41

def initialize(bibtex, graph = RDF::Graph.new, agent = {})
  @bibtex = bibtex
  @graph = graph
  @agent = agent
end

Class Method Details

.convert(bibtex, graph = RDF::Graph.new, agent = {}) ⇒ RDF::Graph

converts a BibTeX entry to RDF

Returns:

  • (RDF::Graph)

    the RDF graph of this entry



36
37
38
# File 'lib/bibtex/entry/rdf_converter.rb', line 36

def self.convert(bibtex, graph = RDF::Graph.new, agent = {})
  new(bibtex, graph, agent).convert!
end

Instance Method Details

#abstractObject



61
62
63
64
65
66
67
# File 'lib/bibtex/entry/rdf_converter.rb', line 61

def abstract
  return unless bibtex.field?(:abstract)
  remove_from_fallback(:abstract)

  graph << [entry, RDF::DC.abstract, bibtex[:abstract].to_s]
  graph << [entry, bibo[:abstract], bibtex[:abstract].to_s]
end

#annoteObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/bibtex/entry/rdf_converter.rb', line 69

def annote
  return unless bibtex.field?(:annote)
  remove_from_fallback(:annote)

  pub = RDF::Node.new
  graph << [pub, RDF.type, bibo[:Note]]
  graph << [pub, bibo[:content], bibtex[:annote]]

  graph << [entry, bibo[:annotates], pub]
end

#authorObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bibtex/entry/rdf_converter.rb', line 80

def author
  return unless bibtex.field?(:author)
  remove_from_fallback(:author)

  seq = RDF::Node.new

  graph << [seq, RDF.type, RDF[:Seq]]
  graph << [entry, bibo[:authorList], seq]

  bibtex[:author].each do |name|
    node = agent(name) { create_agent(name, :Person) }

    graph << [entry, RDF::DC.creator, node]
    graph << [seq, RDF.li, node]
  end
end

#bdsk_urlObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/bibtex/entry/rdf_converter.rb', line 97

def bdsk_url
  count = 1
  while bibtex.field?("bdsk-url-#{count}".to_sym) do
    field = "bdsk-url-#{count}".to_sym
    remove_from_fallback(field)
    graph << [entry, RDF::DC.URI, bibtex[field].to_s]
    graph << [entry, bibo[:uri], bibtex[field].to_s]
    count  += 1
  end
end

#booktitleObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/bibtex/entry/rdf_converter.rb', line 108

def booktitle
  return unless bibtex.field?(:booktitle)
  remove_from_fallback(:booktitle)
  return if bibtex.has_parent? &&
    bibtex.parent[:title] == bibtex[:booktitle]
  return if bibtex.has_parent? &&
    bibtex.parent[:booktitle] == bibtex[:booktitle]
  return if bibtex.has_parent? &&
    bibtex.parent[:isbn] == bibtex[:isbn]
  return if bibtex[:title] == bibtex[:booktitle]

  series = RDF::Node.new
  graph << [series, RDF.type, bibo[:Document]]
  graph << [series, RDF::DC.title, bibtex[:booktitle].to_s]

  graph << [entry, RDF::DC.isPartOf, series]
end

#chapterObject



126
127
128
129
130
131
# File 'lib/bibtex/entry/rdf_converter.rb', line 126

def chapter
  return unless bibtex.field?(:chapter)
  remove_from_fallback(:chapter)

  graph << [entry, bibo[:chapter], bibtex[:chapter].to_s]
end

#childrenObject



133
134
135
136
137
138
139
140
141
# File 'lib/bibtex/entry/rdf_converter.rb', line 133

def children
  return unless bibtex.has_children?

  bibtex.children.each do |child|
    child_id = RDF::URI.new(child.identifier)
    BibTeX::Entry::RDFConverter.new(child, graph, agent).convert! unless uri_in_graph?(child_id)
    graph << [entry, RDF::DC.hasPart, child_id]
  end
end

#convert!RDF::Graph

Returns the RDF graph of this entry.

Returns:

  • (RDF::Graph)

    the RDF graph of this entry



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bibtex/entry/rdf_converter.rb', line 48

def convert!
  bibtex.parse_names
  bibtex.parse_month

  unless uri_in_graph?(entry)
    methods = self.class.instance_methods(false) - [:convert!]
    methods.each { |m| send(m) }
    run_fallback
  end

  graph
end


143
144
145
146
147
148
# File 'lib/bibtex/entry/rdf_converter.rb', line 143

def copyright
  return unless bibtex.field?(:copyright)
  remove_from_fallback(:copyright)

  graph << [entry, RDF::DC.rightsHolder, bibtex[:copyright].to_s]
end

#date_addedObject



150
151
152
153
154
155
# File 'lib/bibtex/entry/rdf_converter.rb', line 150

def date_added
  return unless bibtex.field?(:'date-added')
  remove_from_fallback(:'date-added')

  graph << [entry, RDF::DC.created, bibtex[:'date-added'].to_s]
end

#date_modifiedObject



157
158
159
160
161
162
# File 'lib/bibtex/entry/rdf_converter.rb', line 157

def date_modified
  return unless bibtex.field?(:'date-modified')
  remove_from_fallback(:'date-modified')

  graph << [entry, RDF::DC.modified, bibtex[:'date-modified'].to_s]
end

#doiObject



164
165
166
167
168
169
170
# File 'lib/bibtex/entry/rdf_converter.rb', line 164

def doi
  return unless bibtex.field?(:doi)
  remove_from_fallback(:doi)

  graph << [entry, bibo[:doi], bibtex[:doi].to_s]
  graph << [entry, RDF::DC.identifier, "doi:#{bibtex[:doi].to_s}"]
end

#editionObject



172
173
174
175
176
177
# File 'lib/bibtex/entry/rdf_converter.rb', line 172

def edition
  return unless bibtex.field?(:edition)
  remove_from_fallback(:edition)

  graph << [entry, bibo[:edition], bibtex[:edition].to_s]
end

#editorObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/bibtex/entry/rdf_converter.rb', line 179

def editor
  return unless bibtex.field?(:editor)
  remove_from_fallback(:editor)

  seq = RDF::Node.new

  graph << [seq, RDF.type, RDF[:Seq]]
  graph << [entry, bibo[:editorList], seq]

  bibtex[:editor].each do |name|
    node = agent(name) { create_agent(name, :Person) }

    graph << [entry, bibo.name, node]
    graph << [seq, RDF.li, node]
  end
end

#fallback_defaultObject



196
197
198
# File 'lib/bibtex/entry/rdf_converter.rb', line 196

def fallback_default
  remove_from_fallback(*DEFAULT_REMOVE_FROM_FALLBACK)
end

#howpublishedObject



200
201
202
203
204
205
206
207
# File 'lib/bibtex/entry/rdf_converter.rb', line 200

def howpublished
  return unless bibtex.field?(:howpublished)
  return unless bibtex[:howpublished] =~ /^#{URI.regexp}$/
  remove_from_fallback(:howpublished)

  graph << [entry, RDF::DC.URI, bibtex[:howpublished].to_s]
  graph << [entry, bibo[:uri], bibtex[:howpublished].to_s]
end

#institutionObject



209
210
211
212
213
214
215
216
# File 'lib/bibtex/entry/rdf_converter.rb', line 209

def institution
  return unless bibtex.field?(:institution)
  remove_from_fallback(:institution)

  org = agent(bibtex[:institution].to_s) { create_agent(bibtex[:institution].to_s, :Organization) }

  graph << [entry, RDF::DC.contributor, org]
end

#isbnObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/bibtex/entry/rdf_converter.rb', line 218

def isbn
  return unless bibtex.field?(:isbn)
  remove_from_fallback(:isbn)

  graph << [entry, bibo[:isbn], bibtex[:isbn].to_s]

  if bibtex.contained?
    graph << [entry, RDF::DC.isPartOf, "urn:isbn:#{bibtex[:isbn].to_s}"]
  else
    graph << [entry, RDF::DC.identifier, "urn:isbn:#{bibtex[:isbn].to_s}"]
  end
end

#issnObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/bibtex/entry/rdf_converter.rb', line 231

def issn
  return unless bibtex.field?(:issn)
  remove_from_fallback(:issn)

  graph << [entry, bibo[:issn], bibtex[:issn].to_s]
  if bibtex.contained?
    graph << [entry, RDF::DC.isPartOf, "urn:issn:#{bibtex[:issn].to_s}"]
  else
    graph << [entry, RDF::DC.identifier, "urn:issn:#{bibtex[:issn].to_s}"]
  end
end

#journal_dc_part_ofObject



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/bibtex/entry/rdf_converter.rb', line 256

def journal_dc_part_of
  return unless bibtex.field?(:journal)
  return if bibtex.has_parent? && bibtex.parent[:title] == bibtex[:journal]
  return if bibtex.has_parent? && bibtex.parent[:issn] == bibtex[:issn]

  journal = RDF::Node.new
  graph << [journal, RDF.type, bibo[:Journal]]
  graph << [journal, RDF::DC.title, bibtex[:journal].to_s]

  graph << [entry, RDF::DC.isPartOf, journal]
end

#journal_dc_sourceObject



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bibtex/entry/rdf_converter.rb', line 243

def journal_dc_source
  return unless bibtex.field?(:journal)
  remove_from_fallback(:journal)

  source = []
  source << bibtex[:journal].to_s
  source << "Vol. #{bibtex[:volume].to_s}" if bibtex.field?(:volume)
  source << "No. #{bibtex[:number].to_s}" if bibtex.field?(:number)
  pagination = bibtex[:pagination] || 'pp.'
  source << "#{pagination.to_s} #{bibtex[:pages].to_s}" if bibtex.field?(:pages)
  graph << [entry, RDF::DC.source, source.join(', ')]
end

#keyObject



268
269
270
# File 'lib/bibtex/entry/rdf_converter.rb', line 268

def key
  graph << [entry, RDF::DC.identifier, "urn:bibtex:#{bibtex.key}"]
end

#keywordsObject



272
273
274
275
276
277
278
279
# File 'lib/bibtex/entry/rdf_converter.rb', line 272

def keywords
  return unless bibtex.field?(:keywords)
  remove_from_fallback(:keywords)

  bibtex[:keywords].to_s.split(/\s*[,;]\s*/).each do |keyword|
    graph << [entry, RDF::DC.subject, keyword]
  end
end

#languageObject



281
282
283
284
285
286
287
288
# File 'lib/bibtex/entry/rdf_converter.rb', line 281

def language
  return unless bibtex.field?(:language)
  remove_from_fallback(:language)

  bibtex[:language] = 'german' if bibtex[:language] == 'ngerman'

  graph << [entry, RDF::DC.language, bibtex[:language].to_s]
end

#lccnObject



301
302
303
304
305
306
# File 'lib/bibtex/entry/rdf_converter.rb', line 301

def lccn
  return unless bibtex.field?(:lccn)
  remove_from_fallback(:lccn)

  graph << [entry, bibo[:lccn], bibtex[:lccn].to_s]
end

#locationObject



290
291
292
293
294
295
296
297
298
299
# File 'lib/bibtex/entry/rdf_converter.rb', line 290

def location
  return unless bibtex.field?(:location)
  remove_from_fallback(:location)

  graph << [entry, RDF::DC.Location, bibtex[:location].to_s]
  if [:proceedings, :inproceedings, :conference].include?(bibtex.type)
    event = RDF::Vocabulary.new('http://purl.org/NET/c4dm/event.owl')
    graph << [entry, event[:place], org]
  end
end

#noteObject



308
309
310
311
312
313
314
315
316
317
# File 'lib/bibtex/entry/rdf_converter.rb', line 308

def note
  return unless bibtex.field?(:note)
  remove_from_fallback(:note)

  pub = RDF::Node.new
  graph << [pub, RDF.type, bibo[:Note]]
  graph << [pub, bibo[:content], bibtex[:note]]

  graph << [entry, bibo[:annotates], pub]
end

#numberObject



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/bibtex/entry/rdf_converter.rb', line 319

def number
  return unless bibtex.field?(:number)
  remove_from_fallback(:number)

  case bibtex.type
  when :techreport || :manual || :unpublished
    graph << [entry, bibo[:number], bibtex[:number].to_s]
  else
    graph << [entry, bibo[:issue], bibtex[:number].to_s]
  end
end

#organizationObject



331
332
333
334
335
336
337
338
339
# File 'lib/bibtex/entry/rdf_converter.rb', line 331

def organization
  return unless bibtex.field?(:organization)
  remove_from_fallback(:organization)

  org = agent(bibtex[:organization].to_s) { create_agent(bibtex[:organization].to_s, :Organization) }

  graph << [entry, RDF::DC.contributor, org]
  graph << [entry, bibo[:organizer], org] if [:proceedings, :inproceedings, :conference].include?(bibtex.type)
end

#pagesObject



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/bibtex/entry/rdf_converter.rb', line 341

def pages
  return unless bibtex.field?(:pages)
  remove_from_fallback(:pages)

  if bibtex[:pages].to_s =~ /^\s*(\d+)\s*-+\s*(\d+)\s*$/
    graph << [entry, bibo[:pageStart], Regexp.last_match[1]]
    graph << [entry, bibo[:pageEnd], Regexp.last_match[2]]
  else
    graph << [entry, bibo[:pages], bibtex[:pages].to_s]
  end
end

#pagetotalObject



353
354
355
356
357
358
# File 'lib/bibtex/entry/rdf_converter.rb', line 353

def pagetotal
  return unless bibtex.field?(:pagetotal)
  remove_from_fallback(:pagetotal)

  graph << [entry, bibo[:numPages], bibtex[:pagetotal].to_s]
end

#parentObject



360
361
362
363
364
365
366
367
# File 'lib/bibtex/entry/rdf_converter.rb', line 360

def parent
  return unless bibtex.has_parent?
  remove_from_fallback(:crossref)

  parent_id = RDF::URI.new(bibtex.parent.identifier)
  BibTeX::Entry::RDFConverter.new(bibtex.parent, graph, agent).convert! unless uri_in_graph?(parent_id)
  graph << [entry, RDF::DC.isPartOf, parent_id]
end

#publisherObject



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/bibtex/entry/rdf_converter.rb', line 369

def publisher
  return unless bibtex.field?(:publisher, :organization, :school, :institution)
  remove_from_fallback(:publisher, :address)

  org =
    case
    when bibtex.field?(:publisher)
      agent(bibtex[:publisher].to_s) { create_agent(bibtex[:publisher].to_s, :Organization) }
    when bibtex.field?(:organization)
      agent(bibtex[:organization].to_s) { create_agent(bibtex[:organization].to_s, :Organization) }
    when bibtex.field?(:school)
      agent(bibtex[:school].to_s) { create_agent(bibtex[:school].to_s, :Organization) }
    when bibtex.field?(:institution)
      agent(bibtex[:institution].to_s) { create_agent(bibtex[:institution].to_s, :Organization) }
    end

  if bibtex.field?(:address)
    address = RDF::Vocabulary.new('http://schemas.talis.com/2005/address/schema#')
    graph << [org, address[:localityName], bibtex[:address]]
  end

  graph << [entry, RDF::DC.publisher, org]
  graph << [entry, bibo[:publisher], org]
end

#schoolObject



394
395
396
397
398
399
400
401
# File 'lib/bibtex/entry/rdf_converter.rb', line 394

def school
  return unless bibtex.field?(:school)
  remove_from_fallback(:school)

  org = agent(bibtex[:school].to_s) { create_agent(bibtex[:school].to_s, :Organization) }

  graph << [entry, RDF::DC.contributor, org]
end

#seriesObject



403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/bibtex/entry/rdf_converter.rb', line 403

def series
  return unless bibtex.field?(:series)
  remove_from_fallback(:series)
  return if bibtex.has_parent? && bibtex.parent[:title] == bibtex[:series]
  return if bibtex.has_parent? && bibtex.parent[:series] == bibtex[:series]
  return if bibtex.has_parent? && bibtex.parent[:issn] == bibtex[:issn]

  series = RDF::Node.new
  graph << [series, RDF.type, bibo[:MultiVolumeBook]]
  graph << [series, RDF::DC.title, bibtex[:series].to_s]

  graph << [entry, RDF::DC.isPartOf, series]
end

#thesis_degreeObject



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
# File 'lib/bibtex/entry/rdf_converter.rb', line 417

def thesis_degree
  return unless bibo_class == :Thesis

  degree =
    case bibtex.type
    # ms = masters degree in science
    # Only ma and ms available. We simply chose one.
    when :mastersthesis then bibo['degrees/ms']
    when :phdthesis then bibo['degrees/phd']
    end

  degree =
    case bibtex[:type]
    when 'mathesis' then bibo['degrees/ma']
    when 'phdthesis' then bibo['degrees/phd']
    when /Dissertation/i then bibo['degrees/phd']
    when /Bachelor['s]{0,2} Thesis/i then "Bachelor's Thesis"
    when /Diplomarbeit/i then bibo['degrees/ms']
    when /Magisterarbeit/i then bibo['degrees/ma']
    else degree
    end

  unless degree.nil?
    remove_from_fallback(:type)
    graph << [entry, bibo[:degree], degree]
  end
end

#titleObject



445
446
447
448
449
450
451
452
453
454
# File 'lib/bibtex/entry/rdf_converter.rb', line 445

def title
  return unless bibtex.field?(:title)
  remove_from_fallback(:title)

  title = [bibtex[:title].to_s, bibtex[:subtitle].to_s]
    .reject { |t| t.nil? || t.empty? }
    .join(': ')
  graph << [entry, RDF::DC.title, title]
  graph << [entry, bibo[:shortTitle], bibtex[:title].to_s] if bibtex.field?(:subtitle)
end

#translatorObject



456
457
458
459
460
461
462
463
464
465
466
# File 'lib/bibtex/entry/rdf_converter.rb', line 456

def translator
  return unless bibtex.field?(:translator)
  remove_from_fallback(:translator)

  node = agent(bibtex[:translator].to_s) do
    create_agent(bibtex[:translator].to_s, :Person)
  end

  graph << [entry, RDF::DC.contributor, node]
  graph << [entry, bibo[:translator], node]
end

#typeObject



468
469
470
471
472
473
474
475
476
477
# File 'lib/bibtex/entry/rdf_converter.rb', line 468

def type
  graph << [entry, RDF.type, bibo[bibo_class]]

  case bibtex.type
  when :proceedings, :journal
    graph << [entry, RDF::DC.type, 'Collection']
  else
    graph << [entry, RDF::DC.type, 'Text']
  end
end

#urlObject



479
480
481
482
483
484
485
# File 'lib/bibtex/entry/rdf_converter.rb', line 479

def url
  return unless bibtex.field?(:url)
  remove_from_fallback(:url)

  graph << [entry, RDF::DC.URI, bibtex[:url].to_s]
  graph << [entry, bibo[:uri], bibtex[:url].to_s]
end

#volumeObject



487
488
489
490
491
492
# File 'lib/bibtex/entry/rdf_converter.rb', line 487

def volume
  return unless bibtex.field?(:volume)
  remove_from_fallback(:volume)

  graph << [entry, bibo[:volume], bibtex[:volume].to_s]
end

#volumesObject



494
495
496
497
498
499
# File 'lib/bibtex/entry/rdf_converter.rb', line 494

def volumes
  return unless bibtex.field?(:volumes)
  remove_from_fallback(:volumes)

  graph << [entry, bibo[:numVolumes], bibtex[:volumes].to_s]
end

#yearObject



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/bibtex/entry/rdf_converter.rb', line 501

def year
  return unless bibtex.field?(:year)
  remove_from_fallback(:year, :month)

  year = bibtex[:year].to_s
  if bibtex.field?(:month)
    month = BibTeX::Entry::MONTHS.find_index(bibtex[:month].to_s.intern)
    month += 1 unless month.nil?
  end
  date = month.nil? ? year : [year, month].join('-')

  graph << [entry, RDF::DC.issued, date]
end