Class: PubliSci::Writers::ARFF

Inherits:
Base
  • Object
show all
Defined in:
lib/publisci/writers/arff.rb

Instance Method Summary collapse

Methods inherited from Base

#codes, #dataSet, #dimensions, #handle_input, #measures, #observations, #repo_to_ruby, #turtle_to_ruby

Methods included from Analyzer

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

Methods included from RDFParser

#add_node, #bnode_value, #encode_value, #get_ary, #get_hashes, #is_complex?, #is_uri?, #load_string, #observation_hash, #sanitize, #sanitize_hash, #strip_prefixes, #strip_uri, #to_literal, #to_resource, #turtle_indent

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

include PubliSci::Query include PubliSci::RDFParser include PubliSci::Analyzer


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/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 << "\[email protected]\n"
  data.map { |d| str << Hash[d[1].sort].values.join(',') + "\n" }

  str
end

#from_store(repo, dataset = nil, title = nil, verbose = false) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/publisci/writers/arff.rb', line 61

def from_store(repo, dataset=nil, title=nil, verbose=false)
  # data = observation_hash(execute_from_file("observations.rq",repo,:graph,{"%{dataSet}"=>"<#{dataSet}>"}), true)

  dims = dimensions(repo,dataset)
  meas = measures(repo,dataset)
  data = observations(repo,dataset)
  codes = codes(repo,dataset)
  attributes = {}

  (dims | meas).map{|component|
    attributes[component] = case recommend_range(data.map{|o| o[1][component]})
      when "xsd:int"
        "integer"
      when "xsd:double"
        "real"
      when :coded
        if dims.include? component
          "{#{codes[component].join(', ')}}"
        else
          "string"
        end
      end
  }

  dataset = dataSet(repo) unless dataset
  title = dataset unless title
  build_arff(title,attributes,data,dataset)
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
59
# File 'lib/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 = dimensions(repo)
  meas = measures(repo)
  data = observations(repo)

  relation = dataSet(repo)
  codes = codes(repo)

  attributes = {}

  (dims | meas).map{|component|
    attributes[component] = case recommend_range(data.map{|o| o[1][component]})
      when "xsd:int"
        "integer"
      when "xsd:double"
        "real"
      when :coded
        if dims.include? component
          "{#{codes[component].join(', ')}}"
        else
          "string"
        end
      end
  }

  build_arff(relation, attributes, data, turtle_file)
end