File size: 2,906 Bytes
ca2ea21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b21eeb3
 
 
ca2ea21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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)