2

我正在尝试使用来自https://github.com/suchowan/watson-api-client.

到目前为止,我已经根据以下文档编写了这篇文章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)

问题是因为我找不到任何通过 ruby​​ 发送请求的东西,我可能使用了错误的参数。例如,我得到ArgumentError (Extra parameter(s) : 'source'了这个当前的代码。我试过用文本替换源代码无济于事。有没有人在 Ruby 中成功地提出过这样的请求,或者知道所需的正确参数是什么?

谢谢。

4

1 回答 1

0

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.

于 2018-06-04T11:55:50.530 回答