因此,我终于让ping命令使用httr作为下面的脚本。
library(httr)
curl_com = GET("https://api.rosette.com/rest/v1/ping", add_headers(`X-RosetteAPI-Key` = "my api"))很棒的东西,但现在,因为我在下一点。
现在我想调用另一个api来进行情感分析。
curl -X POST \
-H "X-RosetteAPI-Key: your_api_key" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Cache-Control: no-cache" \
-d '{"content": "Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”" }' \
"https://api.rosette.com/rest/v1/sentiment"我得到了错误405 --方法不允许--您试图使用无效的方法访问Rosette。我不知道如何使用httr翻译上面的curl命令,有人能一步一步地告诉我吗?
希望有人能帮助佩蒂
发布于 2016-10-10 22:49:56
嗯,虽然我的评论说他们有R代码是正确的,但这是可怕的R代码。
您现在只需使用rosette R包来访问完整的API。只需确保将Rosette键放入ROSETTE_API_KEY (最容易编辑~/.Renviron)。
devtools::install_github("hrbrmstr/rosette")
ros_sentiment("Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, 'The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.'")
## $document
## $document$label
## [1] "pos"
##
## $document$confidence
## [1] 0.7962072
##
##
## $entities
## type mention normalized count entityId sentiment.label sentiment.confidence
## 1 PERSON Dan Aykroyd Dan Aykroyd 2 Q105221 pos 0.6385089
## 2 ORGANIZATION Hollywood Reporter Hollywood Reporter 1 Q61503 pos 0.5338094API的其余部分也在那里:
rosette_api_key:获取或设置ROSETTE_API_KEY值ros_categories:玫瑰花环API分类服务ros_embedding:玫瑰API文本嵌入服务ros_entities:玫瑰花API实体提取服务ros_info:玫瑰花API版本信息ros_language:玫瑰花环API语言识别服务ros_make_name:创建一个名称对象ros_morph:玫瑰花API形态分析服务ros_name_similarity:玫瑰花API版本信息ros_name_translation:玫瑰花API名称翻译服务ros_ping Rosette:API可用性ros_relationships:玫瑰花API关系提取服务ros_sentences:玫瑰花API语句确定服务ros_sentiment:玫瑰花API情感分析服务ros_tokens:玫瑰花环API令牌化服务它将在本月晚些时候(当我有时间来润色它的时候)。
发布于 2016-10-10 21:30:14
从curl命令到httr的简单转换结果如下所示:
response = POST("https://api.rosette.com/rest/v1/sentiment",
add_headers(
`X-RosetteAPI-Key` = "",
`Content-Type` = "application/json",
Accept = "application/json",
`Cache-Control` = "no-cache"
),
content = list(content = "Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, 'The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.'"))我们走过去吧。
curl命令中,有四行以-H开头。这些是头语句,因此成为httr::GET中的httr::GET命令的一部分。curl中,-d命令引用数据,手册页建议以该数据作为内容发送POST请求。在这种情况下,我们使用httr::POST和content参数。您可以替换这一行:
`X-RosetteAPI-Key` = "",..。用你合适的钥匙。因为我没有钥匙,所以当我看到回应时,我得到了未经授权的:
content(response)
#> $code
#> [1] "unauthorized"
#>
#> $message
#> [1] "authentication required to access this resource"https://stackoverflow.com/questions/39965952
复制相似问题