summaryrefslogtreecommitdiff
path: root/lib/format_util.rb
blob: 3d3a3e66b25928450d08540dec15b40024e22279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68


class String
  
  # :prediction_feature -> predictionFeature
  # :test_dataset_uri -> testDataset
  # :validation_uris -> validation
  def to_rdf_format
    s = gsub(/_uri(s|)$/,"")
    s.gsub(/_./) do |m|
      m.gsub!(/^_/,"")
      m.upcase
    end
  end
  
  def from_rdf_format
    gsub(/[A-Z]/) do |m|
      "_"+m.downcase
    end
  end
  
  DC_KEYS = [ "title", "creator", "date", "format" ]
  RDF_KEYS = [ "type" ]
  
  def to_owl_uri
    if DC_KEYS.include?(self)
      return DC.send(self)
    elsif RDF_KEYS.include?(self)
      return RDF.send(self)
    else
      return OT.send(self)
    end
  end
end

class Hash
  
  # applies to_rdf_format to all keys
  def keys_to_rdf_format
    res = {}
    keys.each do |k|
      v = self[k]
      if v.is_a?(Hash)
        v = v.keys_to_rdf_format
      elsif v.is_a?(Array)
        v = v.collect{ |vv| vv.is_a?(Hash) ? vv.keys_to_rdf_format : vv }
      end
      res[k.to_s.to_rdf_format] = v
    end
    return res
  end
  
  def keys_to_owl_uris
    res = {}
    keys.each do |k|
      v = self[k]
      if v.is_a?(Hash)
        v = v.keys_to_owl_uris
      elsif v.is_a?(Array)
        v = v.collect{ |vv| vv.is_a?(Hash) ? vv.keys_to_owl_uris : vv }
      end
      res[k.to_s.to_owl_uri] = v
    end
    return res
  end
  
end