michaelginn's picture
Upload folder using huggingface_hub
1c56d55
# Load our data
import os
def load_raw_text(corpus_directory: str, file_names=None) -> str:
"""Loads all the text files in a directory into one large string"""
corpus = ""
for file_name in os.listdir(corpus_directory):
# Read the file as a string
file_path = os.path.join(corpus_directory, file_name)
if os.path.isdir(file_path):
continue
# Make sure we only read text files
if ".txt" not in file_name:
continue
with open(file_path, 'r') as file:
file_contents = file.read()
corpus += (file_contents + "\n")
return corpus
# REPLACE WITH YOUR CORPUS DIRECTORY
corpus = load_raw_text(corpus_directory="./corpus")
import re
import util
# TODO: Strip accents using util.strip_accents
# TODO: Make corpus lowercase
corpus = corpus.lower()
# TODO: Split corpus into tokens using the following function
word_regex = r"[a-zïëñ]+"
def tokenize(text: str):
return re.findall(word_regex, text)
s_tok = tokenize(corpus)
# TODO: Create a set named "lexicon" with all of the unique words
lexicon = set()
for word in s_tok:
lexicon.add(word)
filtered_lexicon = set()
for word in lexicon:
if 3 <= len(word) <= 7:
filtered_lexicon.add(word)
import random
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}
import gradio as gr
from typing import Tuple
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 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."""
if guess in current_state['guessed_letters'] or len(guess) > 1:
# Illegal guess, do nothing
return (current_state, 'Invalid guess')
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:
# No more chances! New word
current_state = pick_new_word(filtered_lexicon)
return (current_state, 'You lose!')
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="karijona Hangman") as hangman:
current_word = gr.State(pick_new_word(filtered_lexicon))
gr.Markdown("# karijona 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")
output_textbox = gr.Textbox(label="Result", 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])
hangman.launch()