Class: Alexandria::PseudoMarcParser
- Inherits:
-
Object
- Object
- Alexandria::PseudoMarcParser
- Defined in:
- lib/alexandria/book_providers/pseudomarc.rb
Overview
A really simple regex-based parser to grab data out of marc text records.
Constant Summary collapse
- BNF_FR_MAPPINGS =
{ title: ['200', 'a'], authors: ['700', 'a'], isbn: ['010', 'a'], publisher: ['210', 'g'], year: ['210', 'd'], binding: ['225', 'a'], notes: ['520', 'a'] }.freeze
- USMARC_MAPPINGS =
{ title: ['245', 'a', 'b'], authors: ['100', 'a'], isbn: ['020', 'a'], publisher: ['490', 'a'], year: ['260', 'c'], binding: ['020', 'a'], # listed with isbn here notes: ['520', 'a'] }.freeze
Class Method Summary collapse
- .get_fields(data, type, stripping, m = USMARC_MAPPINGS) ⇒ Object
- .marc_text_to_book(marc, m = USMARC_MAPPINGS) ⇒ Object
- .marc_text_to_details(marc) ⇒ Object
Class Method Details
.get_fields(data, type, stripping, m = USMARC_MAPPINGS) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/alexandria/book_providers/pseudomarc.rb', line 44 def self.get_fields(data, type, stripping, m = USMARC_MAPPINGS) field = '' m[type][1..m[type].length - 1].each do |part| if data.first[part] part_data = data.first[part].strip if part_data =~ stripping part_data = Regexp.last_match[1] part_data = part_data.strip end field += ': ' if field != '' field += part_data end end field = nil if field == '' field end |
.marc_text_to_book(marc, m = USMARC_MAPPINGS) ⇒ Object
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 |
# File 'lib/alexandria/book_providers/pseudomarc.rb', line 61 def self.marc_text_to_book(marc, m = USMARC_MAPPINGS) details = marc_text_to_details(marc) unless details.empty? title = nil title_data = details[m[:title][0]] if title_data title_data_all = get_fields(title_data, :title, /(.*)[\/:]$/, m) title = title_data_all if title_data_all end = [] = details[m[:authors][0]] &.each do |ad| = ad[m[:authors][1]] if = .strip = Regexp.last_match[1] if =~ /(.*),$/ << end end isbn = nil binding = nil isbn_data = details[m[:isbn][0]] if isbn_data isbn = Regexp.last_match[1] if isbn_data.first[m[:isbn][1]] =~ /([-0-9xX]+)/ end binding_data = details[m[:binding][0]] if binding_data binding = Regexp.last_match[1] if binding_data.first[m[:binding][1]] =~ /([a-zA-Z][a-z\s]+[a-z])/ end publisher = nil publisher_data = details[m[:publisher][0]] publisher = publisher_data.first[m[:publisher][1]] if publisher_data year = nil publication_data = details[m[:year][0]] if publication_data year = publication_data.first[m[:year][1]] year = Regexp.last_match[1].to_i if year =~ /(\d+)/ end notes = '' notes_data = details[m[:notes][0]] notes_data&.each do |note| txt = note[m[:notes][1]] notes += txt if txt end if title.nil? && isbn.nil? # probably didn't undertand the MARC dialect return nil end book = Alexandria::Book.new(title, , isbn, publisher, year, binding) book.notes = notes unless notes.empty? book end end |
.marc_text_to_details(marc) ⇒ Object
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 |
# File 'lib/alexandria/book_providers/pseudomarc.rb', line 124 def self.marc_text_to_details(marc) details = {} marc.each_line do |line| if line =~ /(\d+)\s*(.+)/ code = Regexp.last_match[1] data = Regexp.last_match[2] this_line_data = {} # puts code # puts data d_idx = 0 while d_idx < data.size d_str = data[d_idx..-1] # puts d_str if (idx = d_str =~ /\$([a-z]) ([^\$]+)/) # puts idx sub_code = Regexp.last_match[1] sub_data = Regexp.last_match[2] this_line_data[sub_code] = sub_data # puts " " + $1 # puts " " + $2 # puts idx d_idx += idx + 2 # (2 extra to push beyond this '$a' etc.) else break end end unless this_line_data.empty? details[code] = [] unless details.key?(code) details[code] << this_line_data end end end details end |