Created
June 26, 2012 12:51
-
-
Save isstaif/2995632 to your computer and use it in GitHub Desktop.
Simple Ruby RDF generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'csv' | |
| class Observation | |
| attr :id | |
| attr :province | |
| attr :indicator_name | |
| attr :indicator_value | |
| attr :year | |
| attr :dataset | |
| def initialize(id, province, year, indicator_name, indicator_value) | |
| @id = id | |
| @province = province | |
| @year = year | |
| @indicator_name = indicator_name | |
| @indicator_value = indicator_value | |
| end | |
| def to_s | |
| obs = Hash.new | |
| obs['open'] = %{<rdf:Description rdf:about="http://stats.gov.sy/#{@year}/#{@indicator_name}.rdf#obs#{@id}">} | |
| obs['rdftype'] = %{\n\t<rdf:type rdf:resource="http://purl.org/linked-data/cube#Observation" />} | |
| obs['province'] = %{\n\t<cbssyr:province rdf:resource="http://cbssyr.herokuapp.com/provinces.rdf##{@province}" />} | |
| obs['year'] = %{\n\t<cbssyr:year>#{@year}</cbssyr:year>} | |
| obs['indicator'] = %{\n\t<cbssyr:#{@indicator_name}>#{@indicator_value}</cbssyr:#{@indicator_name}>} | |
| obs['dataset'] = %{\n\t<qb:dataset rdf:resource="http://stats.gov.sy/#{@year}/#{@indicator_name}.rdf#dataset"/>} | |
| obs['label'] = %{\n\t<rdfs:label>OBS#{@id}</rdfs:label>} | |
| obs['end'] = %{\n</rdf:Description>} | |
| export = "" | |
| obs.each do |key, value| | |
| export = export + value | |
| end | |
| export | |
| end | |
| end | |
| obser = Hash.new | |
| provinces = Array.new | |
| provinces.push "Damascus" | |
| provinces.push "Aleppo" | |
| provinces.push "Rural_Damascus" | |
| provinces.push "Homs" | |
| provinces.push "Hama" | |
| provinces.push "Lattakia" | |
| provinces.push "Idleb" | |
| provinces.push "Al-Hasakeh" | |
| provinces.push "Dir-ez-Zor" | |
| provinces.push "Tartous" | |
| provinces.push "Al-Rakka" | |
| provinces.push "Daraa" | |
| provinces.push "Al-Sweida" | |
| provinces.push "Al-Quneitra" | |
| # enter data here | |
| indicators_array = [] | |
| year = 0 | |
| indicator = "" | |
| indicator_label = "" | |
| Dir.foreach(Dir.pwd) do |item| | |
| next if item == '.' or item == '..' or item == 'test.rb' or item == 'data' | |
| i = 0 | |
| puts item | |
| CSV.foreach(item) do |row| | |
| if i == 0 | |
| year = row[0] | |
| puts year | |
| end | |
| if i == 1 | |
| indicator_label = row[0].downcase | |
| indicator = row[0].downcase | |
| indicator.gsub!(" ","-") | |
| indicators_array.push [indicator, indicator_label] | |
| end | |
| j = i - 2 | |
| if j >= 0 and j <= 13 | |
| j = i - 2 | |
| obser[j] = Observation.new(j, provinces[j], year, indicator, row[1]) | |
| end | |
| i = i + 1 | |
| end | |
| File.open("data/" + indicator, 'w') do |f| | |
| #generating file header | |
| f.puts <<DOC | |
| <?xml version="1.0" encoding="utf-8" ?> | |
| <rdf:RDF | |
| xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | |
| xmlns:qb="http://purl.org/linked-data/cube#" | |
| xmlns:provinces="http://cbssyr.herokuapp.com/provinces.rdf#" | |
| xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" | |
| xmlns:cbssyr="http://cbssyr.herokuapp.com/cbssyr#" | |
| > | |
| DOC | |
| #generating dataset information | |
| f.puts <<DOC | |
| <rdf:Description rdf:about="http://stats.gov.sy/#{year}/#{indicator}"> | |
| <rdf:type rdf:resource="http://purl.org/linked-data/cube#DataSet" /> | |
| <qb:structure rdf:resource="http://stats.gov.sy/dsd/#{indicator}"/> | |
| <rdfs:label>#{indicator_label} in year #{year}</rdfs:label> | |
| </rdf:Description> | |
| DOC | |
| f.puts | |
| obser.each do |key, value| | |
| f.puts value.to_s | |
| f.puts | |
| end | |
| f.puts <<DOC | |
| </rdf:RDF> | |
| DOC | |
| end | |
| end | |
| File.open("cbssyr", 'w') do |f| | |
| f.puts <<DOC | |
| <?xml version="1.0" encoding="utf-8" ?> | |
| <rdf:RDF | |
| xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" | |
| xmlns:qb="http://purl.org/linked-data/cube#" | |
| xmlns:provinces="http://cbssyr.herokuapp.com/provinces.rdf#" | |
| xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" | |
| > | |
| <rdf:Description rdf:about="cbssyr#province"> | |
| <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#rdf:Property" /> | |
| <rdf:type rdf:resource="http://purl.org/linked-data/cube#qb:DimensionProperty" /> | |
| <rdfs:label>province</rdfs:label> | |
| <rdfs:range rdf:resource="file:///home/amjad/linked_data/provinces.rdf#Province" /> | |
| </rdf:Description> | |
| <rdf:Description rdf:about="cbssyr#year"> | |
| <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#rdf:Property" /> | |
| <rdf:type rdf:resource="http://purl.org/linked-data/cube#qb:DimensionProperty" /> | |
| <rdfs:label>year</rdfs:label> | |
| <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#gYear" /> | |
| </rdf:Description> | |
| DOC | |
| indicators_array.each do |value| | |
| f.puts <<DOC | |
| <rdf:Description rdf:about="cbssyr##{value[0]}"> | |
| <rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#rdf:Property" /> | |
| <rdf:type rdf:resource="http://purl.org/linked-data/cube#qb:MeasureProperty" /> | |
| <rdfs:label>#{value[1]}</rdfs:label> | |
| <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#nonNegativeInteger" /> | |
| </rdf:Description> | |
| DOC | |
| end | |
| f.puts <<DOC | |
| </rdf:RDF> | |
| DOC | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment