首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rosette无效方法的curl到httr

Rosette无效方法的curl到httr
EN

Stack Overflow用户
提问于 2016-10-10 20:06:09
回答 2查看 257关注 0票数 0

因此,我终于让ping命令使用httr作为下面的脚本。

代码语言:javascript
复制
library(httr)
curl_com  = GET("https://api.rosette.com/rest/v1/ping", add_headers(`X-RosetteAPI-Key` = "my api"))

很棒的东西,但现在,因为我在下一点。

现在我想调用另一个api来进行情感分析。

代码语言:javascript
复制
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命令,有人能一步一步地告诉我吗?

希望有人能帮助佩蒂

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-10 22:49:56

嗯,虽然我的评论说他们有R代码是正确的,但这是可怕的R代码。

您现在只需使用rosette R包来访问完整的API。只需确保将Rosette键放入ROSETTE_API_KEY (最容易编辑~/.Renviron)。

代码语言:javascript
复制
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.5338094

API的其余部分也在那里:

  • 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令牌化服务

它将在本月晚些时候(当我有时间来润色它的时候)。

票数 1
EN

Stack Overflow用户

发布于 2016-10-10 21:30:14

curl命令到httr的简单转换结果如下所示:

代码语言:javascript
复制
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.'"))

我们走过去吧。

  1. 在原始的curl命令中,有四行以-H开头。这些是头语句,因此成为httr::GET中的httr::GET命令的一部分。
  2. curl中,-d命令引用数据,手册页建议以该数据作为内容发送POST请求。在这种情况下,我们使用httr::POSTcontent参数。

您可以替换这一行:

代码语言:javascript
复制
`X-RosetteAPI-Key` = "",

..。用你合适的钥匙。因为我没有钥匙,所以当我看到回应时,我得到了未经授权的:

代码语言:javascript
复制
content(response)

#> $code
#> [1] "unauthorized"
#> 
#> $message
#> [1] "authentication required to access this resource"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39965952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档