Class: TEF::ProgramSelection::Selector
- Inherits:
-
Object
- Object
- TEF::ProgramSelection::Selector
- Defined in:
- lib/tef/ProgramSelection/ProgramSelector.rb
Overview
Program Selector class.
This class’s purpose is to provide a central list of all known programs, as well as to give the user a method of easily selecting a matching program for a given situation or query.
Instance Attribute Summary collapse
-
#group_weights ⇒ Hash<String, Numeric>
Weights of groups.
Instance Method Summary collapse
- #all_groups ⇒ Object
- #all_titles ⇒ Object
-
#fetch_ID(title, group_weights = {}) ⇒ nil, ID
Fetch an ID from the list of known IDs.
-
#fetch_string(str) ⇒ Object
Fetch an ID based on a string.
-
#initialize ⇒ Selector
constructor
A new instance of Selector.
-
#register_ID(program, pgroups = [], pvariant = nil) ⇒ Object
Register a new ID to the list of known IDs.
Constructor Details
#initialize ⇒ Selector
Returns a new instance of Selector.
18 19 20 21 22 23 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 18 def initialize() @known_programs = {} # Hash based on titles @known_groups = {} @group_weights = {} end |
Instance Attribute Details
#group_weights ⇒ Hash<String, Numeric>
Returns Weights of groups. Used when selecing a program via #fetch_ID or #fetch_string. Higher weights mean higher preference.
16 17 18 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 16 def group_weights @group_weights end |
Instance Method Details
#all_groups ⇒ Object
125 126 127 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 125 def all_groups() @known_groups.keys end |
#all_titles ⇒ Object
121 122 123 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 121 def all_titles() @known_programs.keys end |
#fetch_ID(title, group_weights = {}) ⇒ nil, ID
Fetch an ID from the list of known IDs.
This will fetch a specific ID from the list of IDs, based on its title and group weights. The title MUST match the given title. Then, the group scoring of each matching ID will be calculated, based on #group_weights and the given group weights. From all IDs with maximum group weight, a random variant will be selected, and will be returned.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 74 def fetch_ID(title, group_weights = {}) return nil if @known_programs[title].nil? weights = @group_weights.merge(group_weights) current_best = nil; current_list = []; @known_programs[title].each do |prg| p_score = prg.get_scoring(weights); current_best ||= p_score next if current_best > p_score if current_best == p_score current_list << prg else current_best = p_score current_list = [prg] end end current_list.sample end |
#fetch_string(str) ⇒ Object
Fetch an ID based on a string.
This function performs similarly to #fetch_ID, but is based on a more human readable string. The input is parsed as follows:
-
It is split at the first ‘ from ’
-
The first part is taken as a title.
-
The second part will be split through ‘ and ’. Each resulting array element is taken as group weight with a score of 2.
113 114 115 116 117 118 119 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 113 def fetch_string(str) title, groups = str.split(" from "); groups ||= ""; groups = groups.split(" and ").map { |g| [g, 2] }.to_h fetch_ID(title, groups) end |
#register_ID(program, pgroups = [], pvariant = nil) ⇒ Object
Register a new ID to the list of known IDs.
This will ensure that the given ID is present in the list of known IDs. It will then return either the given ID or an equivalent ID already present in the list. Either can be used for identification.
This function can be used in two ways, either by passing a ID as only argument, or by passing a String as first argument and optional group and variant specifiers.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tef/ProgramSelection/ProgramSelector.rb', line 41 def register_ID(program, pgroups = [], pvariant = nil) if program.is_a? String program = ID.new(program, pgroups, pvariant) end proglist = (@known_programs[program.title] ||= []) if found_prog = proglist.find { |prg| prg == program } return found_prog end proglist << program program.groups.each { |group| @known_groups[group] = true } program end |