Class: AnyStyle::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/anystyle/normalizer.rb,
lib/anystyle/normalizer/date.rb,
lib/anystyle/normalizer/page.rb,
lib/anystyle/normalizer/type.rb,
lib/anystyle/normalizer/arxiv.rb,
lib/anystyle/normalizer/names.rb,
lib/anystyle/normalizer/locale.rb,
lib/anystyle/normalizer/pubmed.rb,
lib/anystyle/normalizer/quotes.rb,
lib/anystyle/normalizer/volume.rb,
lib/anystyle/normalizer/edition.rb,
lib/anystyle/normalizer/journal.rb,
lib/anystyle/normalizer/locator.rb,
lib/anystyle/normalizer/unicode.rb,
lib/anystyle/normalizer/brackets.rb,
lib/anystyle/normalizer/location.rb,
lib/anystyle/normalizer/container.rb,
lib/anystyle/normalizer/publisher.rb,
lib/anystyle/normalizer/punctuation.rb

Defined Under Namespace

Classes: ArXiv, Brackets, Container, Date, Edition, Journal, Locale, Location, Locator, Names, Page, PubMed, Publisher, Punctuation, Quotes, Type, Unicode, Volume

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys: self.class.keys) ⇒ Normalizer

Returns a new instance of Normalizer


12
13
14
15
# File 'lib/anystyle/normalizer.rb', line 12

def initialize(keys: self.class.keys)
  @keys = keys
  @skip = false
end

Class Attribute Details

.keysObject (readonly)

Returns the value of attribute keys


6
7
8
# File 'lib/anystyle/normalizer.rb', line 6

def keys
  @keys
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys


9
10
11
# File 'lib/anystyle/normalizer.rb', line 9

def keys
  @keys
end

#skipObject

Returns the value of attribute skip


10
11
12
# File 'lib/anystyle/normalizer.rb', line 10

def skip
  @skip
end

Instance Method Details

#append(item, key, value) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/anystyle/normalizer.rb', line 25

def append(item, key, value)
  if item.key?(key)
    item[key] << value
  else
    item[key] = [value]
  end
end

#detect_language(string) ⇒ Object


33
34
35
# File 'lib/anystyle/normalizer/locale.rb', line 33

def detect_language(string)
  @ld.detect(string) unless @ld.nil?
end

#detect_scripts(string) ⇒ Object


37
38
39
# File 'lib/anystyle/normalizer/locale.rb', line 37

def detect_scripts(string)
  ::Unicode::Scripts.scripts(string) if defined?(::Unicode::Scripts)
end

#doi_extract(value) ⇒ Object


26
27
28
# File 'lib/anystyle/normalizer/locator.rb', line 26

def doi_extract(value)
  value[/10(\.(\d{4,9}\/[-._;()\/:A-Z0-9]+|1002\/\S+)|\/\p{Alnum}{3,})/i]
end

#each_value(item, keys = keys_for(item)) ⇒ Object


33
34
35
36
37
38
39
40
# File 'lib/anystyle/normalizer.rb', line 33

def each_value(item, keys = keys_for(item))
  keys.each do |key|
    item[key].each do |value|
      yield key, value
    end if item.key?(key)
  end
  item
end

#keys_for(item) ⇒ Object


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

def keys_for(item)
  if self.class.keys.empty?
    item.keys
  else
    self.class.keys
  end
end

#map_values(item, keys = keys_for(item)) ⇒ Object


42
43
44
45
46
47
48
49
50
51
# File 'lib/anystyle/normalizer.rb', line 42

def map_values(item, keys = keys_for(item))
  keys.each do |key|
    if item.key?(key)
      item[key] = item[key].map { |value|
        yield key, value
      }.flatten.reject { |v| v.nil? || v.empty? }
    end
  end
  item
end

#nameObject


17
18
19
# File 'lib/anystyle/normalizer.rb', line 17

def name
  self.class.name
end

#normalize(item, **opts) ⇒ Object

Raises:

  • (NotImplementedError)

21
22
23
# File 'lib/anystyle/normalizer.rb', line 21

def normalize(item, **opts)
  raise NotImplementedError
end

#skip?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/anystyle/normalizer.rb', line 61

def skip?
  @skip
end