yuanjunchai
commited on
Commit
·
8249632
1
Parent(s):
7e1eff0
add application files
Browse files
app.py
CHANGED
|
@@ -263,23 +263,39 @@ def averaged_glove_embeddings_gdrive(sentence, word_index_dict, embeddings, mode
|
|
| 263 |
5. Return averaged embeddings
|
| 264 |
(30 pts)
|
| 265 |
"""
|
| 266 |
-
embedding = np.zeros(int(model_type.split("d")[0]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
-
# Split sentence into words
|
| 269 |
words = sentence.split()
|
| 270 |
-
|
|
|
|
| 271 |
|
| 272 |
for word in words:
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
if valid_words > 0:
|
| 280 |
-
raise ValueError("No valid words in sentence")
|
| 281 |
-
|
| 282 |
-
embedding /= valid_words
|
| 283 |
|
| 284 |
return embedding
|
| 285 |
|
|
|
|
| 263 |
5. Return averaged embeddings
|
| 264 |
(30 pts)
|
| 265 |
"""
|
| 266 |
+
# embedding = np.zeros(int(model_type.split("d")[0]))
|
| 267 |
+
|
| 268 |
+
# # Split sentence into words
|
| 269 |
+
# words = sentence.split()
|
| 270 |
+
# valid_words = 0
|
| 271 |
+
|
| 272 |
+
# for word in words:
|
| 273 |
+
# # Check if the word is in the word_index_dict
|
| 274 |
+
# if word in word_index_dict:
|
| 275 |
+
# word_idx = word_index_dict[word]
|
| 276 |
+
# embedding += embeddings[word_idx]
|
| 277 |
+
# valid_words += 1
|
| 278 |
+
|
| 279 |
+
# if valid_words > 0:
|
| 280 |
+
# raise ValueError("No valid words in sentence")
|
| 281 |
+
|
| 282 |
+
# embedding /= valid_words
|
| 283 |
+
|
| 284 |
+
# return embedding
|
| 285 |
+
embedding_dim = np.zeros(int(model_type.split("d")[0]))
|
| 286 |
+
embedding = np.zeros(embedding_dim)
|
| 287 |
|
|
|
|
| 288 |
words = sentence.split()
|
| 289 |
+
|
| 290 |
+
valid_word_count = 0
|
| 291 |
|
| 292 |
for word in words:
|
| 293 |
+
if word.lower() in word_index_dict:
|
| 294 |
+
embedding += embeddings[word_index_dict[word.lower()]]
|
| 295 |
+
valid_word_count += 1
|
| 296 |
+
|
| 297 |
+
if valid_word_count > 0:
|
| 298 |
+
embedding /= valid_word_count
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
return embedding
|
| 301 |
|