python - 如何识别段落中从音频转换为python中文本的句子(语音到文本)
问题描述
这是我的代码
将 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()
我也需要将句子分开。我怎样才能做到这一点??
解决方案
您可以使用带有分隔符的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.']
推荐阅读
- flutter - 扑。ShaderMask 无法从本地容器中获取对齐
- php - 我想将 html 苍蝇与 php 文件连接起来
- c# - 从浏览器中的地址行执行时获取 415 不受支持的媒体类型,提供 JSON 作为 .NET Core 3 中路由的参数
- python - 如果满足条件,嵌套列表中的简单元素减法?
- javascript - 如何在 json.message 对象中显示多个图像?
- chapel - 尝试使用 forall 循环对矩阵中的所有元素求和时遇到错误
- c# - 如何使用 lambda 缩短 if without else 语句?
- javascript - 如何将作为字符串导入的 png 注入 div 标签?
- python - groupby 之后的使用条件
- php - How to send commands using SOAP to AzerothCore worldserver console?