这里有一个mashpe情感分析,curl代码返回json。如何将其与R相结合?
curl -X POST --包括'https://community-sentiment.p.mashape.com/text/‘\ -H 'X-Mashape-Key:https://community-sentiment.p.mashape.com/text/\ -H’内容-类型: application/x-www-form-urlencoded‘\ -H 'Accept: application/json’\ -d 'txt=Today是个好日子。
编辑:另外,我如何在变量中添加-d 'txt=Today是一个好的一天‘的部分?说文本<-“今天是美好的一天”,并使用r语法中的变量文本。很抱歉,如果它是非常基本的,我刚刚开始编辑R - user3548327。
发布于 2015-06-07 05:50:38
甚至在移除之后\(顺便说一句,我很久以前就这么做了!)并且确保所有的语法都是完整的,在我的R环境中,我得到了代码6的错误,因为上面的代码,或者以500个错误结束。作为最后的手段,离开系统(卷曲.)语法并使用postForm("http://sentiment.vivekn.com/api/text/",txt =“my句子”)。为我工作!谢谢你的帮助。
发布于 2015-06-06 17:56:20
只需运行它,并使用其中一个JSON包解析它:
R> res <- system("curl -s ....rest of your query as above...", intern=TRUE)
R> jsonlite::fromJSON(res[-(1:9)])
$result
$result$confidence
[1] "96.7434"
$result$sentiment
[1] "Positive"
R> 我添加了一个-s来保持curl的安静,并且需要忽略前9行不是JSON的代码.
编辑:如果没有明确的示例,OP似乎无法使其工作,下面是另一个副本和粘贴:
R> res <- system("curl -s -X POST --include \
'https://community-sentiment.p.mashape.com/text/' -H \
-X-Mashape-Key: \
pVke3AAqHzmsh4xNdsKrPshYHQC1p1H78y0jsn2uwaEPcU1TnF' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json' -d 'txt=Today is a good day'", \
intern=TRUE)
R> jsonlite::fromJSON(res[-(1:9)])
$result
$result$confidence
[1] "96.7434"
$result$sentiment
[1] "Positive"
R> 您需要删除上面的\并使其全部一行。
https://stackoverflow.com/questions/30685719
复制相似问题