Skip to content

Commit

Permalink
replaced textblob api with google api
Browse files Browse the repository at this point in the history
  • Loading branch information
PasaOpasen committed May 31, 2020
1 parent 02249e1 commit 5fcb5ae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions 4th/text_logger6.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
it`s text_logger4 with epytran transcriptions
"""

from textblob import TextBlob
#from googletrans import Translator
#from textblob import TextBlob
from googletrans import Translator
import speech_recognition as sr
import soundcard as sc
from scipy.io.wavfile import write
Expand All @@ -25,8 +25,10 @@

my_speaker = None
epis = {}
#translator = Translator()
translator = Translator()
bad_result_message = '!!! BAD RESULT OF RECOGNITION. U CAN TRY AGAIN'

print('...import is done...')


def print_on_blue(text, end='\n'):
Expand Down Expand Up @@ -142,7 +144,7 @@ def add2_print(langu, is_not_sup):
def speech_to_text_from_speaker(speaker,time = 3, samplerate = 48000, lang = 'ru-RU'):
"""time is the time in secs, samplerate is the frequency of signal"""
with speaker.recorder(samplerate=samplerate) as mic:
print_on_magenta(f'Listen (expected {lang})')
print_on_magenta(f'Listen (expected {lang}, {time} sec)')
time_count = int(time*samplerate)
dt = mic.record(time_count)
print_on_yellow('Okay. Wait')
Expand Down Expand Up @@ -170,7 +172,7 @@ def speech_to_text_from_wav(lang = 'ru', file = 'tmp.wav'):
return r.recognize_google(audio_text, language = lang)
except Exception as e:
print(e)
return 'bad result of recognition'
return bad_result_message

def speech_to_text_from_micro(lang = 'ru'):
r = sr.Recognizer()
Expand All @@ -192,7 +194,7 @@ def speech_to_text_from_micro(lang = 'ru'):
return r.recognize_google(audio_text, language = lang)
except Exception as e:
print(e)
return 'bad result of recognition'
return bad_result_message



Expand All @@ -203,18 +205,18 @@ def log_text(text, lang_of_text=None, lang_list = ['en','ru'], trans_list = [Tru
print(text)
return

blob = TextBlob(text)
#blob = TextBlob(text)
if lang_of_text == None:
lang_of_text = blob.detect_language()
#lang_of_text = translator.detect(text)
#lang_of_text = blob.detect_language()
lang_of_text = translator.detect(text).lang

bool_list = [r != lang_of_text for r in lang_list]

for lang, it, tc in zip(lang_list, bool_list, trans_list):
print(colored(f'\t {lang}:', color = 'cyan', attrs=['bold']), end=' ')
if it:
txt = str(blob.translate(from_lang = lang_of_text, to = lang))
#txt = translator.translate(text, dest = lang, src = lang_of_text)
#txt = str(blob.translate(from_lang = lang_of_text, to = lang))
txt = translator.translate(text, dest = lang, src = lang_of_text).text
print(txt)
else:
txt = text
Expand Down Expand Up @@ -271,6 +273,11 @@ def do_log_with_recognition(stop_word = '+', lang_list = ['en','ru','fa'], trans
try:
number = int(text)-1
text = speech_to_text_from_micro(lang_list[number])

if text.startswith('!'):
print_on_yellow(text)
continue

print_on_cyan('You said:',end='')
print_on_magenta(' '+text)
except Exception as e:
Expand Down Expand Up @@ -309,11 +316,17 @@ def do_log_with_recognition_both(speaker, listen_time = 3, stop_word = '+', lang
if text == stop_word:
break


# if text is like -1 (should listen)
if text[1:].isdigit():
try:
number = int(text[1:])-1
text = speech_to_text_from_speaker(speaker = my_speaker, lang=lang_list[number], time = listen_time)

if text.startswith('!'):
print_on_yellow(text)
continue

print(colored('You listened:',on_color='on_cyan'),end='')
print_on_magenta(' '+text)
except Exception as e:
Expand All @@ -325,6 +338,11 @@ def do_log_with_recognition_both(speaker, listen_time = 3, stop_word = '+', lang
try:
number = int(text)-1
text = speech_to_text_from_micro(lang_list[number])

if text.startswith('!'):
print_on_yellow(text)
continue

print_on_cyan('You said:',end='')
print_on_magenta(' '+text)
except Exception as e:
Expand All @@ -343,11 +361,6 @@ def do_log_with_recognition_both(speaker, listen_time = 3, stop_word = '+', lang
print_on_magenta("to stop it write",end=' ')
print_on_red(stop_word)

#do_log()





if __name__ == '__main__':

Expand Down
Binary file added 4th/tmp.wav
Binary file not shown.

0 comments on commit 5fcb5ae

Please sign in to comment.