module WhatsonTowns
Queensland locality hints for !whatson — disambiguation and silent towns.
Constants
- QUEENSLAND_TOWNS
- STATE_ALIASES
- USAGE_MESSAGE
Public Instance Methods
Source
# File lib/whatson_towns.rb, line 49 def accept_result?(parsed, title:, snippet:, url:) return true unless parsed[:disambiguate] text = "#{title} #{snippet} #{url}".downcase return false if wrong_state_mention?(text, parsed[:state]) true end
Source
# File lib/whatson_towns.rb, line 72 def normalize_key(text) text.to_s.strip.downcase end
Source
# File lib/whatson_towns.rb, line 24 def resolve(text) town = text.to_s.strip return { error: USAGE_MESSAGE } if town.empty? town_text, state_hint = split_state_hint(town) key = normalize_key(town_text) meta = QUEENSLAND_TOWNS[key] return { town: town, search_town: town, silent: false } unless meta state = state_hint || meta[:state] { town: meta[:label], search_town: search_label(meta[:label], state), state: state, region: meta[:region], silent: meta[:silent] || false, disambiguate: meta[:disambiguate] && state_hint.nil? } end
Source
# File lib/whatson_towns.rb, line 76 def search_label(label, state) case state when "NSW" "#{label} NSW" when "Queensland" "#{label} Queensland" else label.to_s end end
Source
# File lib/whatson_towns.rb, line 45 def silent?(parsed) parsed[:silent] == true end
Source
# File lib/whatson_towns.rb, line 58 def split_state_hint(text) words = text.to_s.strip.split(/\s+/) return [text.to_s.strip, nil] if words.length < 2 suffix = words.last.downcase state = STATE_ALIASES[suffix] return [text.to_s.strip, nil] unless state town_part = words[0..-2].join(" ").strip return [text.to_s.strip, nil] if town_part.empty? [town_part, state] end
Source
# File lib/whatson_towns.rb, line 87 def wrong_state_mention?(text, expected_state) case expected_state when "Queensland" text.match?(/\b(?:nsw|new\s+south\s+wales)\b/i) && !text.match?(/\b(?:qld|queensland)\b/i) when "NSW" text.match?(/\b(?:qld|queensland)\b/i) && !text.match?(/\b(?:nsw|new\s+south\s+wales)\b/i) else false end end