Class: Isq::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/isq/dataset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries: [], math_entries: []) ⇒ Dataset

Returns a new instance of Dataset.



7
8
9
10
11
# File 'lib/isq/dataset.rb', line 7

def initialize(entries: [], math_entries: [])
  @entries = Array(entries)
  @math_entries = Array(math_entries)
  index!
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



5
6
7
# File 'lib/isq/dataset.rb', line 5

def entries
  @entries
end

#math_entriesObject (readonly)

Returns the value of attribute math_entries.



5
6
7
# File 'lib/isq/dataset.rb', line 5

def math_entries
  @math_entries
end

#part_keysObject (readonly)

Returns the value of attribute part_keys.



5
6
7
# File 'lib/isq/dataset.rb', line 5

def part_keys
  @part_keys
end

Class Method Details

.load(dataset_dir) ⇒ Object



13
14
15
16
17
18
# File 'lib/isq/dataset.rb', line 13

def self.load(dataset_dir)
  entries = load_yaml_file(dataset_dir, "quantities.yaml") { |yaml| Isq::Quantity.from_yaml(yaml) }
  math_entries = load_yaml_file(dataset_dir, "math.yaml") { |yaml| Isq::MathConcept.from_yaml(yaml) }

  new(entries: entries, math_entries: math_entries)
end

.load_yaml_file(dir, filename) {|yaml| ... } ⇒ Object

Yields:

  • (yaml)


60
61
62
63
64
65
66
# File 'lib/isq/dataset.rb', line 60

def self.load_yaml_file(dir, filename)
  path = File.join(dir, filename)
  return [] unless File.exist?(path)

  yaml = File.read(path)
  yield yaml
end

Instance Method Details

#counts_by_partObject



44
45
46
# File 'lib/isq/dataset.rb', line 44

def counts_by_part
  by_part.transform_values(&:length)
end

#entries_for_part(part_key) ⇒ Object



20
21
22
# File 'lib/isq/dataset.rb', line 20

def entries_for_part(part_key)
  by_part[part_key] || []
end

#total_countObject



40
41
42
# File 'lib/isq/dataset.rb', line 40

def total_count
  @entries.length + @math_entries.length
end

#unique_units_for_part(part_key) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/isq/dataset.rb', line 24

def unique_units_for_part(part_key)
  part_entries = entries_for_part(part_key)
  seen = Set.new
  part_entries.each_with_object([]) do |entry, acc|
    next unless entry.is_a?(Isq::Quantity)
    next unless entry.has_unit

    Array(entry.has_unit).each do |unit_ref|
      next if seen.include?(unit_ref)

      seen << unit_ref
      acc << unit_ref
    end
  end
end