module ClockWikiLookup
ClockWikiLookup — Wikipedia meanings for upside-down clock flip words. Public: lookup, resolve_result, format_line, ambiguous? Depends: WikiSearch, WikiReport, RabbotHttp Tests: test/lib/clock_wiki_lookup_test.rb, test/plugins/clock_test.rb
Constants
- MORE_ON_WIKIPEDIA_PATTERN
- NUMBERED_ITEM_PATTERN
Public Instance Methods
Source
# File lib/clock/wiki_lookup.rb, line 49 def ambiguous?(line) line.to_s.match?(/\bmay refer to:/i) end
Source
# File lib/clock/wiki_lookup.rb, line 119 def default_fetch ->(url) { RabbotHttp.fetch(url) } end
Source
# File lib/clock/wiki_lookup.rb, line 124 def default_search(fetch) lambda do |flip_word| require_relative "../../plugins/wiki" Wiki.search(flip_word, fetch: fetch) end end
Source
# File lib/clock/wiki_lookup.rb, line 86 def disambiguation_title(line) text = line.to_s.strip title = text.split(" — ", 2).first.to_s.strip title.empty? ? nil : title end
Source
# File lib/clock/wiki_lookup.rb, line 45 def format_line(flip_word, text) "#{flip_word} — #{text}" end
Source
# File lib/clock/wiki_lookup.rb, line 93 def inline_disambiguation_items(line) text = line.to_s body = text.sub(/\A.+?\bmay refer to:\s*/i, "").strip body = body.sub(/\s*\([^)]*\)\s*\z/, "").strip return [] if body.empty? parts = body.split(/\n{2,}/).map(&:strip).reject(&:empty?) return parts if parts.length > 1 if (match = body.match(/\A(.+?\bWar)\s+([A-Z].+)\z/m)) return [match[1].strip, match[2].strip] end parts = body.split(/(?<=[.!?])\s+(?=[A-Z])/).map(&:strip).reject(&:empty?) parts.length > 1 ? parts : [] end
Source
# File lib/clock/wiki_lookup.rb, line 18 def lookup(flip_word, fetch: nil, rng: Random.new, search: nil) fetch ||= default_fetch search ||= default_search(fetch) result = search.call(flip_word.to_s.strip) return nil if result.nil? || result.to_s.strip.empty? return nil if result.to_s.casecmp?("No article found") resolve_result(flip_word, result, fetch: fetch, rng: rng) rescue StandardError nil end
Source
# File lib/clock/wiki_lookup.rb, line 73 def numbered_items(lines) lines.drop(1).filter_map do |line| match = line.match(NUMBERED_ITEM_PATTERN) next unless match text = match[1].strip next if text.match?(MORE_ON_WIKIPEDIA_PATTERN) text end.reject(&:empty?) end
Source
# File lib/clock/wiki_lookup.rb, line 53 def pick_disambiguation_item(lines, fetch:, rng:) numbered = numbered_items(lines) return pick_random(numbered, rng) if numbered.length > 1 return numbered.first if numbered.length == 1 title = disambiguation_title(lines.first) if title && fetch items = WikiSearch.fetch_disambiguation_items(title, fetch: fetch) return pick_random(items, rng) if items.length > 1 return items.first if items.length == 1 end inline = inline_disambiguation_items(lines.first) return pick_random(inline, rng) if inline.length > 1 return inline.first if inline.length == 1 nil end
Source
# File lib/clock/wiki_lookup.rb, line 111 def pick_random(items, rng) pool = Array(items).map(&:to_s).reject(&:empty?) return nil if pool.empty? pool[rng.rand(pool.length)] end
Source
# File lib/clock/wiki_lookup.rb, line 30 def resolve_result(flip_word, result, fetch:, rng: Random.new) lines = result.to_s.split("\n").map(&:strip).reject(&:empty?) first = lines.first.to_s return nil if first.empty? if ambiguous?(first) picked = pick_disambiguation_item(lines, fetch: fetch, rng: rng) return format_line(flip_word, picked) if picked return nil end format_line(flip_word, first) end