简体   繁体   English

沃森语音对文本:响应消息太长-中断

[英]Watson Speech to Text: Response Message too Long - cut off

I have a fully-functional client for IBM Watson Speech To Text. 我有IBM Watson Speech To Text的全功能客户端。 I wanted to start recording more metadata (word confidence, start/end times, etc.) so I added the appropriate fields to the my initial request. 我想开始记录更多的元数据(词的置信度,开始/结束时间等),因此我在初始请求中添加了适当的字段。

Everything worked smoothly until I encountered an exception deserializing the Watson text message response into a JSON object. 一切工作顺利,直到遇到将Watson文本消息响应反序列化为JSON对象的异常。 When I printed the string, this was the result. 当我打印字符串时,这就是结果。 Notice that it is cut off, which explains the error deserializing: 请注意,它已被切断,这说明了反序列化错误:

{
   "results": [
      {
         "word_alternatives": [
            {
               "start_time": 3.71,
               "alternatives": [
                  {
                     "confidence": 1.0,
                     "word": "Hey"
                  }
               ],
               "end_time": 3.97
            },
            {
               "start_time": 3.97,
               "alternatives": [
                  {
                     "confidence": 1.0,
                     "word": "what's"
                  }
               ],
               "end_time": 4.54
            },
            {
               "start_time": 4.54,
               "alternatives": [
                  {
                     "confidence": 1.0,

It appears that I am asking for too much information. 看来我要求的信息太多。 The System.Net.WebSockets.WebSocketReceiveResult object returned from ClientWebSocket.ReceiveAsync() has a property result.Count describing the number of bytes of information sent. 所述System.Net.WebSockets.WebSocketReceiveResult从返回的对象ClientWebSocket.ReceiveAsync()具有属性result.Count描述发送的信息的字节数。 In debugging I found that result.Count = 1024. 在调试中,我发现了result.Count = 1024.

My questions are these: 我的问题是:

1 - Is the 1kB limit imposed by Watson or is that a limitation of the .NET WebSocket library? 1-Watson施加了1kB限制还是.NET WebSocket库的限制?

2 - How can I lift that limitation to receive the full message? 2-如何解除该限制以接收完整消息?

Edit: Minimal example 编辑:最小示例

There is a lot of code that touches this issue, but hopefully this give enough context to help: 有很多代码可以解决这个问题,但是希望可以提供足够的上下文来帮助您:

    // Set up connection
    ClientWebSocket socket = new ClientWebSocket();
    // Works: 
    //string headerInfo = "{ \"content-type\":\"audio/l16;rate=8000\",\"interim_results\":true,\"smart_formatting\":true,\"timestamps\":false,\"inactivity_timeout\":-1,\"word_confidence\":false,\"profanity_filter\":false,\"action\":\"start\"}";
    // Doesn't:
    string headerInfo = "{ \"content-type\":\"audio/l16;rate=8000\",\"interim_results\":true,\"smart_formatting\":true,\"timestamps\":true,\"inactivity_timeout\":-1,\"word_confidence\":true,\"profanity_filter\":false,\"action\":\"start\"}";
    var startMsg = new ArraySegment<byte>(Encoding.UTF8.GetBytes(headerInfo));
    var endOfMsg = true;
    await socket.SendAsync(startMsg, WebSocketMessageType.Text, endOfMsg, default(CancellationToken));

    // Send Audio bytes

    // Receive response
    var msgBuffer = new byte[8 * 1024];
    var receiver = new ArraySegment<byte>(msgBuffer);
    var result = await socket.ReceiveAsync(receiver, CancellationToken.None);
    var message = Encoding.UTF8.GetString(receiver.Array.Take(result.Count).ToArray());
    var result = JsonConvert.DeserializeObject<ResultsObject>(watsonMsg);

ResultsObject is a local type to deserialize into. ResultsObject是要反序列化为的本地类型。

It turned out that a message was being sent in several chunks. 原来,一条消息正在分几块发送。 The solution was this fix: 解决方法是此修复程序:

var ResultMsg = new List<byte>();
if (receiver.Array.Length > 0)
{
    ResultMsg.AddRange(receiver.Array.Take(result.Count));
}
if (result.EndOfMessage)
{
    var msgBytes = ResultMsg.ToArray();
    var message = Encoding.UTF8.GetString(msgBytes);
    TextMessageHandler(message);
    ResultMsg.Clear();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM