【问题标题】:How to identify sentences in a paragraph which is convered from audio to text in python (speech-to-text)如何识别段落中从音频转换为python中文本的句子(语音到文本)
【发布时间】:2021-01-09 16:39:56
【问题描述】:

这是我的代码

将 Speech_recognition 导入为 sr 导入操作系统

def speech_to_text(speech_to_text_name):

#调用识别器() r = sr.Recognizer()

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# FILE_PATH = os.path.join(BASE_DIR, "noise_removed_lectures\\noise_removed_lectures_{}".format(speech_to_text_name))
FILE_PATH = os.path.join(BASE_DIR, "noise_removed_lectures\\{}".format(speech_to_text_name))
print('file path: ', FILE_PATH)
# DESTINATION_DIR = os.path.dirname(os.path.join(BASE_DIR, "LectureSummarizingApp\\speechToText\\{}.txt".format(speech_to_text_name)))
DESTINATION_DIR = os.path.join(BASE_DIR, "speechToText\\{}.txt".format(speech_to_text_name))
print('destination directory: ', DESTINATION_DIR)

with sr.AudioFile(FILE_PATH) as source:
    audio = r.listen(source)
    # file = open('audioToText01.txt', 'w') #open file
    file = open(DESTINATION_DIR, 'w') #open file
    try:
        text = r.recognize_google(audio) #Convert using google recognizer
        file.write(text)
    except:
        file.write('error')

    file.close()

我还需要将句子分开。我该怎么做??

【问题讨论】:

    标签: python speech-recognition speech-to-text google-speech-to-text-api


    【解决方案1】:

    您可以使用带有分隔符的split() 从您的字符串中创建句子列表。

    str = 'This is the first sentence. This is the second, and its a bit longer.'
    sentences = str.split('. ') # Split the string at every dot followed by a space
    
    print(sentences)
    
    >> ['This is the first sentence', 'This is the second, and its a bit longer.']
    

    【讨论】:

    • 但是当我将音频转换为文本时。使用 Google API,我不能使用它,因为没有 (.) 对吗??
    • @LinishaSiriwardana 如果您的文本中没有句子,那么您将无法将其拆分为句子。如果有这样的事情,也许您应该寻找用于检测句子的机器学习库 - 我想不出任何其他方法来做您想做的事情。
    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2014-07-14
    相关资源
    最近更新 更多