Class: Alexandria::PseudoMarcParser

Inherits:
Object
  • Object
show all
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

Class Method Details

.get_fields(data, type, stripping, mappings = 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, mappings = USMARC_MAPPINGS)
  field = ""
  mappings[type][1..mappings[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, mappings = 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
123
# File 'lib/alexandria/book_providers/pseudomarc.rb', line 61

def self.marc_text_to_book(marc, mappings = USMARC_MAPPINGS)
  details = marc_text_to_details(marc)
  return if details.empty?

  title = nil
  title_data = details[mappings[:title][0]]
  if title_data
    title_data_all = get_fields(title_data, :title, %r{(.*)[/:]$}, mappings)
    title = title_data_all if title_data_all
  end

  authors = []
  author_data = details[mappings[:authors][0]]
  author_data&.each do |ad|
    author = ad[mappings[:authors][1]]
    if author
      author = author.strip
      author = Regexp.last_match[1] if author =~ /(.*),$/
      authors << author
    end
  end

  isbn = nil
  binding = nil
  isbn_data = details[mappings[:isbn][0]]
  if isbn_data && isbn_data.first[mappings[:isbn][1]] =~ /([-0-9xX]+)/
    isbn = Regexp.last_match[1]
  end

  binding_data = details[mappings[:binding][0]]
  if binding_data &&
      binding_data.first[mappings[:binding][1]] =~ /([a-zA-Z][a-z\s]+[a-z])/
    binding = Regexp.last_match[1]
  end

  publisher = nil
  publisher_data = details[mappings[:publisher][0]]
  publisher = publisher_data.first[mappings[:publisher][1]] if publisher_data

  year = nil
  publication_data = details[mappings[:year][0]]
  if publication_data
    year = publication_data.first[mappings[:year][1]]
    year = Regexp.last_match[1].to_i if year =~ /(\d+)/
  end

  notes = ""
  notes_data = details[mappings[:notes][0]]
  notes_data&.each do |note|
    txt = note[mappings[: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, authors, isbn,
                              publisher, year, binding)
  book.notes = notes unless notes.empty?
  book
end

.marc_text_to_details(marc) ⇒ Object



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
# File 'lib/alexandria/book_providers/pseudomarc.rb', line 125

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 = {}

      d_idx = 0
      while d_idx < data.size
        d_str = data[d_idx..-1]
        idx = d_str =~ /\$([a-z]) ([^$]+)/
        break unless idx

        sub_code = Regexp.last_match[1]
        sub_data = Regexp.last_match[2]
        this_line_data[sub_code] = sub_data
        d_idx += idx + 2 # (2 extra to push beyond this '$a' etc.)
      end

      unless this_line_data.empty?
        details[code] = [] unless details.key?(code)
        details[code] << this_line_data
      end
    end
  end
  details
end