Files
aladeen/dictate.py
2026-03-30 01:11:08 +01:00

97 lines
2.9 KiB
Python

import pyttsx3
import time
import json
import time
import random
import maths
# WORDS #
def words(config, engine):
# Open the words list
with open(f'languages/{config['language']}.json', 'r') as file:
dump = json.load(file)
words_list = dump['words']
# Dictation is stored in an array
dictation = []
# If the number of words is undefined, set the default value to 20
if not 'words' in config:
config['words'] = 20
# Pre-generate the list of words
for i in range(0, config['words']):
word = words_list[maths.biased_random(len(words_list)-1)]
dictation.append(word)
print(word)
# Dictate this generated list of words
for word in dictation:
engine.say(word)
engine.runAndWait()
# Break between words as determined by the wpm
time.sleep(1/(config['wpm']/60))
# TIMED #
def timed(config, engine):
# Open the words list
with open(f'languages/{config['language']}.json', 'r') as file:
dump = json.load(file)
words_list = dump['words']
if not 'time' in config:
config['time'] = 30
# For keeping track of time
start = time.time()
# The words are generated on-demand
while 1==1:
word = words_list[maths.biased_random(len(words_list)-1)]
engine.say(word)
print(f'{word} ')
engine.runAndWait()
# Break between words as determined by the wpm
time.sleep(1/(config['wpm']/60))
# check if too much time has elapsed for mode: time
now = time.time()
# Break condition
if (now-start) > config['time']:
break
# QUOTE #
def quote(config, engine):
# Fetch the lists of quotes
with open(f'quotes/{config['language']}.json', 'r') as file:
quotes = json.load(file)
# If a quote ID is specified, use that quote
if 'quoteid' in config:
quote = quotes[config['quoteid']]
# Otherwise, generate a random number for the quote ID
else:
x = random.randint(0, len(quotes)-1)
quote = quotes[x]
print(f'{quote['quote']}\n\t\t\t - {quote['author']}')
dictation = quote['quote'].split(' ')
# Dictate the quote
for word in dictation:
engine.say(word)
engine.runAndWait()
# Break between words as determined by the wpm
time.sleep(1/(config['wpm']/60))
# FILE #
def file(config, engine):
# Check the filename is definied
if not 'filename' in config:
print('Filename required, use -f <filename>')
exit()
# Loop over the file, outputting it
with open(f'{config['filename']}', 'r') as file:
lines = file.readlines()
# Text output
for line in lines:
print(line)
# Dictation loop
for line in lines:
for word in line.split(' '):
engine.say(word)
engine.runAndWait()
# Break between words as determined by the wpm
time.sleep(1/(config['wpm']/60))