Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How to identify sentences in a paragraph which is convered from audio to text in python (speech-to-text)

This is my code

import speech_recognition as sr import os

def speech_to_text(speech_to_text_name):

#calling the Recognizer() 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()

I need to separate the sentences as well. how can I do that??

question from:https://stackoverflow.com/questions/65645066/how-to-identify-sentences-in-a-paragraph-which-is-convered-from-audio-to-text-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use split(), which takes a delimiter, to create a list of sentences from your string.

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.']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...