class AnyStyle::Normalizer::Names

Attributes

namae[RW]

Public Class Methods

new(**opts) click to toggle source
Calls superclass method AnyStyle::Normalizer::new
   # File lib/anystyle/normalizer/names.rb
12 def initialize(**opts)
13   super(**opts)
14 
15   @namae = Namae::Parser.new({
16     prefer_comma_as_separator: true,
17     separator: /\A(and|AND|&|;|und|UND|y|e)\s+/,
18     appellation: /\A(?!x)x/,
19     title: /\A(?!x)x/
20   })
21 end

Public Instance Methods

normalize(item, prev: [], **opts) click to toggle source
   # File lib/anystyle/normalizer/names.rb
23 def normalize(item, prev: [], **opts)
24   map_values(item) do |key, value|
25     value.gsub!(/(^[\(\[]|[,;:\)\]]+$)/, '')
26     case
27     when repeater?(value) && prev.length > 0
28       prev[-1].dig(key, 0) || prev[-1].dig(:author, 0) || prev[-1].dig(:editor, 0)
29     else
30       begin
31         parse(strip(value))
32       rescue
33         [{ literal: value.strip }]
34       end
35     end
36   end
37 end
parse(value) click to toggle source
   # File lib/anystyle/normalizer/names.rb
65 def parse(value)
66   raise ArgumentError if value.empty?
67 
68   others = value.sub!(
69     /(,\s+)?((\&\s+)?\bet\s+(al|coll)\b|\bu\.\s*a\b|(\band|\&)\s+others).*$/, ''
70   ) || value.sub!(/\.\.\.|…/, '')
71 
72   # Add surname/initial punctuation separator for Vancouver-style names
73   # E.g. Rang HP, Dale MM, Ritter JM, Moore PK
74   if value.match(/^(\p{Lu}[^\s,.]+)\s+([\p{Lu}][\p{Lu}\-]{0,3})(,|[.]?$)/)
75     value.gsub!(/\b(\p{Lu}[^\s,.]+)\s+([\p{Lu}][\p{Lu}\-]{0,3})(,|[.]?$)/, '\1, \2\3')
76   end
77 
78   names = namae.parse!(value).map { |name|
79     name.normalize_initials
80     name.to_h.reject { |_, v| v.nil? }
81   }
82 
83   names << { others: true } unless others.nil?
84   names
85 end
repeater?(value) click to toggle source
   # File lib/anystyle/normalizer/names.rb
39 def repeater?(value)
40   value =~ /^([\p{Pd}_*][\p{Pd}_* ]+|\p{Co})(,|:|\.|$)/
41 end
strip(value) click to toggle source
   # File lib/anystyle/normalizer/names.rb
43 def strip(value)
44   value
45     .gsub(/^[Ii]n:?\s+/, '')
46     .gsub(/\b[EÉeé]d(s?\.|itors?\.?|ited|iteurs?|ité)(\s+(by|par)\s+|\b|$)/, '')
47     .gsub(/\b([Hh](rsg|gg?)\.|Herausgeber)\s+/, '')
48     .gsub(/\b[Hh]erausgegeben von\s+/, '')
49     .gsub(/\b((d|ein)er )?[Üü]ber(s\.|setzt|setzung|tragen|tragung) v(\.|on)\s+/, '')
50     .gsub(/\b[Tt]rans(l?\.|lated|lation)(\s+by\b)?\s*/, '')
51     .gsub(/\b[Tt]rad(ucteurs?|(uit|\.)(\s+par\b)?)\s*/, '')
52     .gsub(/\b([Dd]ir(\.|ected))(\s+by)?\s+/, '')
53     .gsub(/\b([Pp]rod(\.|uce[rd]))(\s+by)?\s+/, '')
54     .gsub(/\b([Pp]erf(\.|orme[rd]))(\s+by)?\s+/, '')
55     .gsub(/\*/, '')
56     .gsub(/\([^\)]*\)?/, '')
57     .gsub(/\[[^\]]*\)?/, '')
58     .gsub(/[;:]/, ',')
59     .gsub(/^\p{^L}+|\s+\p{^L}+$/, '')
60     .gsub(/[\s,\.]+$/, '')
61     .gsub(/,{2,}/, ',')
62     .gsub(/\s+\./, '.')
63 end