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
|
import maths
|
||||||
|
|
||||||
def words(config):
|
# WORDS #
|
||||||
engine = pyttsx3.init()
|
def words(config, engine):
|
||||||
words_list = []
|
# Open the words list
|
||||||
with open(f'languages/{config['language']}.json', 'r') as file:
|
with open(f'languages/{config['language']}.json', 'r') as file:
|
||||||
dump = json.load(file)
|
dump = json.load(file)
|
||||||
words_list = dump['words']
|
words_list = dump['words']
|
||||||
|
# Dictation is stored in an array
|
||||||
dictation = []
|
dictation = []
|
||||||
if not 'mode_modifier' in config:
|
# If the number of words is undefined, set the default value to 20
|
||||||
config['mode_modifier'] = 20
|
if not 'words' in config:
|
||||||
|
config['words'] = 20
|
||||||
|
# Pre-generate the list of words
|
||||||
for i in range(0, config['words']):
|
for i in range(0, config['words']):
|
||||||
word = words_list[maths.biased_random(len(words_list)-1)]
|
word = words_list[maths.biased_random(len(words_list)-1)]
|
||||||
dictation.append(word)
|
dictation.append(word)
|
||||||
print(word)
|
print(word)
|
||||||
|
# Dictate this generated list of words
|
||||||
for word in dictation:
|
for word in dictation:
|
||||||
engine.say(word)
|
engine.say(word)
|
||||||
engine.runAndWait()
|
engine.runAndWait()
|
||||||
|
# Break between words as determined by the wpm
|
||||||
time.sleep(1/(config['wpm']/60))
|
time.sleep(1/(config['wpm']/60))
|
||||||
|
|
||||||
def timed(config):
|
# TIMED #
|
||||||
engine = pyttsx3.init()
|
def timed(config, engine):
|
||||||
words_list = []
|
# Open the words list
|
||||||
with open(f'languages/{config['language']}.json', 'r') as file:
|
with open(f'languages/{config['language']}.json', 'r') as file:
|
||||||
dump = json.load(file)
|
dump = json.load(file)
|
||||||
words_list = dump['words']
|
words_list = dump['words']
|
||||||
if not 'mode_modifier' in config:
|
if not 'time' in config:
|
||||||
config['time'] = 30
|
config['time'] = 30
|
||||||
# For keeping track of time
|
# For keeping track of time
|
||||||
start = time.time()
|
start = time.time()
|
||||||
@@ -40,6 +45,7 @@ def timed(config):
|
|||||||
engine.say(word)
|
engine.say(word)
|
||||||
print(f'{word} ')
|
print(f'{word} ')
|
||||||
engine.runAndWait()
|
engine.runAndWait()
|
||||||
|
# Break between words as determined by the wpm
|
||||||
time.sleep(1/(config['wpm']/60))
|
time.sleep(1/(config['wpm']/60))
|
||||||
|
|
||||||
# check if too much time has elapsed for mode: time
|
# check if too much time has elapsed for mode: time
|
||||||
@@ -48,27 +54,34 @@ def timed(config):
|
|||||||
if (now-start) > config['time']:
|
if (now-start) > config['time']:
|
||||||
break
|
break
|
||||||
|
|
||||||
def quote(config):
|
# QUOTE #
|
||||||
engine = pyttsx3.init()
|
def quote(config, engine):
|
||||||
|
# Fetch the lists of quotes
|
||||||
with open(f'quotes/{config['language']}.json', 'r') as file:
|
with open(f'quotes/{config['language']}.json', 'r') as file:
|
||||||
quotes = json.load(file)
|
quotes = json.load(file)
|
||||||
|
# If a quote ID is specified, use that quote
|
||||||
if 'quoteid' in config:
|
if 'quoteid' in config:
|
||||||
quote = quotes[config['quoteid']]
|
quote = quotes[config['quoteid']]
|
||||||
|
# Otherwise, generate a random number for the quote ID
|
||||||
else:
|
else:
|
||||||
x = random.randint(0, len(quotes)-1)
|
x = random.randint(0, len(quotes)-1)
|
||||||
quote = quotes[x]
|
quote = quotes[x]
|
||||||
print(f'{quote['quote']}\n\t\t\t - {quote['author']}')
|
print(f'{quote['quote']}\n\t\t\t - {quote['author']}')
|
||||||
dictation = quote['quote'].split(' ')
|
dictation = quote['quote'].split(' ')
|
||||||
|
# Dictate the quote
|
||||||
for word in dictation:
|
for word in dictation:
|
||||||
engine.say(word)
|
engine.say(word)
|
||||||
engine.runAndWait()
|
engine.runAndWait()
|
||||||
|
# Break between words as determined by the wpm
|
||||||
time.sleep(1/(config['wpm']/60))
|
time.sleep(1/(config['wpm']/60))
|
||||||
|
|
||||||
def file(config):
|
# FILE #
|
||||||
engine = pyttsx3.init()
|
def file(config, engine):
|
||||||
|
# Check the filename is definied
|
||||||
if not 'filename' in config:
|
if not 'filename' in config:
|
||||||
print('Filename required, use -f <filename>')
|
print('Filename required, use -f <filename>')
|
||||||
exit()
|
exit()
|
||||||
|
# Loop over the file, outputting it
|
||||||
with open(f'{config['filename']}', 'r') as file:
|
with open(f'{config['filename']}', 'r') as file:
|
||||||
lines = file.readlines()
|
lines = file.readlines()
|
||||||
# Text output
|
# Text output
|
||||||
@@ -79,4 +92,5 @@ def file(config):
|
|||||||
for word in line.split(' '):
|
for word in line.split(' '):
|
||||||
engine.say(word)
|
engine.say(word)
|
||||||
engine.runAndWait()
|
engine.runAndWait()
|
||||||
|
# Break between words as determined by the wpm
|
||||||
time.sleep(1/(config['wpm']/60))
|
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 maths
|
||||||
import dictate
|
import dictate
|
||||||
|
|
||||||
|
engine = pyttsx3.init()
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
with open('config.json', 'r') as file:
|
with open('config.json', 'r') as file:
|
||||||
config = json.load(file)
|
config = json.load(file)
|
||||||
@@ -15,13 +17,14 @@ config = args.handle_arguments(sys.argv, config)
|
|||||||
|
|
||||||
dictation = ''
|
dictation = ''
|
||||||
|
|
||||||
|
engine.setProperty('rate', config['wpm'])
|
||||||
# TODO: match case
|
# TODO: match case
|
||||||
match config['mode']:
|
match config['mode']:
|
||||||
case 'word':
|
case 'words':
|
||||||
dictate.words(config)
|
dictate.words(config, engine)
|
||||||
case 'timed':
|
case 'timed':
|
||||||
dictate.timed(config)
|
dictate.timed(config, engine)
|
||||||
case 'quote':
|
case 'quote':
|
||||||
dictate.quote(config)
|
dictate.quote(config, engine)
|
||||||
case 'file':
|
case 'file':
|
||||||
dictate.file(config)
|
dictate.file(config, engine)
|
||||||
|
|||||||
Reference in New Issue
Block a user