Class: Anystyle::Parser::Normalizer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/anystyle/parser/normalizer.rb

Constant Summary collapse

MONTH =
Hash.new do |h,k|
  case k
  when /jan/i
    h[k] = 1
  when /feb/i
    h[k] = 2
  when /mar/i
    h[k] = 3
  when /apr/i
    h[k] = 4
  when /ma[yi]/i
    h[k] = 5
  when /jun/i
    h[k] = 6
  when /jul/i
    h[k] = 7
  when /aug/i
    h[k] = 8
  when /sep/i
    h[k] = 9
  when /o[ck]t/i
    h[k] = 10
  when /nov/i
    h[k] = 11
  when /dec/i
    h[k] = 12
  else
    h[k] = nil
  end
end

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object


41
42
43
44
45
46
47
48
# File 'lib/anystyle/parser/normalizer.rb', line 41

def method_missing(name, *arguments, &block)
  case name.to_s
  when /^normalize_(.+)$/
    normalize($1.to_sym, *arguments, &block)
  else
    super
  end
end

Instance Method Details

#extract_edition(token, hash) ⇒ Object


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/anystyle/parser/normalizer.rb', line 221

def extract_edition(token, hash)
  edition = [hash[:edition]].flatten.compact

  if token.gsub!(/[^[:alnum:]]*(\d+)(?:st|nd|rd|th)?\s*(?:Aufl(?:age|\.)|ed(?:ition|\.)?)[^[:alnum:]]*/i, '')
    edition << $1
  end

  if token.gsub!(/(?:\band)?[^[:alnum:]]*([Ee]xpanded)[^[:alnum:]]*$/, '')
    edition << $1
  end

  if token.gsub!(/(?:\band)?[^[:alnum:]]*([Ii]llustrated)[^[:alnum:]]*$/, '')
    edition << $1
  end

  if token.gsub!(/(?:\band)?[^[:alnum:]]*([Rr]evised)[^[:alnum:]]*$/, '')
    edition << $1
  end

  if token.gsub!(/(?:\band)?[^[:alnum:]]*([Rr]eprint)[^[:alnum:]]*$/, '')
    edition << $1
  end

  hash[:edition] = edition.join(', ') unless edition.empty?
end

#normalize(key, hash) ⇒ Object

Default normalizer. Strips punctuation.


51
52
53
54
55
56
57
58
59
# File 'lib/anystyle/parser/normalizer.rb', line 51

def normalize(key, hash)
  token, *dangling =  hash[key]
  unmatched(key, hash, dangling) unless dangling.empty?

  token.gsub!(/^[^[:alnum:]]+|[^[:alnum:]]+$/, '')

  hash[key] = token
  hash
end

#normalize_accessed(hash) ⇒ Object


61
62
63
64
65
66
67
68
69
# File 'lib/anystyle/parser/normalizer.rb', line 61

def normalize_accessed(hash)
  token, *dangling =  hash[:accessed]
  unmatched(:accessed, hash, dangling) unless dangling.empty?

  token.gsub!(/(accessed|retrieved):?\s*/i, '')

  hash[:accessed] = token
  hash
end

#normalize_author(hash) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/anystyle/parser/normalizer.rb', line 90

def normalize_author(hash)
  authors, *dangling = hash[:author]
  unmatched(:author, hash, dangling) unless dangling.empty?

  if authors =~ /\b[Ee]d((s|itors?|ited)\b|\.)/ && !hash.has_key?(:editor)
    hash[:editor] = hash.delete(:author)
    hash = normalize_editor(hash)
  else
    hash[:'more-authors'] = true if strip_et_al(authors)
    authors.gsub!(/^[^[:alnum:]]+|[^[:alnum:]]+$/, '')
    hash[:author] = normalize_names(authors)
  end

  hash
end

#normalize_booktitle(hash) ⇒ Object


247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/anystyle/parser/normalizer.rb', line 247

def normalize_booktitle(hash)
  booktitle, *dangling = hash[:booktitle]
  unmatched(:booktitle, hash, dangling) unless dangling.empty?

  booktitle.gsub!(/^in:\s+/i, '')
  booktitle.gsub!(/^In\s+/i, '')

  extract_edition(booktitle, hash)

  booktitle.gsub!(/^\s+|[\.,:;\s]+$/, '')
  hash[:booktitle] = booktitle

  hash
end

#normalize_citation_number(hash) ⇒ Object


82
83
84
85
86
87
88
# File 'lib/anystyle/parser/normalizer.rb', line 82

def normalize_citation_number(hash)
  token, *dangling =  hash[:citation_number]
  unmatched(:citation_number, hash, dangling) unless dangling.empty?

  hash[:citation_number] = token[/\d[\w,.-]+/] || token
  hash
end

#normalize_date(hash) ⇒ Object


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/anystyle/parser/normalizer.rb', line 287

def normalize_date(hash)
  date = Array(hash[:date]).join(' ')

  unless (month = MONTH[date]).nil?
    month = '%02d' % month
  end

  if date =~ /(\d{4})/
    year = $1

    if month && date =~ /\b(\d{1,2})\b/
      day = '%02d' % $1.to_i
    end

    hash[:date] = [year, month, day].compact.join('-')
  end

  hash
end

#normalize_director(hash) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
# File 'lib/anystyle/parser/normalizer.rb', line 157

def normalize_director(hash)
  directors = hash[:director]

  directors.gsub!(/^\W+|\W+$/, '')
  directors.gsub!(/[^[:alpha:]]*direct(or|ed)?\b:w
                  [^[:alpha:]]*/i, '')
  directors.gsub!(/\bby\b/i, '')

  hash[:director] = normalize_names(directors)
  hash
end

#normalize_editor(hash) ⇒ Object


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
# File 'lib/anystyle/parser/normalizer.rb', line 106

def normalize_editor(hash)
  editors, *dangling = hash[:editor]

  unless dangling.empty?
    case
    when !hash.has_key?(:author)
      hash[:author] = editors
      hash[:editor] = dangling
      hash = normalize_author(hash)
      return normalize_editor(hash)
    when dangling[0] =~ /(\d+)/
      hash[:edition] = $1.to_i
    else
      unmatched(:editor, hash, dangling)
    end
  end

  hash[:'more-editors'] = true if strip_et_al(editors)

  editors.gsub!(/^\W+|\W+$/, '')
  editors.gsub!(/^in:?\s+/i, '')
  editors.gsub!(/\W*\b[Ee]d(s|itors?|ited)?\b\W*/, '')
  editors.gsub!(/\W*\b([Hh]rsg|gg?|Herausgeber)\b\W*/, '')
  editors.gsub!(/\b[Hh]erausgegeben von\b/, '')
  editors.gsub!(/\bby\b/i, '')

  is_trans = !!editors.gsub!(/[^[:alpha:]]*trans(lated)?[^[:alpha:]]*/i, '')

  hash[:editor] = normalize_names(editors)
  hash[:translator] = hash.delete :editor if is_trans

  hash
end

#normalize_isbn(hash) ⇒ Object


390
391
392
393
394
395
396
397
398
# File 'lib/anystyle/parser/normalizer.rb', line 390

def normalize_isbn(hash)
  isbn, *dangling = hash[:isbn]
  unmatched(:isbn, hash, dangling) unless dangling.empty?

  isbn = isbn[/[\d-]+/]
  hash[:isbn] = isbn

  hash
end

#normalize_journal(hash) ⇒ Object


262
263
264
265
266
267
268
269
270
# File 'lib/anystyle/parser/normalizer.rb', line 262

def normalize_journal(hash)
  journal, *dangling = hash[:journal]
  unmatched(:journal, hash, dangling) unless dangling.empty?

  journal.gsub!(/^[\s]+|[\.,:;\s]+$/, '')
  hash[:journal] = journal

  hash
end

#normalize_key(hash) ⇒ Object


71
72
73
74
75
76
77
78
79
80
# File 'lib/anystyle/parser/normalizer.rb', line 71

def normalize_key(hash)
  token, *dangling =  hash[:key]
  unmatched(:key, hash, dangling) unless dangling.empty?

  token.gsub!(/^[^[:alnum:]]+|[^[:alnum:]]+$/, '')
  token.gsub!(/^bibitem\{/i, '')

  hash[:key] = token
  hash
end

#normalize_location(hash) ⇒ Object


375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/anystyle/parser/normalizer.rb', line 375

def normalize_location(hash)
  location, *dangling = hash[:location]
  unmatched(:pages, hash, dangling) unless dangling.empty?

  location.gsub!(/^[^[:alnum:]]+|[^[:alnum:]]+$/, '')

  if !hash.has_key?(:publisher) && location =~ /:/
    location, publisher = location.split(/\s*:\s*/)
    hash[:publisher] = publisher
  end

  hash[:location] = location
  hash
end

#normalize_medium(hash) ⇒ Object


408
409
410
411
412
413
414
# File 'lib/anystyle/parser/normalizer.rb', line 408

def normalize_medium(hash)
  medium, *dangling = hash[:medium]
  unmatched(:medium, hash, dangling) unless dangling.empty?

  hash[:medium] = medium.split(/\W+/).reject(&:empty?).join('-')
  hash
end

#normalize_names(names) ⇒ Object


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/anystyle/parser/normalizer.rb', line 180

def normalize_names(names)
  names.gsub!(/\s*(\.\.\.|…)\s*/, '')
  names.gsub!(/;|:/, ',')

  # Add surname/initial punctuation separator for Vancouver-style names
  # E.g. Rang HP, Dale MM, Ritter JM, Moore PK
  if names.match(/^(\p{Lu}[^\s,.]+)\s+([\p{Lu}][\p{Lu}\-]{0,3})(,|[.]?$)/)
    names.gsub!(/\b(\p{Lu}[^\s,.]+)\s+([\p{Lu}][\p{Lu}\-]{0,3})(,|[.]?$)/, '\1, \2\3')
  end

  Namae.parse!(names).map { |name|
    name.normalize_initials
    name.sort_order

  }.join(' and ')

rescue => e
  warn e.message
  names
end

#normalize_pages(hash) ⇒ Object


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/anystyle/parser/normalizer.rb', line 352

def normalize_pages(hash)
  pages, *dangling = hash[:pages]
  unmatched(:pages, hash, dangling) unless dangling.empty?

  # "volume.issue(year):pp"
  case pages
  when /(\d+) (?: \.(\d+))? (?: \( (\d{4}) \))? : (\d.*)/x
    hash[:volume] = $1.to_i
    hash[:number] = $2.to_i unless $2.nil?
    hash[:year] = $3.to_i unless $3.nil?
    hash[:pages] = $4
  end

  case hash[:pages]
  when /(\d+)\D+(\d+)/
    hash[:pages] = [$1,$2].join('') # en-dash
  when  /(\d+)/
    hash[:pages] = $1
  end

  hash
end

#normalize_producer(hash) ⇒ Object


169
170
171
172
173
174
175
176
177
178
# File 'lib/anystyle/parser/normalizer.rb', line 169

def normalize_producer(hash)
  producers = hash[:producer]

  producers.gsub!(/^\W+|\W+$/, '')
  producers.gsub!(/[^[:alpha:]]*produc(er|ed)?[^[:alpha:]]*/i, '')
  producers.gsub!(/\bby\b/i, '')

  hash[:director] = normalize_names(producers)
  hash
end

#normalize_publisher(hash) ⇒ Object


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/anystyle/parser/normalizer.rb', line 334

def normalize_publisher(hash)
  normalize :publisher, hash

  case hash[:publisher]
  when /^producers?$/i
    hash[:publisher] = hash[:producer]

  when /^authors?$/i
    hash[:publisher] = hash[:author]

  when /^editor?$/i
    hash[:publisher] = hash[:editor]

  end

  hash
end

#normalize_source(hash) ⇒ Object


272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/anystyle/parser/normalizer.rb', line 272

def normalize_source(hash)
  source, *dangling = hash[:source]
  unmatched(:source, hash, dangling) unless dangling.empty?

  case source
  when /dissertation abstracts/i
    source.gsub!(/\s*section \w: ([[:alnum:]\s]+).*$/i, '')
    hash[:category] = $1 unless $1.nil?
    hash[:type] = :thesis
  end

  hash[:source] = source
  hash
end

#normalize_title(hash) ⇒ Object


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/anystyle/parser/normalizer.rb', line 203

def normalize_title(hash)
  title, source = hash[:title]

  unless source.nil?
    hash[:source] = source
    normalize(:source, hash)
  end

  extract_edition(title, hash)

  title.gsub!(/^\s+|[\.,:;\s]+$/, '')
  title.gsub!(/^["'”’´‘“`](.+)["'”’´‘“`]$/, '\1')

  hash[:title] = title

  hash
end

#normalize_translator(hash) ⇒ Object


144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/anystyle/parser/normalizer.rb', line 144

def normalize_translator(hash)
  translators = hash[:translator]

  translators.gsub!(/\b([Ii]n (d|ein)er )?[Üü]ber(s\.|setzt|setzung|tragen|tragung) v(\.|on\b)/, '')
  translators.gsub!(/^\W+|\W+$/, '')
  translators.gsub!(/[^[:alpha:]]*\btrans(l(ated)?)?\b[^[:alpha:]]*/i, '')
  translators.gsub!(/\bby\b/i, '')
  translators.gsub!(/\btrad\./i, '')

  hash[:translator] = normalize_names(translators)
  hash
end

#normalize_url(hash) ⇒ Object


400
401
402
403
404
405
406
# File 'lib/anystyle/parser/normalizer.rb', line 400

def normalize_url(hash)
  url, *dangling = hash[:url]
  unmatched(:url, hash, dangling) unless dangling.empty?

  hash[:url] = url[/([a-z]+:\/\/)?\w+\.\w+[\w\.\/%-]+/i] || url
  hash
end

#normalize_volume(hash) ⇒ Object


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/anystyle/parser/normalizer.rb', line 307

def normalize_volume(hash)
  volume, *dangling = hash[:volume]
  unmatched(:volume, hash, dangling) unless dangling.empty?

  if !hash.has_key?(:pages) && volume =~ /\D*(\d+):(\d+(?:[—–-]+)\d+)/
    hash[:volume], hash[:pages] = $1.to_i, $2
    hash = normalize_pages(hash)
  else
    case volume
    when /\D*(\d+)\D+(\d+[\s\/&—–-]+\d+)/
      hash[:volume], hash[:number] = $1.to_i, $2
    when /(\d+)?\D+no\.\s*(\d+\D+\d+)/
      hash[:volume] = $1.to_i unless $1.nil?
      hash[:number] = $2
    when /(\d+)?\D+no\.\s*(\d+)/
      hash[:volume] = $1.to_i unless $1.nil?
      hash[:number] = $2.to_i
    when /\D*(\d+)\D+(\d+)/
      hash[:volume], hash[:number] = $1.to_i, $2.to_i
    when /(\d+)/
      hash[:volume] = $1.to_i
    end
  end

  hash
end

#strip_et_al(names) ⇒ Object


140
141
142
# File 'lib/anystyle/parser/normalizer.rb', line 140

def strip_et_al(names)
  !!names.sub!(/(\bet\s+(al|coll)\b|\bu\.\s*a\.|(\band|\&)\s+others).*$/, '')
end