import gradio as gr import util import re import random ### load and prepare corpus #corpus = util.load_raw_text(corpus_directory="map_avenue") corpus = util.load_single_raw_text_file("quechua.easy.filtered") #corpus = corpus.lower() #word_regex = r"[a-z]+" #def tokenize(text: str): # return re.findall(word_regex, text) #words = tokenize(corpus) words = corpus.split() print(words) lexicon = set() for word in words: lexicon.add(word) filtered_lexicon = set() for word in lexicon: filtered_lexicon.add(word) # if 4 <= len(word) <= 6: # filtered_lexicon.add(word) print(len(filtered_lexicon)) def random_scramble(lexicon: set): lexicon = list(lexicon) word = random.choice(lexicon) # Turn the word into a list of characters word_chars = list(word) # Shuffle those characters random.shuffle(word_chars) # Re-join the characters into a string shuffled = ''.join(word_chars) return {'shuffled': shuffled, 'original': word} def scrambler_game(current_word, guess: str): """ If `guess` is the correct word, return 'Correct' and pick a new word. Otherwise, return 'Incorrect' Returns (correct_label, scrambled_word, current_word) """ if guess == current_word['original']: current_word = random_scramble(filtered_lexicon) return ('😀 ¡Correcto! 😀', current_word['shuffled'], current_word) else: return ('Incorrecto 😕', current_word['shuffled'], current_word) def new_word(): current_word = random_scramble(filtered_lexicon) return ('', current_word['shuffled'], current_word) with gr.Blocks(theme=gr.themes.Soft(), title="Simita Tariy") as unscramble: # Start with some initial word current_word = gr.State(random_scramble(filtered_lexicon)) gr.Markdown("# Simita Tariy") instructions_textbox = gr.Textbox(label="Instrucciones", interactive=False, value="Este juego es el 'Crucigrama', en este encontrarás una serie de letras desordenadas que conforman una palabra. Tu tarea es hallar el orden correcto de esta. ¡Mucho éxito!") # Notice how we set the initial value based on the State scrambled_textbox = gr.Textbox(label="Crucigrama", interactive=False, value=current_word.value['shuffled']) guess_textbox = gr.Textbox(label="Adivinar - Adivina la palabra y luego aprieta en 'enviar'") guess_button = gr.Button(value="Enviar") new_word_button = gr.Button(value="Nueva Palabra") output_textbox = gr.Textbox(label="Resultado", interactive=False) guess_button.click(fn=scrambler_game, inputs=[current_word, guess_textbox], outputs=[output_textbox, scrambled_textbox, current_word]) new_word_button.click(fn=new_word, inputs=[], outputs=[output_textbox, scrambled_textbox, current_word]) unscramble.launch(share=True)