class Nametrainer::Collection
A class for a collection of persons, each with a name, a corresponding (image) file, and a score.
Create a Collection
instance with
collection = CollectionLoader.load( :directory => directory, :extensions => extensions )
You can get a random person from a collection with
person = collection.sample
Persons with a lower score are chosen more often than persons with a higher score.
Constants
- SCORE_FILE
Public Class Methods
Creates a Collection
instance. Expects an argument hash with:
:persons
- array of Person
instances :directory
- collection directory :rng
- random number generator, defaults to RNG
# File lib/nametrainer/collection.rb, line 37 def initialize(args) @collection = args[:persons] @directory = args[:directory] @rng = args[:rng] || RNG.new(:size => @collection.size, :weighting_factor => 6) end
Public Instance Methods
Delete score file.
# File lib/nametrainer/collection.rb, line 76 def delete_scores filename = File.expand_path("#{@directory}/#{SCORE_FILE}") File.delete(filename) end
Export all scores to file (YAML).
# File lib/nametrainer/collection.rb, line 70 def export_scores filename = File.expand_path("#{@directory}/#{SCORE_FILE}") File.open(filename, 'w') {|f| f.write(scores.to_yaml) } end
Loads the scores from file (YAML).
# File lib/nametrainer/collection.rb, line 63 def import_scores filename = File.expand_path("#{@directory}/#{SCORE_FILE}") new_scores = YAML::load(File.read filename) set_scores(new_scores) end
Returns an array of all names.
# File lib/nametrainer/collection.rb, line 44 def names map {|person| person.name } end
Returns a random element, preferring persons with a smaller score.
# File lib/nametrainer/collection.rb, line 82 def sample shuffle.sort[@rng.rand] # shuffle first to mix up elements with equal scores end
Returns a hash with the score of all persons (name => score).
# File lib/nametrainer/collection.rb, line 49 def scores Hash[map {|person| [person.name, person.score] }] end
Sets the score of some or all persons.
new_scores
- hash with (name => score) values
# File lib/nametrainer/collection.rb, line 56 def set_scores(new_scores) each do |person| person.score = new_scores[person.name] if new_scores[person.name] end end
Returns the successor of the specified element, if possible, or the first element.
element
- element whose successor should be returned
# File lib/nametrainer/collection.rb, line 90 def successor(element) element_index = index(element) return first unless element_index self[element_index + 1] || first end