IBM Watson NLU in Ruby

196 Views Asked by At

I am attempting to use the watson api client from https://github.com/suchowan/watson-api-client.

I have this written so far based on documentation from https://watson-api-explorer.ng.bluemix.net/listings/natural-language-understanding-v1.json:

require 'watson-api-client'

service = WatsonAPIClient::NaturalLanguageUnderstanding.new(
  :user=>"xxxxxxxxxxxx",
  :password=>"yyyyyyyyy",
  :verify_ssl=>OpenSSL::SSL::VERIFY_NONE
)

result = service.analyze(
  'version'          => "2018-03-16",
  'parameters'       => "keywords.sentiment",
  'source'           => "The quick brown fox jumps over the lazy cat"
)
p JSON.parse(result.body)

The issue is since there isn't anything I can find for sending the request through ruby I may be using the wrong parameters. For instance I get ArgumentError (Extra parameter(s) : 'source' with this current code. I've tried replacing source with text to no avail. Has anyone successfully made a request like this in Ruby or knows what the proper parameters needed are?

Thanks.

1

There are 1 best solutions below

0
On

looking at the API at https://www.ibm.com/watson/developercloud/natural-language-understanding/api/v1/#post-analyze it looks like parameters should be a JSON object. I also do not see any source (as Simon's comment states, use text) parameter in the API documentation.

Perhaps before jumping into the watson-api-client gem, attempt to make a call using Net::HTTP (documentation https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html) This way you can see exactly what is expected. You also may be able to make a more tailored solution for connecting to the Watson API.

for example

    uri = URI('https://gateway.watsonplatform.net/natural-language-understanding/api/v1/analyze?version=2018-03-16')
  Net::HTTP.start(uri.host, uri.port) do |http|
    request = Net::HTTP::Post.new uri
    request['Content-Type'] = 'application/json'
    request.body = {text: 'your test', keywords: {sentiment: true}}.to_json    
    request.basic_auth 'username', 'password'


    response = http.request request # Net::HTTPResponse object
  end

Please note the above was just produced, not tested. Hope this helps out.