2016-11-17 7 views
1

При отправке документа в API для ключевых фраз возвращенный ответ JSON имеет ошибку «Документ в запросе слишком велик для обработки. Ограничить размер документа до: 10240 байт «.Ограничение размера документа Microsoft Cognitive API в размере 10240 байт

Согласно https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-text-analytics-quick-start, «Максимальный размер одного документа, который может быть отправлен, составляет 10 КБ, а общий максимальный размер подаваемого ввода составляет 1 МБ. В один звонок может быть подано не более 1000 документов».

В этом документе речь идет строка длины 7713. байт длины с помощью Encoding.UTF8.GetBytes() является 7763.

Весь ByteArray, который представлен имеет длину 7965.

Меньший строки работают нормально, но любые строки, длина которых больше 3000, похоже, имеют эту проблему. Ниже приведен код, написанный в VB.NET:

' Create a JSONInput object containing the data to submit 
    Dim myJsonObject As New JSONInput 
    Dim input1 As New JSONText 
    input1.id = "1" 
    input1.text = text 
    myJsonObject.documents.Add(input1) 

    ' Translate the JSONInput object to a serialized JSON string 
    Dim jss As New JavaScriptSerializer() 
    Dim jsonString As String = jss.Serialize(myJsonObject) 

    ' Windows Cognitive Services URL 
    Dim request As System.Net.WebRequest = System.Net.WebRequest.Create("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases") 

    ' Set the Method property of the request to POST. 
    request.Method = "POST" 

    ' Add a header with the account key. 
    request.Headers.Add("Ocp-Apim-Subscription-Key", accountKey_TextAnalytics) 

    ' Create POST data and convert it to a byte array. 
    Dim postData As String = jsonString 
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) 

    ' Set the ContentType property of the WebRequest. 
    request.ContentType = "application/json" 

    ' Set the ContentLength property of the WebRequest. 
    request.ContentLength = byteArray.Length 

    ' Get the request stream. 
    Dim dataStream As System.IO.Stream = request.GetRequestStream() 

    ' Write the data to the request stream. 
    dataStream.Write(byteArray, 0, byteArray.Length) 

    ' Close the Stream object. 
    dataStream.Close() 

    ' Get the response. 
    Dim response As System.Net.WebResponse = request.GetResponse() 

    ' Get the stream containing content returned by the server. 
    dataStream = response.GetResponseStream() 

    ' Open the stream using a StreamReader for easy access. 
    Dim reader As New System.IO.StreamReader(dataStream) 

    ' Read the content. 
    Dim responseFromServer As String = reader.ReadToEnd() 

    ' Display the content. 
    Console.WriteLine(responseFromServer) 

    ' Clean up the streams. 
    reader.Close() 
    dataStream.Close() 
    response.Close() 

    ' Deserialize the json data 
    jss = New JavaScriptSerializer() 
    Dim jsonData = jss.Deserialize(Of Object)(responseFromServer) 

    ' List of key phrases to be returned 
    Dim phrases As New List(Of String) 
    For Each item As String In jsonData("documents")(0)("keyPhrases") 
     phrases.Add(item) 
    Next 

    Return phrases 

Мой вопрос, что я мог бы делать неправильно здесь, что я получаю сообщение, что мой документ превышает предельный размер 10240 байт, но это что данные, которые I POST находятся под этим ограничением.

+0

И ваш вопрос ... –

+0

Я вижу, что вы не указываете заголовок кодировки в HTTP-запросе. Вы пытались указать UTF-8? –

ответ

0

Как упомянуто выше в Assaf, обязательно укажите кодировку UTF-8.