module CerebrumHelper

Private Instance Methods

features_to_vector_index_lookup_table(features) click to toggle source
{a: 1}, {b: 6, c: 7}

-> {a: 0, b: 1, c: 2}

# File lib/cerebrum/cerebrum_helper.rb, line 13
def features_to_vector_index_lookup_table(features)
  flattened_feature_keys = features.inject(:merge)
  reindex_hash_values(flattened_feature_keys)
end
lookup_table_from_array(arr) click to toggle source
5, 3

to {5: 0, 3: 1}

# File lib/cerebrum/cerebrum_helper.rb, line 34
def lookup_table_from_array(arr)
  Hash[arr.each_with_index.map { |val, i| [val, i] }]
end
randos(size) click to toggle source
# File lib/cerebrum/cerebrum_helper.rb, line 8
def randos(size)
  Array.new(size) { rand }
end
reindex_hash_values(hash) click to toggle source

changes hash {a: 6, b: 7} to {a: 0, b: 1}

# File lib/cerebrum/cerebrum_helper.rb, line 19
def reindex_hash_values(hash)
  hash.each_with_index{ |pair, index| hash[pair[0]] = index }
end
to_features_given_vector(vector, lookup_table) click to toggle source

{a: 0, b: 1}, [6, 7] to {a: 6, b: 7}

# File lib/cerebrum/cerebrum_helper.rb, line 29
def to_features_given_vector(vector, lookup_table)
  lookup_table.keys.zip(vector).to_h
end
to_vector_given_features(features, lookup_table) click to toggle source

formats {a: 0, b: 1}, {a: 6} to [6, 0]

# File lib/cerebrum/cerebrum_helper.rb, line 24
def to_vector_given_features(features, lookup_table)
  lookup_table.map { |k,v| features[k] || 0 }
end
zeros(size) click to toggle source
# File lib/cerebrum/cerebrum_helper.rb, line 4
def zeros(size)
  Array.new(size, 0)
end