Bird
0
0

Identify the error in this code snippet for loading embeddings:

medium📝 Debug Q6 of 15
NLP - Word Embeddings
Identify the error in this code snippet for loading embeddings:
embedding = {}
for line in open('embed.txt'):
    word, vector = line.split()
    embedding[word] = list(map(float, vector))
Amap(float, vector) incorrectly maps over string characters
Bline.split() splits into more than two parts, causing unpack error
Cembedding dictionary is not initialized
Dopen() function is missing file mode
Step-by-Step Solution
Solution:
  1. Step 1: Analyze line.split() output

    Embedding lines have word plus multiple vector values, so split() returns many parts.
  2. Step 2: Check unpacking and mapping

    Unpacking into word, vector fails because vector is expected as one string but is multiple tokens; also map(float, vector) maps over string characters, not floats.
  3. Final Answer:

    line.split() splits into more than two parts, causing unpack error -> Option B
  4. Quick Check:

    Vector must be list of floats, not string chars [OK]
Quick Trick: Split line correctly to separate word and vector list [OK]
Common Mistakes:
MISTAKES
  • Unpacking split line into two variables only
  • Mapping float over string instead of list
  • Ignoring multiple vector values per line

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes