Class: Mabmapper::MabXml::Document

Inherits:
Object
  • Object
show all
Includes:
QueryHelper
Defined in:
lib/mabmapper/mab_xml/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryHelper

#field, #get, #subfield

Constructor Details

#initialize(contents) ⇒ Document

Returns a new instance of Document.



12
13
14
15
16
# File 'lib/mabmapper/mab_xml/document.rb', line 12

def initialize(contents)
  @source = Metacrunch::Mab2::Document.from_aleph_mab_xml(contents)
  #@xml = Nokogiri::XML(contents)
  #@xml.remove_namespaces!
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



10
11
12
# File 'lib/mabmapper/mab_xml/document.rb', line 10

def source
  @source
end

#xmlObject

Returns the value of attribute xml.



10
11
12
# File 'lib/mabmapper/mab_xml/document.rb', line 10

def xml
  @xml
end

Instance Method Details

#controlfield(name) ⇒ Object

Returns the contents of a MAB control field as an array.



21
22
23
24
25
# File 'lib/mabmapper/mab_xml/document.rb', line 21

def controlfield(name)
  xpath  = "//controlfield[@tag='#{name}']"
  result = @xml.at_xpath(xpath).try(:text)
  result.present? ? result.chars.to_a.map{|e| (e=='|') ? nil : e} : []
end

#naco_normalization(value) ⇒ Object

Normalize text based on the NACO rules



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mabmapper/mab_xml/document.rb', line 28

def naco_normalization(value)
  return unless value.present?

  # Downcase everything
  value.downcase!

  # Convert unicode [and accented ASCII] characters to their
  # plain-text ASCII equivalents
  Stringex::Localization.backend = :internal
  Stringex::Localization.locale = :de
  Stringex::Localization.store_translations(:de, :transliterations, {"ü" => "ue", "ä" => "ae", "ö" => "oe", "ß" => "ss"}) if Stringex::Localization.backend.translations.blank?
  value = value.to_ascii

  # Convert special chars to spaces
  value.gsub!(/[,$~^%*\/?@.:;<>{}!\(\)\-]/, ' ')

  # Convert special chars to spaces
  value.gsub!(/[\[\]\|]/, '')

  # Remove leading and trailing spaces
  value.strip!

  # Condense multiple spaces
  value.gsub!(/\s+/, ' ')

  value
end