Skip to content
Newer
Older
100644 144 lines (134 sloc) 3.81 KB
4df4323 @rsinger New pass at a new modeler
authored
1 require 'rubygems'
2 require 'enhanced_marc'
3 require 'rdf'
4 require 'rdf/ntriples'
5 require 'sru'
6 require 'sequel'
7 require 'yaml'
493b3a6 @rsinger Began modeling identifiers
authored
8 require 'isbn/tools'
4df4323 @rsinger New pass at a new modeler
authored
9
10 CONFIG = YAML.load_file('config/config.yml')
493b3a6 @rsinger Began modeling identifiers
authored
11 DB = Sequel.connect(CONFIG['database'])
4df4323 @rsinger New pass at a new modeler
authored
12 # Initialize the vocabularies we will be drawing from
13 module RDF
14 class BIBO < RDF::Vocabulary("http://purl.org/ontology/bibo/");end
15 class RDA < RDF::Vocabulary("http://RDVocab.info/Elements/");end
16 class RDAG2 < RDF::Vocabulary("http://RDVocab.info/ElementsGr2/");end
17 class DCAM < RDF::Vocabulary("http://purl.org/dc/dcam/");end
18 class FRBR < RDF::Vocabulary("http://purl.org/vocab/frbr/core#");end
19 class BIO < RDF::Vocabulary("http://purl.org/vocab/bio/0.1/");end
20 class OV < RDF::Vocabulary("http://open.vocab.org/terms/");end
21 end
22
23 class String
24 def slug
25 slug = self.gsub(/[^A-z0-9\s\-]/,"")
26 slug.gsub!(/\s/,"_")
27 slug.downcase.strip_leading_and_trailing_punct
28 end
29 def strip_trailing_punct
30 self.sub(/[\.:,;\/\s]\s*$/,'').strip
31 end
32 def strip_leading_and_trailing_punct
33 str = self.sub(/[\.:,;\/\s\)\]]\s*$/,'').strip
34 return str.strip.sub(/^\s*[\.:,;\/\s\(\[]/,'')
35 end
36 def lpad(count=1)
37 "#{" " * count}#{self}"
38 end
39
40 end
41 class IdentifierFieldNotFoundError < Exception;end
42 module BookModeler
493b3a6 @rsinger Began modeling identifiers
authored
43 def self.extended(o)
44 o.set_type case
45 when o.record.is_manuscript? then RDF::BIBO.Manuscript
46 when o.record.is_conference? then RDF::BIBO.Proceedings
4df4323 @rsinger New pass at a new modeler
authored
47 else
493b3a6 @rsinger Began modeling identifiers
authored
48 RDF::BIBO.Book
4df4323 @rsinger New pass at a new modeler
authored
49 end
50 end
51 end
52
53 module SerialModeler
493b3a6 @rsinger Began modeling identifiers
authored
54 def self.extended(o)
4df4323 @rsinger New pass at a new modeler
authored
55 t = nil
493b3a6 @rsinger Began modeling identifiers
authored
56 if n = o.record.nature_of_work
4df4323 @rsinger New pass at a new modeler
authored
57 t = case
58 when 'd' then RDF::BIBO.ReferenceSource
59 when 'e' then RDF::BIBO.ReferenceSource
60 when 'g' then RDF::BIBO.LegalDocument
61 when 'j' then RDF::BIBO.Patent
62 when 'l' then RDF::BIBO.Legislation
63 when 'm' then RDF::BIBO.Thesis
64 when 't' then RDF::BIBO.Report
65 when 'u' then RDF::BIBO.Standard
66 when 'v' then RDF::BIBO.LegalCaseDocument
67 when 'w' then RDF::BIBO.Report
68 when 'x' then RDF::BIBO.Report
69 when 'z' then RDF::BIBO.Treaty
70 end
71 end
493b3a6 @rsinger Began modeling identifiers
authored
72 unless t
73 if st = o.record.serial_type
74 t = case
75 when 'a' then RDF::BIBO.Collection
76 when 'm' then RDF::BIBO.Series
77 when 'n' then RDF::BIBO.Newspaper
78 when 'p' then RDF::BIBO.Periodical
79 when 'w' then RDF::BIBO.Website
80 end
81 end
82 end
83 t = RDF::BIBO.Periodical unless t
84 o.set_type(t)
4df4323 @rsinger New pass at a new modeler
authored
85 end
86 end
87 class RDFModeler
88 attr_reader :record, :statements, :uri
89 def initialize(record)
90 @record = record
91 construct_uri
92 @statements = []
93 end
94
95 def parse
96 case @record
493b3a6 @rsinger Began modeling identifiers
authored
97 when MARC::BookRecord then extend(BookModeler)
98 when MARC::SerialRecord then extend(SerialModeler)
4df4323 @rsinger New pass at a new modeler
authored
99 end
493b3a6 @rsinger Began modeling identifiers
authored
100 gather_identifiers
4df4323 @rsinger New pass at a new modeler
authored
101 end
102
103 def set_type(t)
104 @statements << RDF::Statement.new(@uri, RDF.type, t)
105 end
106
107 def construct_uri
108 @uri = RDF::URI.intern(CONFIG['uri']['base'] + CONFIG['uri']['resource_path'])
109 id = @record[CONFIG['uri']['resource_identifier_field']]
110 raise IdentifierFieldNotFoundError unless id
111 @uri += id.value.strip.slug
112 end
493b3a6 @rsinger Began modeling identifiers
authored
113
114 def gather_identifiers
115 fld_list = ['010', '020','022']
116 @record.each_by_tag(fld_list) do |field|
117 case field.tag
118 when '010' then model_lccn(field)
119 when '020' then model_isbn(field)
120 when '022' then model_isbn(field)
121 end
122 end
123 end
124
125 def model_lccn(f)
126 return unless lccn = f['a']
127 lccn.strip!
128 prefix = nil
129 year = nil
130 serial = nil
131 if prefix_m = lccn.match(/^([a-z]{1,3})(\s|\d)/)
132 prefix = prefix_m[1]
133 year_first_digit = lccn.match(/^[a-z]{0,3}\s*(\d)/)
134 if year_first_digit
135 year_m = case year_first_digit[1]
136 when "2" then lccn.match(/^[a-z]{0,3}\s*(\d{4})/)
137 else
138 lccn.match(/^[a-z]{0,3}\s*(\d{2})/)
139 end
140 year = year_m[1] if year_m
141 end
142 end
4df4323 @rsinger New pass at a new modeler
authored
143 end
144
Something went wrong with that request. Please try again.