- [word, word + "_1"].each { |check|
- url = @oxurl % CGI.escape(check)
- if params[:british]
- url << "?view=uk"
- end
- h = @bot.httputil.get(url, :max_redir => 5)
- if h
- defs = h.split("<span class=\"definition\">")
- defs = defs[1..-1].map {|d| d.split("</span>")[0]}
- if defs.size == 0
- return false if justcheck
- m.reply "#{word} not found"
- return false
- end
- m.reply("#{word}: #{url}") unless justcheck
- defn = defs[0]
- m.reply("#{Bold}%s#{Bold}: %s" % [word, defn.ircify_html(:nbsp => :space)], :overlong => :truncate)
- return true
+
+ url = "#{@base_url}/definition/#{CGI.escape word}"
+
+ begin
+ response = @bot.httputil.get(url, resp: true)
+ definition = parse_definition(response)
+
+ if definition.empty?
+ closest = response.xpath('//div[@class="no-exact-matches"]//ul/li/a').first
+
+ url = @base_url + closest['href']
+
+ m.reply "did you mean: #{Bold}#{closest.content.ircify_html}#{NormalText}"
+
+ response = @bot.httputil.get(url, resp: true)
+ definition = parse_definition(response)