gpt4 book ai didi

python - 如何根据 JSON 文件进行特定计算

转载 作者:行者123 更新时间:2023-12-01 06:27:44 25 4
gpt4 key购买 nike

下面您可以找到一个 json 文件。该文件包含演讲结果。

当前情况:

您可以看到包含所有文本的“CombinedResults”。在我的例子中:

hey good morning this is X from am i'm working as a employee and this is just an audio file to test"

所需情况

我不仅想看到整个文本,还想看到对话中几秒钟的沉默。

hey good morning [seconds: 3.1] this is X from am i'm working as a employee and this is just an audio file to test"

如何解决这个问题?

有可能获得所需的输出。我认为我需要做什么:

  1. 循环“SegmentResults”并保存A)。 “偏移秒数”,B).“持续时间”,C)。 “NBest”->“Lexical”(包含文本)。

  2. 之后,这个计算可以帮助我们进行“秒”计算:

('OffsetInSeconds' + 'DurationInSeconds') of the previous 'SegmentResult' - ('OffsetInSeconds') of the current 'SegmentResult'

在此示例中:0.57 + 1.67 = 2.24 和 5.34 = 3.1 秒之间的差值。

我尝试编写这个代码,但我有点卡住了。为什么?因为我可以计算当前段(我现在正在循环的位置)的“OffsetInSeconds”+“DurationInSeconds”。然而,我怀念之前的计算。在所需的输出中获取所有文本也是一个问题。

希望有人能帮忙。预先非常感谢!

# Focus on the SegmentResults 
All_result_list = Test_JSON["AudioFileResults"][0].get("SegmentResults")

# Loop over the SegmentResults, to get each Segment separately

for speech_result in All_result_list:
print(type(speech_result))
#print(speech_result)
Pause_Time = 0
Off_Set_Seconds = speech_result.get('OffsetInSeconds')
Duration_Seconds = speech_result.get('DurationInSeconds')

print(Off_Set_Seconds)
print(Duration_Seconds)
Text_All = speech_result.get('NBest')

for correct_text in Text_All:
Text = correct_text.get('Lexical')
print(Text)

请在此处找到 json 文件:

Test_JSON = {
"AudioFileResults": [
{
"AudioFileName": "Channel.0.wav",
"AudioFileUrl": null,
"AudioLengthInSeconds": 29.76,
"CombinedResults": [
{
"ChannelNumber": null,
"Lexical": "hey good morning this is X from am i'm working as a employee and this is just an audio file to test",
"ITN": "hey good morning this is X from am i'm working as a employee and this is just an audio file to test",
"MaskedITN": "Hey good morning This is X from am I'm working as a employee and this is just an audio file to test",
"Display": "Hey good morning. This is X from am I'm working as a employee and this is just an audio file to test"
}
],
"SegmentResults": [
{
"RecognitionStatus": "Success",
"ChannelNumber": null,
"SpeakerId": null,
"Offset": 5700000,
"Duration": 16700000,
"OffsetInSeconds": 0.57,
"DurationInSeconds": 1.67,
"NBest": [
{
"Confidence": 0.9073331,
"Lexical": "hey good morning",
"ITN": "hey good morning",
"MaskedITN": "Hey good morning",
"Display": "Hey good morning.",
"Sentiment": null,
"Words": null
}
]
},
{
"RecognitionStatus": "Success",
"ChannelNumber": null,
"SpeakerId": null,
"Offset": 53400000,
"Duration": 66700000,
"OffsetInSeconds": 5.34,
"DurationInSeconds": 6.67,
"NBest": [
{
"Confidence": 0.8709568,
"Lexical": "this is X from am i'm working as a employee and this is just an audio file to test",
"ITN": "this is X from am i'm working as a employee and this is just an audio file to test",
"MaskedITN": "This is X from am I'm working as a employee and this is just an audio file to test",
"Display": "This is X from am I'm working as a employee and this is just an audio file to test.",
"Sentiment": null,
"Words": null
}
]
}
]
}
]
}

最佳答案

看来您只需要跟踪以前的偏移量(以秒为单位)+持续时间(以秒为单位)。

previous_time = None

for speech_result in All_result_list:
print(type(speech_result))

Off_Set_Seconds = speech_result.get('OffsetInSeconds')
Duration_Seconds = speech_result.get('DurationInSeconds')

if previous_time:
Pause_Time = Off_Set_Seconds - previous_time
print(f'[seconds: {Pause_Time}]')

previous_time = Off_Set_Seconds + Duration_Seconds

Text_All = speech_result.get('NBest')

也许这是一个间距问题,但为什么要在此处嵌套循环?您的 JSON 示例中的列表中似乎只有一项。如果是这样,只需调用 [0] 元素即可。

    for correct_text in Text_All:
Text = correct_text.get('Lexical')
print(Text)

关于python - 如何根据 JSON 文件进行特定计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60055480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com