File size: 2,638 Bytes
97bf531
05b28c8
 
 
97bf531
05b28c8
397ad86
97bf531
397ad86
 
 
 
 
 
 
 
 
 
05b28c8
 
 
 
 
 
 
 
 
bb4edc6
 
 
05b28c8
 
 
b4cc7bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05b28c8
06dc31a
05b28c8
06dc31a
 
bb475f1
06dc31a
 
36bb725
05b28c8
36bb725
05b28c8
 
06dc31a
 
 
 
 
36bb725
06dc31a
 
05b28c8
36bb725
05b28c8
06dc31a
36bb725
05b28c8
36bb725
 
06dc31a
36bb725
05b28c8
36bb725
05b28c8
06dc31a
 
05b28c8
06dc31a
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
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("mapudungun.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="Inan Nemül") as unscramble:
    # Start with some initial word
    current_word = gr.State(random_scramble(filtered_lexicon))
    
    gr.Markdown("# Inan Nemül")
    
    # 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)