vecchiemaniere
vecchiemaniere

Reputation: 43

Looking for a way for pydroid3 to pronounce unknown words instead of spelling them

I am trying to write Python code that is able to read txt files in pydroid3. In this file there are words not exist in the Italian vocabulary.

This is the structure of the txt:

LAGEN
VAMTU
OSBUL
...

I need the script to pronounce the words without spelling them.

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.slider import Slider
import os
import random
import time
import threading
import pyttsx3  # Libreria di sintesi vocale offline

class WordViewerApp(App):
    def build(self):
        # Ottieni il percorso assoluto del file usando os.path
        script_dir = os.path.dirname(os.path.abspath(__file__))  # Ottieni la directory del file corrente
        self.file_path = os.path.join(script_dir, 'punti.txt')  # Costruisci il percorso assoluto

        # Leggi le parole dal file
        self.words = read_words_from_file(self.file_path)
        random.shuffle(self.words)
        self.index = 0
        self.playing = False
        self.delay = 3
        self.is_muted = False

        # Inizializza il motore di sintesi vocale
        self.engine = pyttsx3.init()
        self.set_language('italian')  # Imposta la lingua italiana

        layout = BoxLayout(orientation='horizontal', padding=10, spacing=10)

        self.word_label = Label(text="", font_size='24sp', size_hint=(0.8, 1))
        layout.add_widget(self.word_label)
        
        self.play_button = Button(text="Play", on_press=self.start_auto_play)
        layout.add_widget(self.play_button)
        
        self.pause_button = Button(text="Pause", on_press=self.pause_auto_play)
        layout.add_widget(self.pause_button)

        self.next_button = Button(text="Next", on_press=self.next_word)
        layout.add_widget(self.next_button)

        self.prev_button = Button(text="Prev", on_press=self.prev_word)
        layout.add_widget(self.prev_button)

        self.volume_slider = Slider(min=0, max=100, value=100, size_hint=(0.5, 1))
        self.volume_slider.bind(value=self.set_volume)
        layout.add_widget(self.volume_slider)

        self.speed_slider = Slider(min=50, max=300, value=200, size_hint=(0.5, 1))
        self.speed_slider.bind(value=self.set_speed)
        layout.add_widget(self.speed_slider)

        self.mute_button = Button(text="Mute", on_press=self.toggle_mute)
        layout.add_widget(self.mute_button)

        self.show_word()

        return layout

    def set_language(self, language):
        """Imposta la voce in base alla lingua selezionata."""
        voices = self.engine.getProperty('voices')
        for voice in voices:
            if language in voice.languages or language in voice.name:
                self.engine.setProperty('voice', voice.id)
                break

    def show_word(self):
        if self.words:
            word = self.words[self.index]
            self.word_label.text = word
            if not self.is_muted:
                self.speak_word(word)
        else:
            self.word_label.text = "No words"

    def speak_word(self, word):
        """Pronuncia la parola corrente usando pyttsx3."""
        self.engine.say(word)
        self.engine.runAndWait()

    def next_word(self, instance):
        if self.words:
            self.index = (self.index + 1) % len(self.words)
            self.show_word()

    def prev_word(self, instance):
        if self.words:
            self.index = (self.index - 1) % len(self.words)
            self.show_word()

    def start_auto_play(self, instance):
        if not self.playing:
            self.playing = True
            self.auto_thread = threading.Thread(target=self.auto_play)
            self.auto_thread.start()

    def pause_auto_play(self, instance):
        self.playing = False
        if hasattr(self, 'auto_thread'):
            self.auto_thread.join()

    def auto_play(self):
        while self.playing:
            self.next_word(None)
            time.sleep(self.delay)

    def set_volume(self, instance, value):
        volume = value / 100
        self.engine.setProperty('volume', volume)

    def set_speed(self, instance, value):
        # Imposta la velocità di lettura
        self.engine.setProperty('rate', value)

    def toggle_mute(self, instance):
        self.is_muted = not self.is_muted
        self.mute_button.text = "Unmute" if self.is_muted else "Mute"
        if self.is_muted:
            self.engine.setProperty('volume', 0)
        else:
            self.set_volume(self.volume_slider.value)

def read_words_from_file(file_path):
    with open(file_path, 'r') as file:
        words = file.read().splitlines()
    return words

if __name__ == "__main__":
    WordViewerApp().run()

The code runs, but it keeps spelling them.

Upvotes: 0

Views: 24

Answers (0)

Related Questions