使用Azure计算机学习 - 文本分析REST api,位于此处。需要通过POST将有效负载发送到服务器。我试图获得与IBM watson相似的结果
以下是我在控制台应用中尝试的内容,这是核心代码:
static IRestResponse GetResp(string url, string key, string jsonText) {
IRestClient client = new RestClient(url);
IRestRequest request = new RestRequest() { RequestFormat = DataFormat.Json };
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Ocp-Apim-Subscription-Key", key);
IRestResponse response = client.ExecuteAsPost(request, "POST");
}
// Here the code that serializes the object to look precisely like body advertised calls it:
string json = JsonConvert.SerializeObject(documents);
IRestResponse resp = GetResponse("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases", TaxonomyGlueKey, json);
序列化“文档”的消息正文是:
{
"documents": [
{
"language": "en",
"id": "4",
"text": "Lateral internal sphincterotomy and fissurectomy"
},
{
"language": "en",
"id": "5",
"text": "Fissurectomy and Botox injection"
}
]}
我收到错误请求错误。我已验证我的请求已发送并通过身份验证(之前已失败)。我也尝试了很多变化。
我可以尝试使用我的请求体,当将调试变量中的文本直接复制到Azure提供的主体时,它可以正常工作:
如果我使用上面的测试我得到预期的响应,状态200:
Transfer-Encoding: chunked
x-aml-ta-request-id: c4ea9fff-8068-42a3-99c4-68717acddcf5
X-Content-Type-Options: nosniff
apim-request-id: e5eb593b-96a3-4806-9143-1d83424569be
Date: Thu, 21 Jul 2016 14:14:44 GMT
Content-Type: application/json; charset=utf-8
{
"documents": [
{
"keyPhrases": [
"fissurectomy"
],
"id": "4"
},
{
"keyPhrases": [
"Botox injection"
],
"id": "5"
}
],
"errors": []
}
答案 0 :(得分:1)
我正在使用JQuery
和REST API
进行情感分析。我收到了与您收到的相同的错误。
我设法通过提供输入的JSON-serialized version
作为请求主体来实现它。
这是工作代码 -
$(function() {
var params ={
"documents": [
{
"language": "en",
"id": "1",
"text": "this is AWESOME!"
}
]
};
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param( params );,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<your subscription key here>");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
// Request body
data: JSON.stringify(params)
})
.done(function(data) {
alert("Sentiment score is " + data.documents[0].score);
})
.fail(function() {
alert("error");
});
});
答案 1 :(得分:0)
@Makk,看起来你正在使用C#代码。 在文本分析文档中,有一个quick-start部分,其中包含适用于您的C#示例。