-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
173 lines (132 loc) · 4.64 KB
/
app.py
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import streamlit as st
import numpy as np
from backend import (
get_secret_word,
get_neihgboors,
get_distance_rank,
write_charade,
secret_word_list,
get_word_of_the_day
)
@st.experimental_dialog("Mostrar Vizinhos")
def show_neihgboors(neighboors):
neighboors = neighboors.drop(columns="l2_distance")
st.dataframe(
neighboors,
column_config={
"word": "Palavra",
"distance": st.column_config.NumberColumn(
"Distancia",
format="%d ⬆️",
),
},
hide_index=True
)
@st.experimental_dialog("Mostrar Dicas")
def show_guesses(guess):
guess = guess.drop(columns="l2_distance")
st.dataframe(
guess,
column_config={
"word": "Palavra",
"distance": st.column_config.NumberColumn(
"Distancia",
format="%d ⬆️",
),
},
hide_index=True
)
@st.cache_data
def escolhendo_palavra(guessed_word):
return get_neihgboors(guessed_word)
@st.cache_data
def cache_secret_wordlist():
return secret_word_list()
def icon(emoji: str):
"""Shows an emoji as a Notion-style page icon."""
st.write(
f'<span style="font-size: 78px; line-height: 1">{emoji}</span>',
unsafe_allow_html=True,
)
st.set_page_config(page_icon='🕹️', layout='wide', page_title='Jogo de palavras') ''
# Initialize session state
# ----------------------
if 'secret_word' not in st.session_state:
st.session_state.secret_word = get_word_of_the_day(cache_secret_wordlist())
if 'attempts' not in st.session_state:
st.session_state.attempts = []
if 'w_guessed_word' not in st.session_state:
st.session_state.w_guessed_word = []
if 'charade' not in st.session_state:
st.session_state.charade = write_charade(st.session_state.secret_word)
# Declare functions
# -----------------
def submit():
st.session_state.w_guessed_word = st.session_state.widget
st.session_state.widget = ""
def submit_guessed_word(guessed_word):
guessed_word = guessed_word.lower().strip()
df_neihgboors = escolhendo_palavra(st.session_state.secre
distance=get_distance_rank(guessed_word, df_neihgboors)
st.session_state.attempts.append((guessed_word, distance))
if d istance > 1000:
str_distance="Longe demais 😒"
else:
str_distance=f"{distance} 🤔"
st.markdown(
f"""
**Distancia:** {str_distance} **Tentativas:** {len(st.session_state.attempts)}
""",
unsafe_allow_html=True
)
if guessed_word.lower() == st.session_state.secret_word.lower():
st.success(f"Parabens! Você descobriu a palavra: {st.session_state.secret_word}")
st.balloons()
if st.button("Vizinhos"):
df_neihgboors=escolhendo_palavra(st.session_state.secret_word)
show_neihgboors(df_neihgboors)
else:
st.info("Tente de novo!")
def register_attempts():
for attempt, distance in reversed(list(st.session_state.attempts)):
st.error(f'{attempt} {distance}')
# ---------------------------
# Start UI design
# ---------------------------
# Side Bar
with st.sidebar:
if st.button('Novo Jogo'):
st.session_state.secret_word=get_secret_word(cache_secret_wordlist())
st.session_state.attempts=[]
st.session_state.charade=write_charade(st.session_state.secret_word)
st.session_state.attempts=[]
st.session_state.w_guessed_word=[]
escolhendo_palavra(st.session_state.secret_word)
print(st.session_state.secret_word)
if prompt := st.button("Dicas"):
df_neihgboors=escolhendo_palavra(st.session_state.secret_word)
numero=np.random.randint(2, 50)
guessed_word=df_neihgboors.loc[[numero]]['word'].values[0]
st.session_state.w_guessed_word=guessed_word
st.markdown(
"""
### Como jogar 🎲 ###
O jogo sorteia uma palavra e daí cria uma charada para você tentar adivinhar qual palavra é.
Quanto mais longe da resposta certa maior a distância.
O Jogo calcula a distância entre as palavras. De acordo com seu contexto de uso gramatical.
As palavras são sorteadas aleatoriamente de um arquivo de palavras.
"""
)
# -------------------------------
# Body
icon("📝")
st.subheader("Advinhe a palavra secreta!", divider="rainbow", anchor=False)
escolhendo_palavra(st.session_state.secret_word)
# st.write(f"Palavra secreta gerada!")
st.markdown(st.session_state.charade)
st.text_input("Digite seu palpite: ", key="widget", on_change=submit)
guessed_word=st.session_state.w_guessed_word
if guessed_word:
submit_guessed_word(guessed_word)
register_attempts()
print(st.session_state.secret_word)