处理来自Google Speech的回复

问题描述

我有一个语音转文本应用程序,我在黑暗中徘徊了如何有效地处理响应并将其组织成转录。我像这样all_text = pool.map(transcribe,enumerate(files))来给转录函数提供45秒的数据块。这是我得到的答复:

all text:  [{'idx': 0,'text': ['users outnumber',' future'],'participant': 'str_MIC_Ct3G_con_O6qn4m00bs','file_index': 0,'words': [{'word': 'users','start_time': 0,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'},{'word': 'outnumber',{'word': 'future','start_time': 4,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'}]},{'idx': 1,'text': ["and the sustainable energy'],'file_index': 1,'words': [{'word': 'and','start_time': 45,{'word': 'the',{'word': 'sustainable',{'word': 'energy','start_time': 52,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'}]}]

因此,在这里,我从埃隆·马克斯(Elon Musks)的演讲中得到了两个45秒块。我削减了大部分响应以使其更短,但是如您所见,有两个块,索引为0和1。我想知道如何根据单词starting_time值从该响应中获取转录?在这里我只花了几秒钟,但是我当然也可以得到纳米。是否可以制作另一个列表来推送所有单词,然后使用starting_time对列表进行排序?这使我想到第二个问题:这有多有效?如果我终于有了一英里长的单词列表以及来自多个用户的其他信息,那么可能会出现一些问题吗?会有更好的方法吗?

编辑。这就是我尝试过的。它适用于较短的会话,但较长的会话会导致应用崩溃。我想知道这与列表太大有关吗?

words = []
clean_transcript = ''

for word in alternative.words:
    words.append({'word': word.word,'start_time': word.start_time.seconds,'participant': participant})

words.sort(key=lambda x: x['start_time'])
print('ALL WORDS: ',words)

for w in words:
    clean_transcript += w['word'] + ' '

print(clean_transcript)

有明显的“不要这样做”吗?

解决方法

首先,您应该尝试使用普通的 environment: # proper indentation to have PRISMA_CONFIG part of environment PRISMA_CONFIG: | port: 4466 databases: default: connector: mysql host: test.mysql.database.azure.com database: default@default user: rie@dak-prod # double $ to have a literal '$' password: ak123#$$ ssl: false rawAccess: true port: '3306' migrations: true 循环或嵌套的for循环。

for

结果:

text = [
    {'idx': 0,'text': ['users outnumber',' future'],'participant': 'str_MIC_Ct3G_con_O6qn4m00bs','file_index': 0,'words': [{'word': 'users','start_time': 0,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'},{'word': 'outnumber',{'word': 'future','start_time': 4,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'}]},{'idx': 1,'text': ['and the sustainable energy'],'file_index': 1,'words': [{'word': 'and','start_time': 45,{'word': 'the',{'word': 'sustainable',{'word': 'energy','start_time': 52,'participant': 'str_MIC_Ct3G_con_O6qn4m00bs'}]}
]

for item in text:
    print('---',item['idx'],'---')
    for word in item['words']:
        if word['start_time'] >= 45:
            print(word['start_time'],word['word'])

然后您可以尝试将其转换为列表理解。

--- 0 ---
--- 1 ---
45 and
45 the
45 sustainable
52 energy

结果

result = [[(word['start_time'],word['word'])  for word in item['words'] if word['start_time'] >= 45] for item in text]
print(result)

或者没有开始时间

[[],[(45,'and'),(45,'the'),'sustainable'),(52,'energy')]]

结果

result = [[word['word'] for word in item['words'] if word['start_time'] >= 45] for item in text]
print(result)

或者如果您要创建平面列表而不是子列表

[[],['and','the','sustainable','energy']]

结果

result = [word['word'] for item in text for word in item['words'] if word['start_time'] >= 45]
print(result)