Spaces:
Sleeping
Sleeping
File size: 4,885 Bytes
17900ec 378388e 17900ec |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import gradio as gr
import util
import re
import random
### load and prepare corpus
corpus = util.load_single_raw_text_file("ckunsa.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 create_hangman_clue(word, guessed_letters):
"""
Given a word and a list of letters, create the correct clue.
For instance, if the word is 'apple' and the guessed letters are 'a' and 'l', the clue should be 'a _ _ l _'
"""
clue = ''
for letter in word:
if letter in guessed_letters:
clue += letter + ' '
else:
clue += '_ '
return clue
def free_hint(current_state):
"""
Give user a free hint by filling in one randomly-selected letter.
"""
word = current_state['word']
guessed_letters = current_state['guessed_letters']
hint = random.choice(word)
while hint in guessed_letters:
hint = random.choice(word)
guessed_letters.add(hint)
clue = create_hangman_clue(word, guessed_letters)
return clue
def pick_new_word(lexicon):
lexicon = list(lexicon)
return {
'word': random.choice(lexicon),
'guessed_letters': set(),
'remaining_chances': 6
}
def hangman_game(current_state, guess):
"""Update the current state based on the guess."""
guess = guess.lower()
if guess in current_state['guessed_letters'] or len(guess) > 1:
# Illegal guess, do nothing
return (current_state, 'Letra ya encontrada - intenta nuevamente')
current_state['guessed_letters'].add(guess)
if guess not in current_state['word']:
# Wrong guess
current_state['remaining_chances'] -= 1
if current_state['remaining_chances'] == 0:
old_word = current_state['word']
# No more chances! New word
current_state = pick_new_word(filtered_lexicon)
return (current_state, 'No quedan intentos. La palabra era: '+old_word)
else:
return (current_state, 'Tu letra no está en la palabra :(')
else:
# Right guess, check if there's any letters left
for letter in current_state['word']:
if letter not in current_state['guessed_letters']:
# Still letters remaining
return (current_state, '¡Intento correcto!')
# If we made it here, there's no letters left.
old_word = current_state['word']
current_state = pick_new_word(filtered_lexicon)
return (current_state, '😀 ¡Buen trabajo! La palabra era: 😀 '+ old_word)
def state_changed(current_state):
clue = create_hangman_clue(current_state['word'], current_state['guessed_letters'])
guessed_letters = current_state['guessed_letters']
remaining_chances = current_state['remaining_chances']
return (clue, guessed_letters, remaining_chances)
with gr.Blocks(theme=gr.themes.Soft(), title="Ckunsa Ahorcado") as hangman:
current_word = gr.State(pick_new_word(filtered_lexicon))
gr.Markdown("# Ckunsa Ahorcado")
instructions_textbox = gr.Textbox(label="Instrucciones", interactive=False, value="Te presentamos el juego del 'Ahorcado', en este deberás selecionar letras hasta encontrar la palabra correcta. A medida que aciertes aparecerá la letra seleccionada en el espacio indicado. Recuerda que puedes usar pistas. ¡Buena suerte!")
with gr.Row():
current_word_textbox = gr.Textbox(label="La palabra", interactive=False, value=create_hangman_clue(current_word.value['word'], current_word.value['guessed_letters']))
guessed_letters_textbox = gr.Textbox(label="Letras encontradas", interactive=False)
remaining_chances_textbox = gr.Textbox(label="Intentos restantes", interactive=False, value=6)
guess_textbox = gr.Textbox(label="Adivina la letra y luego aprieta en 'Enviar'")
guess_button = gr.Button(value="Enviar")
hint_button = gr.Button(value="Aprieta acá para obtener una pista")
output_textbox = gr.Textbox(label="Resultado", interactive=False)
guess_button.click(fn=hangman_game, inputs=[current_word, guess_textbox], outputs=[current_word, output_textbox])\
.then(fn=state_changed, inputs=[current_word], outputs=[current_word_textbox, guessed_letters_textbox, remaining_chances_textbox])
hint_button.click(fn=free_hint, inputs=[current_word], outputs=[current_word_textbox])
hangman.launch(share=True)
|