Updated the comments and how rate is done
This commit is contained in:
40
dictate.py
40
dictate.py
@@ -6,31 +6,36 @@ import random
|
||||
|
||||
import maths
|
||||
|
||||
def words(config):
|
||||
engine = pyttsx3.init()
|
||||
words_list = []
|
||||
# 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 not 'mode_modifier' in config:
|
||||
config['mode_modifier'] = 20
|
||||
# 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))
|
||||
|
||||
def timed(config):
|
||||
engine = pyttsx3.init()
|
||||
words_list = []
|
||||
# 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 'mode_modifier' in config:
|
||||
if not 'time' in config:
|
||||
config['time'] = 30
|
||||
# For keeping track of time
|
||||
start = time.time()
|
||||
@@ -40,6 +45,7 @@ def timed(config):
|
||||
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
|
||||
@@ -48,27 +54,34 @@ def timed(config):
|
||||
if (now-start) > config['time']:
|
||||
break
|
||||
|
||||
def quote(config):
|
||||
engine = pyttsx3.init()
|
||||
# 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))
|
||||
|
||||
def file(config):
|
||||
engine = pyttsx3.init()
|
||||
# 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
|
||||
@@ -79,4 +92,5 @@ def file(config):
|
||||
for word in line.split(' '):
|
||||
engine.say(word)
|
||||
engine.runAndWait()
|
||||
# Break between words as determined by the wpm
|
||||
time.sleep(1/(config['wpm']/60))
|
||||
|
||||
13
main.py
13
main.py
@@ -6,6 +6,8 @@ import args # Handles command line arguments
|
||||
import maths
|
||||
import dictate
|
||||
|
||||
engine = pyttsx3.init()
|
||||
|
||||
config = {}
|
||||
with open('config.json', 'r') as file:
|
||||
config = json.load(file)
|
||||
@@ -15,13 +17,14 @@ config = args.handle_arguments(sys.argv, config)
|
||||
|
||||
dictation = ''
|
||||
|
||||
engine.setProperty('rate', config['wpm'])
|
||||
# TODO: match case
|
||||
match config['mode']:
|
||||
case 'word':
|
||||
dictate.words(config)
|
||||
case 'words':
|
||||
dictate.words(config, engine)
|
||||
case 'timed':
|
||||
dictate.timed(config)
|
||||
dictate.timed(config, engine)
|
||||
case 'quote':
|
||||
dictate.quote(config)
|
||||
dictate.quote(config, engine)
|
||||
case 'file':
|
||||
dictate.file(config)
|
||||
dictate.file(config, engine)
|
||||
|
||||
Reference in New Issue
Block a user