module ClockFlipRank
ClockFlipRank — rank upside-down flip word candidates by lookup quality. Public: pick_best Depends: ClockFlip, ClockWordnet Tests: test/lib/clock_flip_rank_test.rb
Constants
- URBAN_BASE
- WIKI_BASE
- WORDNET_BASE
Public Instance Methods
Source
# File lib/clock/flip_rank.rb, line 64 def beats_cached?(winner, cached) score_for(winner) > score_for_cached(cached) end
Source
# File lib/clock/flip_rank.rb, line 87 def best_external_candidate(candidates, lookup, base:) best = nil best_score = -1 candidates.each_with_index do |word, preference| line, bonus = normalize_external_result(lookup.call(word)) next if line.nil? || line.to_s.strip.empty? candidate_score = base + bonus + preference_score(preference) next if candidate_score <= best_score best_score = candidate_score best = { flip_word: word, lines: [line], source: lookup_source(base) } end best end
Source
# File lib/clock/flip_rank.rb, line 68 def best_wordnet_candidate(candidates, wordnet_lookup) best = nil best_score = -1 candidates.each_with_index do |word, preference| lines = wordnet_lookup.call(word) next if lines.nil? || lines.empty? score = WORDNET_BASE + lines.length + preference_score(preference) next if score <= best_score best_score = score best = { flip_word: word, lines: lines, source: "wordnet" } end best end
Source
# File lib/clock/flip_rank.rb, line 125 def lookup_source(base) case base when URBAN_BASE then "urban" when WIKI_BASE then "wiki" else "unknown" end end
Source
# File lib/clock/flip_rank.rb, line 106 def normalize_external_result(result) case result when Hash line = result[:line] || result["line"] bonus = (result[:thumbs_up] || result["thumbs_up"]).to_i * 0.01 [line, bonus] when String [result, 0] else [nil, 0] end end
Source
# File lib/clock/flip_rank.rb, line 18 def pick_best( candidates:, wordnet_lookup:, urban_lookup:, wiki_lookup:, throttle:, store:, now: Time.now ) list = Array(candidates).map { |word| word.to_s.strip.downcase }.reject(&:empty?) return nil if list.empty? wordnet_winner = best_wordnet_candidate(list, wordnet_lookup) return wordnet_winner if wordnet_winner return nil unless throttle.allowed?(store: store, now: now) urban_winner = best_external_candidate(list, urban_lookup, base: URBAN_BASE) if urban_winner throttle.record!(store: store, now: now) return urban_winner end wiki_winner = best_external_candidate(list, wiki_lookup, base: WIKI_BASE) if wiki_winner throttle.record!(store: store, now: now) return wiki_winner end nil end
Source
# File lib/clock/flip_rank.rb, line 120 def preference_score(preference) -preference * 0.001 end
Source
# File lib/clock/flip_rank.rb, line 50 def score_for(winner) return -1 unless winner.is_a?(Hash) base = source_base(winner[:source]) base + Array(winner[:lines]).length end
Source
# File lib/clock/flip_rank.rb, line 57 def score_for_cached(entry) return -1 unless entry.is_a?(Hash) base = source_base(entry["source"]) base + Array(entry["lines"]).length end
Source
# File lib/clock/flip_rank.rb, line 134 def source_base(source) case source.to_s when "wordnet" then WORDNET_BASE when "urban" then URBAN_BASE when "wiki" then WIKI_BASE else 0 end end