- 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