2017-07-05 12 views
0

JSONオブジェクトを渡すことによって、テキストアナリティクスAPIを使用して感情とスコアを検出しています。ドキュメントに記載されている情報に従って、レスポンスが200になると、apiは正しい結果とスコアを返します。今問題私は200のような応答を取得していているが、私は感情のスコアはさらにMicrosoftテキスト分析のトピック感情認識の出力結果を取得できません

リクエストオブジェクトrocesssingに使用するなっていない午前:以下

{ "documents": [{ "language": "en", "id": 1, "text": "I had a wonderful experience" }, { "language": "en", "id": 2, "text": "It was an Ugly picture" }] } 

response object: 

{ 
    "Version": { 
     "Major": 1, 
     "Minor": 1, 
     "Build": -1, 
     "Revision": -1, 
     "MajorRevision": -1, 
     "MinorRevision": -1 
    }, 
    "Content": { 
     "Headers": [{ 
      "Key": "Content-Type", 
      "Value": ["application/json; charset=utf-8"] 
     }] 
    }, 
    "StatusCode": 200, 
    "ReasonPhrase": "OK", 
    "Headers": [{ 
     "Key": "Transfer-Encoding", 
     "Value": ["chunked"] 
    }, { 
     "Key": "x-ms-transaction-count", 
     "Value": ["1"] 
    }, { 
     "Key": "x-aml-ta-request-id", 
     "Value": ["f78d8964-a2a1-4b67-aaa5-1a92b6ea25f9"] 
    }, { 
     "Key": "X-Content-Type-Options", 
     "Value": ["nosniff"] 
    }, { 
     "Key": "apim-request-id", 
     "Value": ["0882ea1e-6c31-4a2c-b3c0-1963ec0734c1"] 
    }, { 
     "Key": "Strict-Transport-Security", 
     "Value": ["max-age=31536000; includeSubDomains; preload"] 
    }, { 
     "Key": "Date", 
     "Value": ["Sat, 01 Jul 2017 19:49:10 GMT"] 
    }], 
    "RequestMessage": { 
     "Version": { 
      "Major": 1, 
      "Minor": 1, 
      "Build": -1, 
      "Revision": -1, 
      "MajorRevision": -1, 
      "MinorRevision": -1 
     }, 
     "Content": { 
      "Headers": [{ 
       "Key": "Content-Type", 
       "Value": ["application/json"] 
      }, { 
       "Key": "Content-Length", 
       "Value": ["135"] 
      }] 
     }, 
     "Method": { 
      "Method": "POST" 
     }, 
     "RequestUri": "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", 
     "Headers": [{ 
      "Key": "Ocp-Apim-Subscription-Key", 
      "Value": ["************************"] 
     }], 
     "Properties": {} 
    }, 
    "IsSuccessStatusCode": true 
} 

はJSONオブジェクトへのコードのパスであるとWEBAPI

を呼び出します続き
var client = new HttpClient(); 
      var queryString = HttpUtility.ParseQueryString(string.Empty); 
    enter code here 
      // Request headers 
      client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "******************"); 
      var uri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment"; 

      HttpResponseMessage response; 
      Document doc = new Document(); 
      byte[] byteData = Encoding.UTF8.GetBytes(doc.ReturnJson());      

      using (var content = new ByteArrayContent(byteData)) 
      { 
       content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
       response = await client.PostAsync(uri, content); 
       response.ToJSON(); 
      } 

は、Microsoftのコードとリファレンスを参照するためのリンクです:

https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/oper ations/56f30ceeeda5650db055a3c9

https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment

答えて

1

私はここでテストを行ったし、それが正常に働いている:私はこの問題を解決するにはあなたの助けに感謝したいと思い

using System; 
using System.Net.Http.Headers; 
using System.Text; 
using System.Net.Http; 
using System.Web; 
using System.IO; 

namespace CSHttpClientSample 
{ 
    static class Program 
    { 
     static void Main() 
     { 
      MakeRequest(); 
      Console.WriteLine("Hit ENTER to exit..."); 
      Console.ReadLine(); 
     } 

     static async void MakeRequest() 
     { 
      var client = new HttpClient(); 
      var queryString = ""; 

      // Request headers 
      client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "YOUR KEY IN HERE"); 

      var json = "{ \"documents\": [ {  \"language\": \"en\",  \"id\": \"1\",  \"text\": \"I had a wonderful experience\" }, {  \"language\": \"en\",  \"id\": \"2\",  \"text\": \"It was an Ugly picture\" } ]}"; 


      var uri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + queryString; 

      HttpResponseMessage response; 

      // Request body 
      byte[] byteData = Encoding.UTF8.GetBytes(json); 

      using (var content = new ByteArrayContent(byteData)) 
      { 
       content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
       response = await client.PostAsync(uri, content); 

       Console.WriteLine(response.Content.ReadAsStringAsync().Result); 
      } 

     } 
    } 
} 

OUTPUT

Hit ENTER to exit... 
{"documents":[{"score":0.972635705189504,"id":"1"},{"score":0.0438970184772759,"id":"2"}],"errors":[]} 
+0

を問題はAPIの応答を読んでいる間でした。私はこのような方法で読むはずです。 "response.Content.ReadAsStringAsync()。Result"しかし、 "response.ToJSON()"のようなJsonオブジェクトとして読み込んでいました。 – Uttam

関連する問題