2

我目前正在使用 Microsoft Azure 从实时语音识别中获取转录文本。使用该转录文本,我将其放入 TextRank 以从该语音流中提取关键字。但是,当我运行它时,我在运行 TextRank 代码时会丢失很多语音识别。有没有办法在将转录结果传递给下一个过程的同时不断运行语音识别,同时处理TextRank关键字提取,这样我就不会丢失任何语音并提取关键字?

def from_mic():
    speech_config = speechsdk.SpeechConfig(subscription="", region="")
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

    # print("Speak into your microphone.")
    result = speech_recognizer.recognize_once_async().get()
    print(result.text)
    return result.text

for i in range(1,10):
    transcript = from_mic()
    summa_keywords = summa_keyword_extractor.keywords(transcript, ratio=1.0)
    print(summa_keywords)
4

1 回答 1

1

您需要设置两个并行进程,但与任务队列相互关联。

这是因为您依赖于记录器进程的提取。

这是实现这一目标的一种方法的尝试(显然它没有经过完善,可以进一步改进):

def recorder_process(recorder_queue, extractor_queue):
  speech_config = speechsdk.SpeechConfig(subscription="", region="")
  speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

  while True:
    request = recorder_queue.get()
    result = speech_recognizer.recognize_once_async().get()
    extractor_queue.put(result.text)

def extractor_process(extractor_queue, results_queue):
  while True:
    transcript = extractor_queue.get()
    summa_keywords = summa_keyword_extractor.keywords(transcript, ratio=1.0)
    results_queue.put({'transcript': transcript, 'keywords': summa_keywords})

if __name__ == "__main__":
    # Connect to remote host over TCP
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((HOST,PORT))

    # Set up a Queue to pass data to the update process, and another one
    # for the two children to communicate
    recorder_queue = Queue()
    extractor_queue = Queue()
    results_queue = Queue()

    # Create two child processes, pass a reference to the Queue to each
    recorder = Process(target=recorder_process, args=(recorder_queue, extractor_queue))
    extractor = Process(target=extractor_process, args=(extractor_queue, results_queue))

    recorder.start()
    extractor.start()

    index = 0
    while True:
      recorder_queue.put(index)
      index += 1
      sleep(1)

    recorder.join()
    extractor.join()
于 2020-11-12T12:10:20.590 回答