johnpaulbin commited on
Commit
2ac09a0
·
1 Parent(s): f40ebd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -31,32 +31,41 @@ def random_spanish_pair():
31
  random_pair = random.choice(json_data)
32
  return jsonify(random_pair)
33
 
 
 
 
 
 
 
 
 
 
 
34
  def get_distractors(target_word, all_words, num_distractors=3):
35
- """
36
- Get distractor words.
37
- 'target_word' is the correct word,
38
- 'all_words' is a list of words to pick distractors from,
39
- and 'num_distractors' is the number of distractors to return.
40
  """
41
  distractors = set()
42
  while len(distractors) < num_distractors:
43
- distractor = random.choice(all_words)
44
  if distractor.lower() != target_word.lower():
45
  distractors.add(distractor)
46
  return list(distractors)
47
 
48
  @app.route('/fillgame')
49
- def random_spanish_pairfillintheblank():
50
  # Select a random English-Spanish pair
51
  random_pair = random.choice(json_data)
52
 
53
  # Choose either English or Spanish for the fill-in-the-blank game
54
  if random.choice([True, False]):
55
- sentence = random_pair['english']
56
  language = 'english'
 
57
  else:
58
- sentence = random_pair['spanish']
59
  language = 'spanish'
 
60
 
61
  # Split the sentence into words
62
  words = re.findall(r'\b\w+\b', sentence)
@@ -65,11 +74,8 @@ def random_spanish_pairfillintheblank():
65
  blank_word = random.choice(words)
66
  sentence_with_blank = sentence.replace(blank_word, "_____")
67
 
68
- # Collect all words across sentences for distractors (you might want to customize this)
69
- all_words = [word for pair in json_data for word in re.findall(r'\b\w+\b', pair[language])]
70
-
71
  # Get distractors
72
- distractors = get_distractors(blank_word, all_words)
73
 
74
  # Combine correct word with distractors and shuffle
75
  options = [blank_word] + distractors
@@ -82,6 +88,7 @@ def random_spanish_pairfillintheblank():
82
  'correctWord': blank_word,
83
  'language': language
84
  })
 
85
 
86
 
87
  @app.route('/translate', methods=['POST'])
 
31
  random_pair = random.choice(json_data)
32
  return jsonify(random_pair)
33
 
34
+ english_words = set()
35
+ spanish_words = set()
36
+
37
+ # Populate the word lists
38
+ for pair in json_data:
39
+ if "english" in pair:
40
+ english_words.update(re.findall(r'\b\w+\b', pair["english"]))
41
+ if "spanish" in pair:
42
+ spanish_words.update(re.findall(r'\b\w+\b', pair["spanish"]))
43
+
44
  def get_distractors(target_word, all_words, num_distractors=3):
45
+ """
46
+ Get distractor words.
 
 
 
47
  """
48
  distractors = set()
49
  while len(distractors) < num_distractors:
50
+ distractor = random.choice(list(all_words))
51
  if distractor.lower() != target_word.lower():
52
  distractors.add(distractor)
53
  return list(distractors)
54
 
55
  @app.route('/fillgame')
56
+ def random_spanish_pair():
57
  # Select a random English-Spanish pair
58
  random_pair = random.choice(json_data)
59
 
60
  # Choose either English or Spanish for the fill-in-the-blank game
61
  if random.choice([True, False]):
62
+ sentence = random_pair.get('english', "")
63
  language = 'english'
64
+ word_set = english_words
65
  else:
66
+ sentence = random_pair.get('spanish', "")
67
  language = 'spanish'
68
+ word_set = spanish_words
69
 
70
  # Split the sentence into words
71
  words = re.findall(r'\b\w+\b', sentence)
 
74
  blank_word = random.choice(words)
75
  sentence_with_blank = sentence.replace(blank_word, "_____")
76
 
 
 
 
77
  # Get distractors
78
+ distractors = get_distractors(blank_word, word_set)
79
 
80
  # Combine correct word with distractors and shuffle
81
  options = [blank_word] + distractors
 
88
  'correctWord': blank_word,
89
  'language': language
90
  })
91
+
92
 
93
 
94
  @app.route('/translate', methods=['POST'])