Alexis Palmer commited on
Commit
05b28c8
1 Parent(s): ba43e42
Files changed (1) hide show
  1. app.py +109 -4
app.py CHANGED
@@ -1,7 +1,112 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import util
3
+ import re
4
+ import random
5
 
6
+ ### load and prepare corpus
7
+ corpus = util.load_raw_text(corpus_directory="map_avenue")
8
 
9
+ corpus = corpus.lower()
10
+ word_regex = r"[a-z]+"
11
+ def tokenize(text: str):
12
+ return re.findall(word_regex, text)
13
+
14
+ words = tokenize(corpus)
15
+
16
+ lexicon = set()
17
+ for word in words:
18
+ lexicon.add(word)
19
+
20
+ filtered_lexicon = set()
21
+
22
+ for word in lexicon:
23
+ if 4 <= len(word) <= 6:
24
+ filtered_lexicon.add(word)
25
+
26
+ print(len(filtered_lexicon))
27
+
28
+
29
+ def create_hangman_clue(word, guessed_letters):
30
+ """
31
+ Given a word and a list of letters, create the correct clue.
32
+
33
+ For instance, if the word is 'apple' and the guessed letters are 'a' and 'l', the clue should be 'a _ _ l _'
34
+ """
35
+ clue = ''
36
+ for letter in word:
37
+ if letter in guessed_letters:
38
+ clue += letter + ' '
39
+ else:
40
+ clue += '_ '
41
+ return clue
42
+
43
+
44
+ def pick_new_word(lexicon):
45
+ lexicon = list(lexicon)
46
+
47
+ return {
48
+ 'word': random.choice(lexicon),
49
+ 'guessed_letters': set(),
50
+ 'remaining_chances': 6
51
+ }
52
+
53
+
54
+ def hangman_game(current_state, guess):
55
+ """Update the current state based on the guess."""
56
+
57
+
58
+ if guess in current_state['guessed_letters'] or len(guess) > 1:
59
+ # Illegal guess, do nothing
60
+ return (current_state, 'Invalid guess')
61
+
62
+ current_state['guessed_letters'].add(guess)
63
+
64
+ if guess not in current_state['word']:
65
+ # Wrong guess
66
+ current_state['remaining_chances'] -= 1
67
+
68
+ if current_state['remaining_chances'] == 0:
69
+ # No more chances! New word
70
+ current_state = pick_new_word(filtered_lexicon)
71
+ return (current_state, 'You lose!')
72
+ else:
73
+ return (current_state, 'Wrong guess :(')
74
+
75
+ else:
76
+ # Right guess, check if there's any letters left
77
+ for letter in current_state['word']:
78
+ if letter not in current_state['guessed_letters']:
79
+ # Still letters remaining
80
+ return (current_state, 'Correct guess!')
81
+
82
+ # If we made it here, there's no letters left.
83
+ current_state = pick_new_word(filtered_lexicon)
84
+ return (current_state, 'You win!')
85
+
86
+
87
+ def state_changed(current_state):
88
+ clue = create_hangman_clue(current_state['word'], current_state['guessed_letters'])
89
+ guessed_letters = current_state['guessed_letters']
90
+ remaining_chances = current_state['remaining_chances']
91
+ return (clue, guessed_letters, remaining_chances)
92
+
93
+
94
+ with gr.Blocks(theme=gr.themes.Soft(), title="Mapudungun Hangman") as hangman:
95
+ current_word = gr.State(pick_new_word(filtered_lexicon))
96
+
97
+ gr.Markdown("# Mapudungun Hangman")
98
+
99
+ with gr.Row():
100
+ current_word_textbox = gr.Textbox(label="Clue", interactive=False, value=create_hangman_clue(current_word.value['word'], current_word.value['guessed_letters']))
101
+ guessed_letters_textbox = gr.Textbox(label="Guessed letters", interactive=False)
102
+ remaining_chances_textbox = gr.Textbox(label="Remaining chances", interactive=False, value=6)
103
+
104
+ guess_textbox = gr.Textbox(label="Guess")
105
+ guess_button = gr.Button(value="Submit")
106
+
107
+ output_textbox = gr.Textbox(label="Result", interactive=False)
108
+
109
+ guess_button.click(fn=hangman_game, inputs=[current_word, guess_textbox], outputs=[current_word, output_textbox])\
110
+ .then(fn=state_changed, inputs=[current_word], outputs=[current_word_textbox, guessed_letters_textbox, remaining_chances_textbox])
111
+
112
+ hangman.launch(share=True)