Bird
0
0

Which Python code snippet correctly loads GloVe embeddings from a text file into a dictionary?

easy📝 Syntax Q3 of 15
NLP - Word Embeddings
Which Python code snippet correctly loads GloVe embeddings from a text file into a dictionary?
Awith open('glove.txt') as f: embeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in f}
Bwith open('glove.txt') as f: embeddings = {line[0]: line[1:] for line in f.read().split()}
Cembeddings = open('glove.txt').readlines().split()
Dembeddings = dict(open('glove.txt').read())
Step-by-Step Solution
Solution:
  1. Step 1: Understand GloVe file format

    Each line has a word followed by numbers representing its vector.
  2. Step 2: Check code correctness

    with open('glove.txt') as f: embeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in f} splits each line, uses first token as key, rest as float vector list, which is correct.
  3. Final Answer:

    with open('glove.txt') as f: embeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in f} -> Option A
  4. Quick Check:

    Correct loading = split line, map floats, assign word key [OK]
Quick Trick: Split line, first word key, rest floats vector [OK]
Common Mistakes:
MISTAKES
  • Not converting vector strings to floats
  • Using read() instead of iterating lines
  • Incorrect dictionary comprehension syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes