简体   繁体   English

Azure 语音到带数字的文本

[英]Azure speech to text with numbers

A use case for my app is to convert speech (single word utterances) to text.我的应用程序的一个用例是将语音(单字话语)转换为文本。 I need to use Azure speech to text for this.为此,我需要使用 Azure 语音来发送文本。 Sometimes the speech needs to be converted into an integer - I need to submit the response as a quantity for example.有时需要将语音转换为整数 - 例如,我需要将响应作为数量提交。 My question is is there anyway, via the REST API, to tell the speech to text service I want a numeric result?我的问题是无论如何,通过 REST API 将语音告诉文本服务我想要一个数字结果吗? Currently it is returning things like 'one' instead of '1' and 'free' instead of '3'.目前它正在返回诸如“一”而不是“1”和“免费”而不是“3”之类的东西。 I don't think there is a way to do this from the documentation but I wanted to see if anyone else has solved this problem before I think of a way around it.我不认为有办法从文档中做到这一点,但我想看看是否有其他人解决了这个问题,然后再想办法解决它。 This is the code I am using in my proof of concept project:这是我在概念验证项目中使用的代码:

 public static async Task SpeechToTextAsync(MemoryStream data, ISpeechResultCallback callBack)
    {
        string accessToken = await Authentication.GetAccessToken();

        IToast toastWrapper = DependencyService.Get<IToast>();

        if (accessToken != null)
        {
            toastWrapper.Show("Acquired token");
            callBack.SpeechReturned("Acquired token");
            using (var client = new HttpClient())
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-GB&format=detailed");

                request.SendChunked = true;
                request.Accept = @"application/json;text/xml";
                request.Method = "POST";
                request.ProtocolVersion = HttpVersion.Version11;
                request.Host = "westus.stt.speech.microsoft.com";
                request.ContentType = @"audio/wav; codecs=audio/pcm; samplerate=16000";
                // request.Headers["Ocp-Apim-Subscription-Key"] = Program.SubscriptionKey;
                request.Headers.Add("Authorization", "Bearer " + accessToken);
                request.AllowWriteStreamBuffering = false;

                data.Position = 0;
                byte[] buffer = null;
                int bytesRead = 0;
                using (Stream requestStream = request.GetRequestStream())
                {

                    buffer = new Byte[checked((uint)Math.Min(1024, (int)data.Length))];
                    while ((bytesRead = data.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        requestStream.Write(buffer, 0, bytesRead);
                    }

                    // Flush
                    requestStream.Flush();
                }

                try
                {
                    string responseData = null;
                    using (WebResponse response = request.GetResponse())
                    {
                        var encoding = Encoding.GetEncoding(((HttpWebResponse)response).CharacterSet);

                        using (var responseStream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(responseStream, encoding))
                            {
                                responseData = reader.ReadToEnd();

                                AzureSTTResults deserializedProduct = JsonConvert.DeserializeObject<AzureSTTResults>(responseData);

                                if(deserializedProduct == null || deserializedProduct.NBest == null || deserializedProduct.NBest.Length == 0)
                                {
                                    toastWrapper.Show("No results");
                                    callBack.SpeechReturned("No results");
                                }
                                else
                                {
                                    toastWrapper.Show(deserializedProduct.NBest[0].ITN);
                                    callBack.SpeechReturned(deserializedProduct.NBest[0].ITN);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    toastWrapper.Show(ex.Message);
                    callBack.SpeechReturned(ex.Message);
                }

            }
        }
        else
        {
            toastWrapper.Show("No token required");
            callBack.SpeechReturned("No token required");
        }
    }

And here is an example of the result that I would like to be '1':这是我希望为“1”的结果示例:

{
 "RecognitionStatus": "Success",
  "Offset": 0,
  "Duration": 22200000,
  "NBest": [
    {
      "Confidence": 0.43084684014320374,
      "Lexical": "one",
      "ITN": "One",
      "MaskedITN": "One",
      "Display": "One."
    }
  ]
}

I suggest to use this nuget from Microsoft.我建议使用 Microsoft 的这个nuget It works like a charm, here an example.它就像一个魅力, 这里有一个例子。

NumberRecognizer.RecognizeNumber("I have two apples", Culture.English)

According to the offical document Speech-to-text REST API , there is no option can help converting the numberic words to numbers.根据官方文档Speech-to-text REST API ,没有选项可以帮助将数字单词转换为数字。

Considering for the numberic words in English have the pattern in syntax, you can use a simple algorithm to implement the feature for converting words to numbers.考虑到英文中的数字词在句法上有规律,可以用一个简单的算法来实现单词转数字的功能。 As references, you can follow these below to write your own one in C# by yourself.作为参考,你可以按照下面的这些来自己用 C# 写一个。

  1. Converting words to numbers in c++ c++ 将单词转换为数字
  2. Translate (Convert) Words to Numbers RRS feed in SQL Server在 SQL Server 中将单词翻译(转换)为数字 RRS 提要
  3. Words in Numbers数字中的单词

Hope it helps.希望能帮助到你。

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

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