ruby-on-rails - Ruby 中的 IBM Watson NLU
问题描述
我正在尝试使用来自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 中成功地提出过这样的请求,或者知道所需的正确参数是什么?
谢谢。
解决方案
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.
推荐阅读
- python - 尝试使用 groupby() 函数但不断收到“TypeError:列表索引必须是整数或切片,而不是 str”
- asp.net-mvc - 如何将值从 Javascript 发送到 ActionResult?
- python - 如何防止 Python 请求在运行后中止
- python - 按下提交按钮时,UNIQUE 约束失败。(姜戈)
- php - 如何从 Laravel 7 中包含多个图像的帖子中删除单个图像
- python - 有没有办法在熊猫中对字典进行分组
- ember.js - 即使存在于数据存储中,相关对象也不会显示
- python - 根据数据更改 numpy 数组大小
- html - :hover 在使用类来定位 Font-Awesome 图标时不起作用
- python - 在 kivy 应用程序中添加 MDdatatable 后下拉菜单未打开