belt_test / app.py
Alexis Palmer
new mapudungun word list
397ad86
raw
history blame
4.47 kB
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:
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, 'Already guessed!')
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, 'Out of guesses! The word is: '+old_word)
else:
return (current_state, 'Wrong guess :(')
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, 'Correct guess!')
# If we made it here, there's no letters left.
current_state = pick_new_word(filtered_lexicon)
return (current_state, 'You win!')
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="Mapudungun Hangman") as hangman:
current_word = gr.State(pick_new_word(filtered_lexicon))
gr.Markdown("# Mapudungun Hangman")
with gr.Row():
current_word_textbox = gr.Textbox(label="Clue", interactive=False, value=create_hangman_clue(current_word.value['word'], current_word.value['guessed_letters']))
guessed_letters_textbox = gr.Textbox(label="Guessed letters", interactive=False)
remaining_chances_textbox = gr.Textbox(label="Remaining chances", interactive=False, value=6)
guess_textbox = gr.Textbox(label="Guess")
guess_button = gr.Button(value="Submit")
hint_button = gr.Button(value="Click here for a free hint")
output_textbox = gr.Textbox(label="Result", interactive=False)
#hints_textbox = gr.Textbox(label="Would you like a hint?", 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)