]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
plugin(oxford): fix result handling, closes #37
authorMatthias Hecker <mail@apoc.cc>
Mon, 13 Apr 2020 18:07:02 +0000 (20:07 +0200)
committerMatthias Hecker <mail@apoc.cc>
Mon, 13 Apr 2020 18:07:02 +0000 (20:07 +0200)
data/rbot/plugins/oxford.rb

index 29ba3dce23c0c8f7353f98c3525efa0dd6bab37e..5c9b0e4eb4f481f0f0b6411fcac11acf1284430e 100644 (file)
@@ -9,6 +9,7 @@
 # License:: GPL v2
 #
 require 'cgi'
+require 'uri'
 
 class OxfordPlugin < Plugin
   Config.register Config::IntegerValue.new(
@@ -26,23 +27,43 @@ class OxfordPlugin < Plugin
   end
 
   def oxford(m, params)
-    word = params[:word].join
+    word = params[:word].join(' ')
 
-    url = "#{@base_url}/definition/#{CGI.escape word}"
+    url = "#{@base_url}/definition/#{URI::encode word}"
 
     begin
+      debug "searching definition for #{word.inspect}"
+
       response = @bot.httputil.get(url, resp: true)
       definition = parse_definition(response)
 
+      # try to find alternative word (different spelling, typos, etc.)
       if definition.empty?
-        closest = response.xpath('//div[@class="no-exact-matches"]//ul/li/a').first
-
-        url = @base_url + closest['href']
+        debug "search for alternative spelling result"
+        url = title = nil
+        exact_matches = response.xpath('//div[@class="no-exact-matches"]//ul/li/a')
+        if not exact_matches.empty? and not exact_matches.first['href'].empty?
+          url = @base_url + exact_matches.first['href']
+          title = exact_matches.first.content
+        else
+          debug 'use web-service to find alternative result'
+          # alternatively attempt to use their webservice (json-p) instead
+          url = "#{@base_url}/search/dataset.js?dataset=noad&dictionary=en&query=#{CGI.escape word}"
+          response = @bot.httputil.get(url, headers: {'X-Requested-With': 'XMLHttpRequest'})
+          alternative = response.gsub(/\\/, '').scan(/href="([^"]+)">([^<]+)</)
+          url = @base_url + alternative.first[0]
+          title = alternative.first[1]
+        end
 
-        m.reply "did you mean: #{Bold}#{closest.content.ircify_html}#{NormalText}"
+        debug "search for alternative spelling result, returned title=#{title.inspect} url=#{url.inspect}"
 
-        response = @bot.httputil.get(url, resp: true)
-        definition = parse_definition(response)
+        if url and title
+          unless title.downcase == word.downcase
+            m.reply "did you mean: #{Bold}#{title.ircify_html}#{NormalText}?"
+          end
+          response = @bot.httputil.get(url, resp: true)
+          definition = parse_definition(response)
+        end
       end
     rescue => e
       m.reply "error accessing lexico url -> #{url}"
@@ -50,7 +71,7 @@ class OxfordPlugin < Plugin
       return
     end
 
-    if definition
+    unless definition.empty?
       m.reply definition.ircify_html, max_lines: @bot.config['oxford.max_lines']
     else
       m.reply "couldn't find a definition for #{word} on oxford dictionary"