5 # :title: Oxford Dictionary lookup plugin for rbot
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
14 class OxfordPlugin < Plugin
15 Config.register Config::IntegerValue.new(
18 :desc => 'The number of lines to respond with.')
22 @base_url = "https://www.lexico.com"
25 def help(plugin, topic="")
26 'oxford <word>: check for <word> on the lexico english dictionary (powered by oxford english dictionary).'
30 word = params[:word].join(' ')
32 url = "#{@base_url}/definition/#{URI::encode word}"
35 debug "searching definition for #{word.inspect}"
37 response = @bot.httputil.get(url, resp: true)
38 definition = parse_definition(response)
40 # try to find alternative word (different spelling, typos, etc.)
42 debug "search for alternative spelling result"
44 exact_matches = response.xpath('//div[@class="no-exact-matches"]//ul/li/a')
45 if not exact_matches.empty? and not exact_matches.first['href'].empty?
46 url = @base_url + exact_matches.first['href']
47 title = exact_matches.first.content
49 debug 'use web-service to find alternative result'
50 # alternatively attempt to use their webservice (json-p) instead
51 url = "#{@base_url}/search/dataset.js?dataset=noad&dictionary=en&query=#{CGI.escape word}"
52 response = @bot.httputil.get(url, headers: {'X-Requested-With': 'XMLHttpRequest'})
53 alternative = response.gsub(/\\/, '').scan(/href="([^"]+)">([^<]+)</)
54 unless alternative.empty?
55 url = @base_url + alternative.first[0]
56 title = alternative.first[1]
60 debug "search for alternative spelling result, returned title=#{title.inspect} url=#{url.inspect}"
63 unless title.downcase == word.downcase
64 m.reply "did you mean: #{Bold}#{title.ircify_html}#{NormalText}?"
66 response = @bot.httputil.get(url, resp: true)
67 definition = parse_definition(response)
71 m.reply "error accessing lexico url -> #{url}"
76 unless definition.empty?
77 m.reply definition.ircify_html, max_lines: @bot.config['oxford.max_lines']
79 m.reply "couldn't find a definition for #{word} on oxford dictionary"
85 def parse_definition(r)
86 r.xpath('//section[@class="gramb"]//text()').map(&:content).join(' ')
90 plugin = OxfordPlugin.new
91 plugin.map 'oxford *word', :action => 'oxford', :threaded => true