【发布时间】:2021-07-12 09:00:31
【问题描述】:
我正在编写一个使用 IBM Watson Speech to Text API 的自动化脚本。检查下面的代码(我使用了 IBM 的 Speech to Text 文档)。
require "ibm_watson/authenticators"
require "ibm_watson/speech_to_text_v1"
include IBMWatson
authenticator = Authenticators::IamAuthenticator.new(
apikey: "{apikey}"
)
speech_to_text = SpeechToTextV1.new(
authenticator: authenticator
)
speech_to_text.service_url = "{url}"
speech_to_text.configure_http_client(disable_ssl_verification: true)
File.open("audio-file.flac") do |audio_file|
speech_recognition_results = speech_to_text.recognize(
audio: audio_file,
content_type: "audio/flac",
word_alternatives_threshold: 0.9
)
transcript_json = JSON.pretty_generate(speech_recognition_results.result)
puts transcript_json
end
每次我在终端上运行命令 ruby test.rb 时,它都会以 JSON 格式为我提供转录结果。
LaboteamnoMacBook-puro:GoogleDriveApiTest laboteam$ ruby test.rb
{
"result_index": 0,
"results": [
{
"final": true,
"alternatives": [
{
"transcript": "several tornadoes touched down as a line of severe thunderstorms swept through Colorado on Sunday ",
"confidence": 0.94
}
],
....
}
我想parse 它,但我不知道怎么做,因为有很多子键值对。我只想在alternatives 键中获取transcript 键的值。希望得到一些关于如何解析这个 JSON 的回应。
【问题讨论】:
-
试试
puts transcript_json["results"]["alternatives"].first["transcript"]:)
标签: json ruby parsing speech-to-text