speech to text v1.0.1
Wed Oct 20 2021 21:55:27 GMT+0000 (Coordinated Universal Time)
Saved by @etanderson #python
''' Ethan Anderson Balance of Nature VtT v1.0.1 Oct. 20 2021 ''' # importing libraries import speech_recognition as sr import os from pydub import AudioSegment from pydub.silence import split_on_silence #create a speech recognition object r = sr.Recognizer() #change this to the folder you have your wav files VM_directory = r'vm temp wav' def send_to_rr(): print("Sending to R&R...") #put actual transfer process here def send_to_customer_care(): print("Sending to CC...") #put actual transfer process here def send_to_new_sales(): print("Sending to NS...") #put actual transfer process here def send_to_customer_sales(): print("Sending to CS...") #put actual transfer process here def send_to_returns(): print("Sending to Returns...") #put actual transfer process here def send_to_declines(): print("Sending to Declines...") #put actual transfer process here def send_to_scheduling(): print("Sending to Scheduling...") #put actual transfer process here def no_matches(): print("No Matches") #put actual transfer process here all_departments_words = [ { 'words' : ["tracking", "shipment", "pending", "shipped", "shipping", "where my order is", "where is my order", "delay", "delayed", "not received", "not recieve"], 'target' : send_to_rr, 'message' : "Sent to Reception and Routing" }, { 'words' : ["stop", "discontinue", "backlog", "surplus", "postpone", "no shipments"], 'target' : send_to_customer_care, 'message' : "Sent to Customer Care" }, { 'words' : ["fox","new customer", "cost", "discount code", "commercial", "do not have a computer", "don't have a computer", "35%", "35 percent", "Fox"], 'target' : send_to_new_sales, 'message' : "Sent to New Sales" }, { 'words' : ["reinstate", "old account", "ordered before", "place an order", "place another order", "renew", "change in my order", "reactivate", "reorder", "make an order", "apple", "preferred customer"], 'target' : send_to_customer_sales, 'message' : "Sent to Customer Sales" }, { 'words' : ["refund", "money back", "charged", "RMA", "R M A", "refunded", "refunding", "refused", "refuse"], 'target' : send_to_returns, 'message' : "Sent to Returns" }, { 'words' : ["fraud", "update", "declined", "decline", "account is disabled", "account has been disabled", "declines", "updated"], 'target' : send_to_declines, 'message' : "Sent to Declines" }, { 'words' : ["appointment", "scheduled", "schedule", "reschedule", "coach"], 'target' : send_to_scheduling, 'message' : "Sent to Scheduling" }, { 'words' : ["return", "returns", "returning"], 'target' : send_to_returns, 'message' : "Sent to Returns" }, { 'words' : ["cancel", "cancelled", "cancelling", "canceling"], 'target' : send_to_customer_care, 'message' : "Sent to Customer Care" }, { 'words' : ["ship"], 'target' : send_to_rr, 'message' : "Sent to Reception and Routing" }, { 'words' : [' ', '-'], 'target' : no_matches, 'message' : "No Matches" } ] ####################################### FUNCTIONS ########################################## # a function that splits the audio file into chunks # and applies speech recognition - edited to make it one giant chunk instead of multiple small ones (would want to be split into small ones for conversations) def get_large_audio_transcription(path): """ Splitting the large audio file into chunks and apply speech recognition on each of these chunks """ # open the audio file using pydub sound = AudioSegment.from_wav(path) # split audio sound where silence is 700 miliseconds or more and get chunks chunks = split_on_silence(sound, # experiment with this value for your target audio file min_silence_len = 1500, #change this if you want to modify for conversations, detects the silence length to find breaks # adjust this per requirement silence_thresh = sound.dBFS-14, # keep the silence for 1 second, adjustable as well keep_silence=500, ) folder_name = "audio-chunks" # create a directory to store the audio chunks if not os.path.isdir(folder_name): os.mkdir(folder_name) whole_text = "" # process each chunk for i, audio_chunk in enumerate(chunks, start=1): # export audio chunk and save it in # the `folder_name` directory. chunk_filename = os.path.join(folder_name, f"chunk{i}.wav") audio_chunk.export(chunk_filename, format="wav") # recognize the chunk with sr.AudioFile(chunk_filename) as source: audio_listened = r.record(source) # try converting it to text try: text = r.recognize_google(audio_listened) except sr.UnknownValueError as e: print("Error:", str(e)) else: text = f"{text.capitalize()}. " print(chunk_filename, ":", text) whole_text += text # return the text for all chunks detected return whole_text def main(): #checklist = [] for filename in os.scandir(VM_directory): #only running through .wav files if filename.path.endswith(".wav"): vm_text_string = get_large_audio_transcription(filename) #flags for if there are no keywords flag = 0 #loops through dictionaries for department in all_departments_words: if flag == 1: break #loops through keywords for word in department['words']: #loops words through VM and splits string into words if word in vm_text_string: #calls the function that will actually send the vm department['target']() #prints the "sent to ..." from dict print(department['message']) #checklist.append(department['message']) flag += 1 #ends loop to prevent repeats break #if no keywords are detected flag will stay 0 if flag == 0: #runs function for no matches no_matches() #checklist.append('No Matches') '''print(checklist) answers = ['Sent to Customer Care', 'Sent to Customer Care', 'Sent to Customer Care', 'Sent to Customer Care', 'Sent to Customer Sales', 'Sent to Customer Sales', 'No Matches', 'No Matches', 'No Matches', 'Sent to New Sales', 'Sent to New Sales', 'Sent to Returns', 'Sent to Returns', 'Sent to Reception and Routing', 'Sent to Reception and Routing', 'Sent to Reception and Routing', 'Sent to Reception and Routing', 'Sent to Scheduling', 'Sent to Scheduling'] i = -1 while i < 19: i += 1 if checklist[i] == answers[i]: print('correct') else: print('incorrect')''' main()
-fixed elif chains -added a list queue
Comments