C#,不知道如何使用这个 Json 反序列化

发布于 2025-01-18 16:43:58 字数 1345 浏览 4 评论 0原文

总而言之,我正在尝试将语音转文本调用的结果传递给 IBM 的 Watson,并将最终转录本身转换为纯文本。转录的输出最终会是这种格式的一团 Json,

{
  "result_index": 0,
  "results": [
    {
      "final": true,
      "alternatives": [
        {
          "transcript": "%HESITATION %HESITATION %HESITATION %HESITATION %HESITATION it is probably is actual words okay what's my best ",
          "confidence": 0.74
        }
      ]
    }
  ]
}

当通过 JSON 到 C# 转换器运行时,会导致类似这样的结果

    public class Alternative
    {
        public string transcript { get; set; }
        public double confidence { get; set; }
    }

    public class Result
    {
        public bool final { get; set; }
        public List<Alternative> alternatives { get; set; }
    }

    public class Root
    {
        public int result_index { get; set; }
        public List<Result> results { get; set; }
    }

最终,我只想在替代下获取该转录并将其发送到文本框,但是我一生都无法做到这一点,这就是我所能得到的,不知道从这里到哪里去得到那个成绩单。

                var bAudio = File.ReadAllBytes(file);
                var memAudio = new MemoryStream(bAudio);
                var results1 = speechToText.Recognize(memAudio, model: "en-US_NarrowbandModel");
                Root resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response); 

我最终得到 0 输出。我似乎无法准确理解这是如何将 JSON 分解为不同的组件

Long and short, I'm trying to take the results from a speech to text call to IBM's Watson, and get the final transcription itself into plain text. The output of the transcription ends up being a mess of Json in this format,

{
  "result_index": 0,
  "results": [
    {
      "final": true,
      "alternatives": [
        {
          "transcript": "%HESITATION %HESITATION %HESITATION %HESITATION %HESITATION it is probably is actual words okay what's my best ",
          "confidence": 0.74
        }
      ]
    }
  ]
}

which when run through a JSON to C# Converter, leads to something like this

    public class Alternative
    {
        public string transcript { get; set; }
        public double confidence { get; set; }
    }

    public class Result
    {
        public bool final { get; set; }
        public List<Alternative> alternatives { get; set; }
    }

    public class Root
    {
        public int result_index { get; set; }
        public List<Result> results { get; set; }
    }

Ultimately, I just want to get to that transcript under alternative and send it to a text box, however I cannot for the life of me get that working, here's about as far as I get, with no idea on where to go from here to get to that transcript.

                var bAudio = File.ReadAllBytes(file);
                var memAudio = new MemoryStream(bAudio);
                var results1 = speechToText.Recognize(memAudio, model: "en-US_NarrowbandModel");
                Root resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response); 

I ultimately get 0 output. I can't seem to comprehend exactly how this is breaking down the JSON into it's various components

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2025-01-25 16:43:58

您不必让 C# 打破其自身的命名约定并遵循 JSON:

public class Alternative
{
    [JsonProperty("transcript")]
    public string Transcript { get; set; }
    [JsonProperty("confidence")]
    public double Confidence { get; set; }
}

public class Result
{
    [JsonProperty("final")]
    public bool Final { get; set; }

    [JsonProperty("alternatives")]
    public List<Alternative> Alternatives { get; set; }
}

public class Root
{
    [JsonProperty("result_index")]
    public int ResultIndex { get; set; }
    [JsonProperty("results")]
    public List<Result> Results { get; set; }
}

使用更复杂的网站,例如 https:// QuickType.iO(无隶属关系)来转换您的 JSON,它将为您填写这些属性,这些属性为 json 指定一个名称,为 csharp 指定另一个名称。

您还可以从文件进行流式传输,这将节省内存;不需要将文件加载到内存流中只是为了获取流(好吧,除非 Recognize 确实采用 MemoryStream;大多数基于流的东西都采用简单的 Stream,因此它们可以从提供数据流的任何内容中获取;内存、文件、网络):

using var audioStream = File.OpenRead(file);
var results1 = speechToText.Recognize(audioStream, model: "en-US_NarrowbandModel");
 var resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response);

然后您可以使用普通索引或 linq 样式访问:

resultsTranscription.Results[0].Alternatives[0].Transcript;

resultsTranscription.Results.First().Alternatives.FirstOrDefault(a => a.Confidence > 0.7)?.Transcript;

例如,后一种形式获取置信度高于 70% 的第一个措辞。

You don't have to make your C# break its own naming conventions and follow the JSON:

public class Alternative
{
    [JsonProperty("transcript")]
    public string Transcript { get; set; }
    [JsonProperty("confidence")]
    public double Confidence { get; set; }
}

public class Result
{
    [JsonProperty("final")]
    public bool Final { get; set; }

    [JsonProperty("alternatives")]
    public List<Alternative> Alternatives { get; set; }
}

public class Root
{
    [JsonProperty("result_index")]
    public int ResultIndex { get; set; }
    [JsonProperty("results")]
    public List<Result> Results { get; set; }
}

Use a more sophisticated website, like https://QuickType.iO (no affiliation) to convert your JSON and it will fill in for you these properties that specify one name for the json and another for the csharp

You can also stream from a file, which would save memory; there's no need to load a file into a memory stream just to get a stream (well, unless Recognize really does take a MemoryStream; most stream based things take a simple Stream so they can be sourced from anything that's provided a stream of data; memory, file, network):

using var audioStream = File.OpenRead(file);
var results1 = speechToText.Recognize(audioStream, model: "en-US_NarrowbandModel");
 var resultsTranscription = JsonConvert.DeserializeObject<Root>(results1.Response);

Then you can use normal indexed or linq style access:

resultsTranscription.Results[0].Alternatives[0].Transcript;

resultsTranscription.Results.First().Alternatives.FirstOrDefault(a => a.Confidence > 0.7)?.Transcript;

The latter form gets the first wording whose confidence is above 70%, for example..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文