Class: PubliSci::Writer::ARFF

Inherits:
Object
  • Object
show all
Includes:
Analyzer, Parser, Query
Defined in:
lib/bio-publisci/writers/arff.rb

Instance Method Summary collapse

Methods included from Analyzer

#check_integrity, #dirty?, #recommend_range, #recommend_range_strings

Methods included from Parser

#get_ary, #get_hashes, #is_uri?, #load_string, #observation_hash, #sanitize, #sanitize_hash, #strip_prefixes, #strip_uri, #to_literal, #to_resource

Methods included from Query

#execute, #execute_from_file, #property_names, #property_values, #row_names, #vocabulary

Instance Method Details

#build_arff(relation, attributes, data, source) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bio-publisci/writers/arff.rb', line 8

def build_arff(relation, attributes, data, source)
  str = <<-EOS
% 1. Title: #{relation.capitalize} Database
%
% 2. Sources:
%    (a) Generated from RDF source #{source}
%
@RELATION #{relation}

EOS

  Hash[attributes.sort].map{|attribute,type|
    str << "@ATTRIBUTE #{attribute} #{type}\n"
  }

  str << "\n@DATA\n"
  data.map { |d| str << Hash[d[1].sort].values.join(',') + "\n" }

  str
end

#from_store(endpoint_url, variable_in = nil, variable_out = nil, verbose = false) ⇒ Object



60
61
62
# File 'lib/bio-publisci/writers/arff.rb', line 60

def from_store(endpoint_url,variable_in=nil, variable_out=nil, verbose=false)
  raise "not implemented yet"
end

#from_turtle(turtle_file, verbose = false) ⇒ Object



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
# File 'lib/bio-publisci/writers/arff.rb', line 29

def from_turtle(turtle_file, verbose=false)
  puts "loading #{turtle_file}" if verbose
  repo = RDF::Repository.load(turtle_file)
  puts "loaded #{repo.size} statements into temporary repo" if verbose

  dims = execute_from_file("dimensions.rq",repo,:graph).to_h.map{|d| [d[:dimension].to_s, d[:label].to_s]}
  meas = execute_from_file("measures.rq",repo,:graph).to_h.map{|m| [m[:measure].to_s, m[:label].to_s]}
  relation = execute_from_file("dataset.rq",repo,:graph).to_h.first[:label].to_s
  codes = execute_from_file("codes.rq",repo,:graph).to_h.map{|e| e.values.map(&:to_s)}.inject({}){|h,el|
    (h[el.first]||=[]) << el.last; h
  }

  data = observation_hash(execute_from_file("observations.rq",repo,:graph), true)
  attributes = {}
  (dims | meas).map{|component|
    attributes[component[1]] = case recommend_range(data.map{|o| o[1][component[1]]})
      when "xsd:int"
        "integer"
      when "xsd:double"
        "real"
      when :coded
        if dims.include? component
          "{#{codes[component[1]].join(',')}}"
        else
          "string"
        end
      end
  }
  build_arff(relation, attributes, data, turtle_file)
end