Bird
0
0

Which Python code correctly loads a pre-trained embedding file named glove.txt into a dictionary called embeddings?

easy📝 Syntax Q12 of 15
NLP - Word Embeddings
Which Python code correctly loads a pre-trained embedding file named glove.txt into a dictionary called embeddings?
Aembeddings = open('glove.txt').split()
Bembeddings = open('glove.txt').read()
Cembeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in open('glove.txt')}
Dembeddings = dict(open('glove.txt'))
Step-by-Step Solution
Solution:
  1. Step 1: Understand the file format

    Each line has a word followed by numbers (vector components).
  2. Step 2: Choose code that maps words to vectors

    embeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in open('glove.txt')} splits each line, uses first part as key, rest as float list values.
  3. Final Answer:

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

    Dictionary comprehension with split and float conversion = embeddings = {line.split()[0]: list(map(float, line.split()[1:])) for line in open('glove.txt')} [OK]
Quick Trick: Use dict comprehension with split and float conversion [OK]
Common Mistakes:
MISTAKES
  • Using read() returns a string, not a dict
  • Trying to split on file object directly
  • Passing file object to dict() without processing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes