ironbar commited on
Commit
0f7d64d
1 Parent(s): 46686f8

small refactor

Browse files
Files changed (1) hide show
  1. app.py +18 -15
app.py CHANGED
@@ -1,31 +1,34 @@
1
  import urllib.request
2
  import random
3
- import json
4
  import gradio
5
 
6
- page = urllib.request.urlopen('https://raw.githubusercontent.com/mazyvan/most-common-spanish-words/master/most-common-spanish-words-v5.txt').read().decode()
7
- words = page.split('\n')
8
- len(words)
9
 
10
- len_to_words = {}
11
- for word in words:
12
- if len(word) not in len_to_words:
13
- len_to_words[len(word)] = [word]
14
- else:
15
- len_to_words[len(word)].append(word)
 
 
 
 
 
16
 
17
  def get_random_word(n_letters, forbidden_letters='', required_letters=''):
18
- random.shuffle(len_to_words[n_letters])
19
- for word in len_to_words[n_letters]:
20
  lower_word = word.lower()
21
- if any(letter in lower_word for letter in forbidden_letters):
22
  continue
23
  if required_letters:
24
- if all(letter in lower_word for letter in required_letters):
25
  return '# ' + word
26
  else:
27
  return '# ' + word
28
 
 
 
29
  description = """
30
  Genera palabras aleatorias con el número deseado de letras para aprender a leer.
31
  Se puede forzar a que las palabras tengan o no tengan determinadas letras
@@ -40,4 +43,4 @@ interface = gradio.Interface(
40
  title='Aprende a leer',
41
  description=description,
42
  allow_flagging=False)
43
- interface.launch(server_name="0.0.0.0")
 
1
  import urllib.request
2
  import random
 
3
  import gradio
4
 
 
 
 
5
 
6
+ def download_data():
7
+ page = urllib.request.urlopen('https://raw.githubusercontent.com/mazyvan/most-common-spanish-words/master/most-common-spanish-words-v5.txt').read().decode()
8
+ words = page.split('\n')
9
+ len_to_words = {}
10
+ for word in words:
11
+ if len(word) not in len_to_words:
12
+ len_to_words[len(word)] = [word]
13
+ else:
14
+ len_to_words[len(word)].append(word)
15
+ return len_to_words
16
+
17
 
18
  def get_random_word(n_letters, forbidden_letters='', required_letters=''):
19
+ random.shuffle(LEN_TO_WORDS[n_letters])
20
+ for word in LEN_TO_WORDS[n_letters]:
21
  lower_word = word.lower()
22
+ if any(letter in lower_word for letter in forbidden_letters.lower()):
23
  continue
24
  if required_letters:
25
+ if all(letter in lower_word for letter in required_letters.lower()):
26
  return '# ' + word
27
  else:
28
  return '# ' + word
29
 
30
+
31
+ LEN_TO_WORDS = download_data()
32
  description = """
33
  Genera palabras aleatorias con el número deseado de letras para aprender a leer.
34
  Se puede forzar a que las palabras tengan o no tengan determinadas letras
 
43
  title='Aprende a leer',
44
  description=description,
45
  allow_flagging=False)
46
+ interface.launch(server_name="0.0.0.0")