class TwitterCldr::DataReaders::AdditionalDateFormatSelector
Attributes
pattern_hash[R]
Public Class Methods
new(pattern_hash)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 11 def initialize(pattern_hash) @pattern_hash = pattern_hash end
Public Instance Methods
find_closest(goal_pattern)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 15 def find_closest(goal_pattern) if !goal_pattern || goal_pattern.strip.empty? nil else cache_key = TwitterCldr::Utils.compute_cache_key(goal_pattern) pattern_cache[cache_key] ||= if @pattern_hash.include?(goal_pattern.to_sym) goal_pattern.to_sym else rank(goal_pattern).min do |(p1, score1), (p2, score2)| score1 <=> score2 end.first.to_sym end @pattern_hash[pattern_cache[cache_key]] end end
patterns()
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 32 def patterns @pattern_hash.keys.map(&:to_s) end
Protected Instance Methods
all_separated_patterns()
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 51 def all_separated_patterns @separated_patterns ||= @pattern_hash.map { |pattern, _| separate(pattern.to_s) } end
count_score(entities, goal_entities)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 82 def count_score(entities, goal_entities) goal_entities.inject(0) do |sum, goal_entity| if found_entity = entities.select { |entity| entity[0] == goal_entity[0] }.first sum + (found_entity.size - goal_entity.size).abs else sum end end end
exist_score(entities, goal_entities)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 72 def exist_score(entities, goal_entities) goal_entities.inject(0) do |sum, goal_entity| if !entities.any? { |entity| entity[0] == goal_entity[0] } sum + 1 else sum end end end
pattern_cache()
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 38 def pattern_cache @pattern_cache ||= {} end
position_score(entities, goal_entities)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 62 def position_score(entities, goal_entities) goal_entities.each_with_index.inject(0) do |sum, (goal_entity, index)| if found = entities.index(goal_entity) sum + (found - index).abs else sum end end end
rank(goal_pattern)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 92 def rank(goal_pattern) separated_goal_pattern = separate(goal_pattern) all_separated_patterns.inject({}) do |ret, separated_pattern| ret[separated_pattern.join] = score(separated_pattern, separated_goal_pattern) ret end end
score(entities, goal_entities)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 55 def score(entities, goal_entities) # weight existence a little more heavily than the others score = exist_score(entities, goal_entities) * 2 score += position_score(entities, goal_entities) score + count_score(entities, goal_entities) end
separate(pattern_key)
click to toggle source
# File lib/twitter_cldr/data_readers/additional_date_format_selector.rb, line 42 def separate(pattern_key) last_char = "" pattern_key.each_char.each_with_index.inject([]) do |ret, (char, index)| char == last_char ? ret[-1] += char : ret << char last_char = char ret end end