Class: See5::Schema
- Inherits:
-
Object
- Object
- See5::Schema
- Defined in:
- lib/see5/schema.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#class_attribute ⇒ Object
readonly
Returns the value of attribute class_attribute.
Class Method Summary collapse
-
.from_dataset(data, class_attribute: nil) ⇒ Object
Infers a schema from a dataset.
Instance Method Summary collapse
-
#initialize(classes:, attributes:, class_attribute: nil) ⇒ Schema
constructor
A new instance of Schema.
- #names_file_contents ⇒ Object
Constructor Details
#initialize(classes:, attributes:, class_attribute: nil) ⇒ Schema
Returns a new instance of Schema.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/see5/schema.rb', line 7 def initialize(classes:, attributes:, class_attribute: nil) @classes = classes @attributes = attributes @class_attribute = class_attribute&.to_sym || :class_attribute # if the class attribute doesn't exist in the attributes, add it unless @attributes.key?(@class_attribute) @attributes[@class_attribute] = @classes end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
5 6 7 |
# File 'lib/see5/schema.rb', line 5 def attributes @attributes end |
#class_attribute ⇒ Object (readonly)
Returns the value of attribute class_attribute.
5 6 7 |
# File 'lib/see5/schema.rb', line 5 def class_attribute @class_attribute end |
Class Method Details
.from_dataset(data, class_attribute: nil) ⇒ Object
Infers a schema from a dataset
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/see5/schema.rb', line 27 def self.from_dataset(data, class_attribute: nil) classes = data.map { |record| record[class_attribute.to_sym] }.uniq attributes = {} data.each do |record| record.each do |key, value| if !attributes.include?(key) attributes[key] = [value] elsif !attributes[key].include?(value) attributes[key].append(value) end end end new(classes: classes, attributes: attributes, class_attribute: class_attribute) end |
Instance Method Details
#names_file_contents ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/see5/schema.rb', line 18 def names_file_contents class_attribute.to_s + # TODO: continuous class attribute "\n" + attributes.map do |attr, vals| "#{attr}: #{vals.join(',')}" end.join("\n") end |