Sha256: 992500364e6ca9ed8cdc9e401726b09ee7ee0bef69b9cdb04b6b46ef88fd4d95

Contents?: true

Size: 1.72 KB

Versions: 26

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

require "net/http"
module Boxcars
  # A Boxcar that uses the Wikipedia search API to get answers to questions.
  class WikipediaSearch < Boxcar
    # the description of this boxcar
    WDESC = "useful for when you need to answer questions about topics from Wikipedia." \
            "You should ask targeted questions"

    # implements a boxcar that uses the Wikipedia Search to get answers to questions.
    # @param name [String] The name of the boxcar. Defaults to classname.
    # @param description [String] A description of the boxcar. Defaults to SERPDESC.
    # @param serpapi_api_key [String] The API key to use for the SerpAPI. Defaults to Boxcars.configuration.serpapi_api_key.
    def initialize(name: "Wikipedia", description: WDESC)
      super(name: name, description: description)
    end

    # Get an answer from Google using the SerpAPI.
    # @param question [String] The question to ask Google.
    # @return [String] The answer to the question.
    def run(question)
      Boxcars.debug "Question: #{question}", :yellow
      uri = URI("https://en.wikipedia.org/w/api.php")
      params = { action: "query", list: "search", srsearch: question, format: "json" }
      uri.query = URI.encode_www_form(params)

      res = Net::HTTP.get_response(uri)
      raise "Error getting response from Wikipedia: #{res.body}" unless res.is_a?(Net::HTTPSuccess)

      response = JSON.parse res.body
      answer = response.dig("query", "search", 0, "snippet").to_s.gsub(/<[^>]*>/, "")
      pageid = response.dig("query", "search", 0, "pageid")
      answer = "#{answer}\nurl: https://en.wikipedia.org/?curid=#{pageid}" if pageid
      Boxcars.debug "Answer: #{answer}", :yellow, style: :bold
      answer
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
boxcars-0.4.10 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.9 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.8 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.7 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.6 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.5 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.4 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.3 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.2 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.1 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.4.0 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.3.5 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.3.4 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.3.3 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.3.2 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.3.1 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.2.16 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.2.15 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.2.14 lib/boxcars/boxcar/wikipedia_search.rb
boxcars-0.2.13 lib/boxcars/boxcar/wikipedia_search.rb