Class: SemanticallyTaggable::SkosImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/semantically_taggable/skos_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(skos_filename, scheme) ⇒ SkosImporter

Returns a new instance of SkosImporter.



5
6
7
8
9
# 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

Instance Method Details

#import(&block) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



41
42
43
44
45
46
47
48
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# 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_synonymsObject



21
22
23
24
25
26
27
# 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