Spaces:
Running
Running
johnpaulbin
commited on
Commit
·
836cf05
1
Parent(s):
c88d786
Update app.py
Browse files
app.py
CHANGED
@@ -30,6 +30,59 @@ def random_spanish_pair():
|
|
30 |
random_pair = random.choice(json_data)
|
31 |
return jsonify(random_pair)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
@app.route('/translate', methods=['POST'])
|
34 |
def dotranslate():
|
35 |
data = request.get_json()
|
|
|
30 |
random_pair = random.choice(json_data)
|
31 |
return jsonify(random_pair)
|
32 |
|
33 |
+
def get_distractors(target_word, all_words, num_distractors=3):
|
34 |
+
"""
|
35 |
+
Get distractor words.
|
36 |
+
'target_word' is the correct word,
|
37 |
+
'all_words' is a list of words to pick distractors from,
|
38 |
+
and 'num_distractors' is the number of distractors to return.
|
39 |
+
"""
|
40 |
+
distractors = set()
|
41 |
+
while len(distractors) < num_distractors:
|
42 |
+
distractor = random.choice(all_words)
|
43 |
+
if distractor.lower() != target_word.lower():
|
44 |
+
distractors.add(distractor)
|
45 |
+
return list(distractors)
|
46 |
+
|
47 |
+
@app.route('/fillgame')
|
48 |
+
def random_spanish_pair():
|
49 |
+
# Select a random English-Spanish pair
|
50 |
+
random_pair = random.choice(json_data)
|
51 |
+
|
52 |
+
# Choose either English or Spanish for the fill-in-the-blank game
|
53 |
+
if random.choice([True, False]):
|
54 |
+
sentence = random_pair['english']
|
55 |
+
language = 'english'
|
56 |
+
else:
|
57 |
+
sentence = random_pair['spanish']
|
58 |
+
language = 'spanish'
|
59 |
+
|
60 |
+
# Split the sentence into words
|
61 |
+
words = re.findall(r'\b\w+\b', sentence)
|
62 |
+
|
63 |
+
# Choose a random word to replace with blank
|
64 |
+
blank_word = random.choice(words)
|
65 |
+
sentence_with_blank = sentence.replace(blank_word, "_____")
|
66 |
+
|
67 |
+
# Collect all words across sentences for distractors (you might want to customize this)
|
68 |
+
all_words = [word for pair in json_data for word in re.findall(r'\b\w+\b', pair[language])]
|
69 |
+
|
70 |
+
# Get distractors
|
71 |
+
distractors = get_distractors(blank_word, all_words)
|
72 |
+
|
73 |
+
# Combine correct word with distractors and shuffle
|
74 |
+
options = [blank_word] + distractors
|
75 |
+
random.shuffle(options)
|
76 |
+
|
77 |
+
# Return the sentence with a blank, options, and the correct word
|
78 |
+
return jsonify({
|
79 |
+
'sentence': sentence_with_blank,
|
80 |
+
'options': options,
|
81 |
+
'correctWord': blank_word,
|
82 |
+
'language': language
|
83 |
+
})
|
84 |
+
|
85 |
+
|
86 |
@app.route('/translate', methods=['POST'])
|
87 |
def dotranslate():
|
88 |
data = request.get_json()
|