python - 如何搜索音频文件中的内容?

标签 python amazon-web-services audio full-text-search speech-to-text

我有一个音频文件,我使用AWS transcribe从音频中获取文本。我现在有一个包含成绩单的json文件。 json文件还包含每个单词的开始时间和结束时间。例如 :
enter image description here
我想知道如何搜索完整的句子并返回所说的时间吗?我正在使用python来做到这一点。
谢谢您的帮助。

最佳答案

我会提取列表中的所有单词和时间,然后寻找句子的出现。
像这样的事情,如果我正确地获取了数据格式(总是使用第一个替代词作为提取词):

def extract_words_and_time(data):
    word_list = []
    time_list = []
    for item in data['items']:
        word_list.append(item['alternatives'][0]['content'].lower())
        time_list.append((item['start_time'], item['end_time']))
    return word_list, time_list

def get_sub_list_index(sub_list, complete_list):
    sublist_length = len(sub_list)
    for ind in (i for i, element in enumerate(complete_list) if element == sub_list[0]):
        if complete_list[ind:ind + sublist_length] == sub_list:
            return ind, ind + sublist_length - 1

def get_start_and_end_time(sentence, word_list):
    matching_start_stop = get_sub_list_index(sentence.lower().split(), word_list)
    if matching_start_stop:
        start_time = time_list[matching_start_stop[0]][0]
        end_time = time_list[matching_start_stop[1]][1]
        return start_time, end_time

word_list, time_list = extract_words_and_time(your_data_from_json)
sentence = 'Bonjour mon petit chien'
sentence_timing = get_start_and_end_time(sentence, word_list)

if sentence_timing:
    print(f'Start: {sentence_timing[0]}, Stop: {sentence_timing[1]}')
else:
    print('Sentence was not found')
无法真正测试它,理论上它应该可以工作;)

关于python - 如何搜索音频文件中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64711274/

相关文章:

iphone - objective-c/IOS : What's the simplest way to play an audio file backwards

ios - AVAudioRecorderDelegate 的代表没有调用

python - 如何根据列表中的项目请求输入?

python - 具有重新标准化颜色条的 Cartopy pcolormesh

python - 复杂转换为 Python Complex

python - 将 Pandas 中的时间序列重新采样为每周一次

c# - 是否有比使用 C# 的 AWSSDK.Iot 更好的包发布到 Aws Iot Core?

ios - 如何检测iphone是否处于静音模式

amazon-web-services - AWS ECS - 一种传递 secret 的免费方式

python - 向 AWS Lambda 发送带有正文的请求