Module: PubliSci::RDFParser
- Included in:
- DataSet::ORM::DataCube, DataSet::ORM::DataCube, Dataset::DataCube, Metadata::Generator, PubliSci::Readers::Base, Writers::Base
- Defined in:
- lib/publisci/parser.rb
Instance Method Summary collapse
- #add_node(n, str = "") ⇒ Object
- #bnode_value(obj, node_index, node_str, options) ⇒ Object
- #encode_value(obj, options = {}, node_index = nil, node_str = "") ⇒ Object
- #get_ary(query_results, method = 'to_s') ⇒ Object
- #get_hashes(query_results, method = nil) ⇒ Object
- #is_complex?(obj) ⇒ Boolean
- #is_uri?(obj) ⇒ Boolean
- #load_string(string, repo = RDF::Repository.new) ⇒ Object
- #observation_hash(query_results, shorten_uris = false, method = 'to_s') ⇒ Object
- #sanitize(array) ⇒ Object
- #sanitize_hash(h) ⇒ Object
- #strip_prefixes(string) ⇒ Object
- #strip_uri(uri) ⇒ Object
- #to_literal(obj, options = {}) ⇒ Object
- #to_resource(obj, options = {}) ⇒ Object
- #turtle_indent(turtle_str) ⇒ Object
Instance Method Details
#add_node(n, str = "") ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/publisci/parser.rb', line 147 def add_node(n,str="") raise "need index or identifier to generate blank nodes" unless n raise "need base string or blank string for blank node" unless str.is_a? String if str["node"] ret = str[0..-2] + "/#{n}" + ">" ret # str[0..-2] + "/#{n}" + ">" else "<node/#{n}>" end end |
#bnode_value(obj, node_index, node_str, options) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/publisci/parser.rb', line 173 def bnode_value(obj, node_index, node_str, ) # TODO - Implement proper recursion # TODO - check if object is "a" (rdf:type) => or convert rdf:type to "a" str = "" subnodes = [] if obj.is_a?(Array) # && obj.size == 2 if obj.size == 2 if obj[0].is_a?(String) if is_complex?(obj[1]) str << "#{to_resource(obj[0])} #{add_node(node_index,node_str)} . \n" subnodes << encode_value(obj[1], , node_index, node_str) else str << "#{to_resource(obj[0])} #{encode_value(obj[1], , node_index, node_str)} " end elsif obj[0].is_a?(Array) && obj[1].is_a?(Array) newnode = add_node(0,node_str) v1 = bnode_value(obj[0], 0, node_str, ) v2 = bnode_value(obj[1], 1, node_str, ) if v1.is_a? Array subnodes << v1 v1 = nil end if v2.is_a? Array subnodes << v2 v2 = nil end if v1 str << "#{v1} ;" end str << "\n#{v2} .\n" if v2 end elsif obj.all?{|ent| ent.is_a? Array} obj.each{|ent| bn = bnode_value(ent,node_index,node_str,) if bn.is_a? String str << bn + "\n" else str << bn[0] + "\n" subnodes << bn[1] end } end else raise "Invalid Structured value: #{obj}" end if subnodes.size > 0 [str, subnodes.flatten].flatten else str end end |
#encode_value(obj, options = {}, node_index = nil, node_str = "") ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/publisci/parser.rb', line 160 def encode_value(obj,={}, node_index=nil, node_str = "") if RDF::Resource(obj).valid? to_resource(obj,) elsif obj && obj.is_a?(String) && (obj[0]=="<" && obj[-1] = ">") obj elsif obj.is_a?(Array) node_str = add_node(node_index,node_str) ["#{node_str}" ] + [bnode_value(obj, node_index, node_str, )] else to_literal(obj,) end end |
#get_ary(query_results, method = 'to_s') ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/publisci/parser.rb', line 50 def get_ary(query_results,method='to_s') query_results.map{|solution| solution.to_a.map{|entry| if entry.last.respond_to? method entry.last.send(method) else entry.last.to_s end } } end |
#get_hashes(query_results, method = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/publisci/parser.rb', line 62 def get_hashes(query_results,method=nil) arr=[] query_results.map{|solution| h={} solution.map{|element| if method && element[1].respond_to?(method) h[element[0]] = element[1].send(method) else h[element[0]] = element[1] end } arr << h } arr end |
#is_complex?(obj) ⇒ Boolean
143 144 145 |
# File 'lib/publisci/parser.rb', line 143 def is_complex?(obj) obj.is_a? Array end |
#is_uri?(obj) ⇒ Boolean
4 5 6 |
# File 'lib/publisci/parser.rb', line 4 def is_uri?(obj) RDF::Resource(obj).valid? end |
#load_string(string, repo = RDF::Repository.new) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/publisci/parser.rb', line 41 def load_string(string,repo=RDF::Repository.new) f = Tempfile.new('repo') f.write(string) f.close repo.load(f.path, :format => :ttl) f.unlink repo end |
#observation_hash(query_results, shorten_uris = false, method = 'to_s') ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/publisci/parser.rb', line 78 def observation_hash(query_results,shorten_uris=false,method='to_s') h={} query_results.map{|sol| (h[sol[:observation].to_s] ||= {})[sol[:property].to_s] = sol[:value].to_s } if shorten_uris newh= {} h.map{|k,v| newh[strip_uri(k)] ||= {} v.map{|kk,vv| newh[strip_uri(k)][strip_uri(kk)] = strip_uri(vv) } } newh else h end end |
#sanitize(array) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/publisci/parser.rb', line 8 def sanitize(array) #remove spaces and other special characters array = Array(array) processed = [] array.map{|entry| if entry.is_a? String if is_uri? entry processed << entry.gsub(/[\s]/,'_') else processed << entry.gsub(/[\s]/,'_') end else processed << entry end } processed end |
#sanitize_hash(h) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/publisci/parser.rb', line 26 def sanitize_hash(h) mappings = {} h.keys.map{|k| if(k.is_a? String) mappings[k] = k.gsub(' ','_') end } h.keys.map{|k| h[mappings[k]] = h.delete(k) if mappings[k] } h end |
#strip_prefixes(string) ⇒ Object
261 262 263 |
# File 'lib/publisci/parser.rb', line 261 def strip_prefixes(string) string.to_s.split(':').last end |
#strip_uri(uri) ⇒ Object
255 256 257 258 259 |
# File 'lib/publisci/parser.rb', line 255 def strip_uri(uri) uri = uri.to_s.dup uri[-1] = '' if uri[-1] == '>' uri.to_s.split('/').last.split('#').last end |
#to_literal(obj, options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/publisci/parser.rb', line 124 def to_literal(obj, ={}) if obj.is_a? String # Depressing that there's no more elegant way to check if a string is # a number... if val = Integer(obj) rescue nil val elsif val = Float(obj) rescue nil val else '"'+obj+'"' end elsif obj == nil && [:encode_nulls] #TODO decide the right way to handle missing values, since RDF has no null 'rdf:nil' else obj end end |
#to_resource(obj, options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/publisci/parser.rb', line 98 def to_resource(obj, ={}) if obj.is_a? String if is_uri? obj obj = RDF::Resource(obj).to_base unless obj[/\w+:\w/] else #TODO decide the right way to handle missing values, since RDF has no null #probably throw an error here since a missing resource is a bigger problem obj = "rdf:nil" if obj.empty? obj= obj.to_s.gsub(' ','_') end obj #TODO remove special characters (faster) as well (eg '?') elsif obj == nil && [:encode_nulls] 'rdf:nil' elsif obj.is_a? Numeric #resources cannot be referred to purely by integer (?) "n"+obj.to_s else obj end end |
#turtle_indent(turtle_str) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/publisci/parser.rb', line 230 def turtle_indent(turtle_str) tabs = 0 turtle_str.split("\n").map{|str| case str[-1] when "." last_tabs = tabs tabs = 0 (" " * last_tabs) + str when ";" last_tabs = tabs tabs = 1 if tabs == 0 (" " * last_tabs) + str else last_tabs = tabs if str.size < 2 tabs = 0 else tabs += 1 end (" " * last_tabs) + str end }.join("\n") end |