Beispiel #1
0
def translate_text(text, target_language):
    if len(text) > 10000:
        print('Error: Text too large. Maximum: 10000 characters')
        sys.exit()
    try:
        client = translate.Client()
    except:
        credentials()
        sys.exit()
    try:
        confidence = client.detect_language(text)
        result = client.translate(text, target_language)
        if opt_c == True:
            print('Detected language confidence: '),
            print('{:.2%}'.format(confidence['confidence']))  # convert to %
        if opt_b != True:
            if result['detectedSourceLanguage'] != target_language:
                print_language_name(result['detectedSourceLanguage'])
                print(result['input'])
            print_language_name(target_language)
        print(decode(result['translatedText'])).encode('utf-8')
        if opt_s == True:
            text_to_speech(result['translatedText'], target_language)
    except Exception as e:
        print('Error: '),
        print(e)
        sys.exit()
Beispiel #2
0
def run():
    try:
        system.play("listen") #воспроизведение предупреждения о начале записи
        system.record("record") #запись голосовой команды
        text = speech.speech_to_text("record") #преобразование голосовой записи в текстовую строку
        answer = chat.get_answer(text) #получение ответа из преобразованной фразы
        if answer != "":
            speech.text_to_speech(answer, "temp") #синтез ответа в голосовую запись
            system.convert("temp") #конвертация ответа в необходимый формат
            system.play("temp") #воспроизведение ответа через динамики
    except Exception:
        system.play("error") #обработка ошибки и воспроизведение сообщения об ошибке
Beispiel #3
0
 def post(self):
     request = self.request.body.decode("utf-8")
     print(request)
     stuff = parse_text(request)
     text_to_speech(stuff[0])
     print(stuff)
     if stuff[1] is not None:
         with open("Data/foods.json") as r:
             newarr = ["Order", stuff[1][1], "entree", "$2.00"]
             for num in range(stuff[1][2]):
                 items.append(newarr)
     self.redirect('/')
Beispiel #4
0
def getSound(word):
    res = {"word": word}
    res["audio"] = base64.b64encode(text_to_speech(word))
    # data = base64.b64decode(res["audio"])
    # with open('output1.wav', 'wb') as out:
    #     # Write the response to the output file.
    #     out.write(data)
    #     print('Audio content written to file "output1.wav"')
    return json.dumps(res)
Beispiel #5
0
    def ask_question(self):
        # Check if need to stop
        if self.ques_count == self.max_num_of_questions:
            self.end_session()

        self.ques_count += 1
        # Display question
        ######
        print(self.question['string'])

        # Speak question
        speech.text_to_speech(self.question['string'], '{}{}{}.mp3'.format(\
            self.question['string'][:4], self.question['num1'], self.question['num2']))

        # Update pending answer
        self.pending_answer = True

        # Return to event manager
        self.event_manager()
Beispiel #6
0
def call_convert():
    import speech
    a = variables.output_txt.get("1.0", 'end-1c')
    speech.text_to_speech(a)