class SemanticallyTaggable::SkosImporter

Public Class Methods

new(skos_filename, scheme) click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 5
def initialize(skos_filename, scheme)
  @doc = Nokogiri::XML(File.read(skos_filename))
  @scheme = scheme
  @concept_urls = {}
end

Public Instance Methods

import(&block) click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 11
def import &block
  raise ArgumentError, "Can't import SKOS for non-hierarchical schemes" unless @scheme.polyhierarchical?
  root_nodes = @doc.xpath('//skos:Concept[not(skos:broader)]')
  raise ArgumentError, "Expected only one root, got #{root_nodes.length}" unless root_nodes.length == 1

  import_concepts &block
  import_relations :narrower, :broader, :related
  import_synonyms
end
import_concepts(&block) click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 41
def import_concepts(&block)
  puts "Importing concepts..."
  iterate_concepts do |concept, label|
    @scheme.create_tag(:name => label) do |tag|
      block.call tag, concept if block
    end
  end
end
import_relations(*relations) click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 29
def import_relations(*relations)
  puts "Importing relations #{relations.join ', '}"
  relations.each do |relation|
    iterate_concepts do |concept, label|
      tag = @scheme.tags.find_by_name! label
      skos_element = "skos:#{relation}"
      others = concept.xpath(skos_element).collect { |other_node| lookup_tag(other_node) }
      tag.send("#{relation}_tags=".to_sym, others)
    end
  end
end
import_synonyms() click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 21
def import_synonyms
  puts "Importing synonyms..."
  iterate_concepts do |concept, label|
    tag = @scheme.tags.find_by_name! label
    tag.create_synonyms(concept.xpath('skos:altLabel').collect(&:content))
  end
end

Private Instance Methods

iterate_concepts() { |concept, pref_label| ... } click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 51
def iterate_concepts
  concepts = @doc.xpath('//skos:Concept')
  concepts.each do |concept|
    pref_label = concept.at_xpath('skos:prefLabel').content
    yield concept, pref_label
  end
end
lookup_tag(pointer_node) click to toggle source
# File lib/semantically_taggable/skos_importer.rb, line 59
def lookup_tag(pointer_node)
  url_xpath = "//skos:Concept[@rdf:about='#{pointer_node['resource']}']"
  concept_node = pointer_node.at_xpath(url_xpath) || (raise RuntimeError, "Concept at #{url_xpath} not found")
  pref_label = concept_node.at_xpath('skos:prefLabel').content
  @scheme.tags.find_by_name pref_label
end