aprender_a_leer / app.py
ironbar's picture
better syllabes
e37d1d1
import urllib.request
import random
import gradio
def download_data():
page = urllib.request.urlopen('https://raw.githubusercontent.com/mazyvan/most-common-spanish-words/master/most-common-spanish-words-v5.txt').read().decode()
words = page.split('\n')
len_to_words = {}
for word in words:
if len(word) not in len_to_words:
len_to_words[len(word)] = [word]
else:
len_to_words[len(word)].append(word)
len_to_words[2] = get_syllabes()
return len_to_words
def get_syllabes():
syllabes = set()
vowels = 'aeiou'
consonants = 'bcdfghjklmnpqrstvwxyz'
for consonant in consonants:
for vowel in vowels:
if consonant in 'gq' and vowel in 'ei':
syllabes.add(consonant + 'u' + vowel)
else:
syllabes.add(consonant + vowel)
remove = ['qu', 'qa', 'qi', 'qo', 'qu']
syllabes = sorted(list(syllabes.difference(remove)))
print(syllabes)
return syllabes
def get_random_word(n_letters, forbidden_letters='', required_letters=''):
random.shuffle(LEN_TO_WORDS[n_letters])
for word in LEN_TO_WORDS[n_letters]:
lower_word = word.lower()
if any(letter in lower_word for letter in forbidden_letters.lower()):
continue
if required_letters:
if all(letter in lower_word for letter in required_letters.lower()):
return '# ' + word
else:
return '# ' + word
LEN_TO_WORDS = download_data()
description = """
Genera palabras aleatorias con el número deseado de letras para aprender a leer.
Se puede forzar a que las palabras tengan o no tengan determinadas letras
"""
interface = gradio.Interface(
get_random_word,
inputs=[gradio.Slider(2, 15, value=5, step=1, label='Número de letras'),
gradio.Textbox(label='Letras prohibidas'),
gradio.Textbox(label='Letras obligatorias')],
#outputs=[gradio.Textbox(label='')],
outputs=[gradio.Markdown(label='')],
title='Aprende a leer',
description=description,
allow_flagging=False)
interface.launch(server_name="0.0.0.0")