Bird
0
0

You have a list of documents:

hard📝 Model Choice Q15 of 15
NLP - Text Similarity and Search
You have a list of documents:
docs = ['Data Science is fun', 'I love machine learning', 'Deep learning and data']

You want to create a dictionary where keys are unique words (case-insensitive) from all documents, and values are lists of document indices where the word appears. Which code snippet correctly does this?
Aword_docs = {} for i, doc in enumerate(docs): for word in doc.lower().split(): word_docs.setdefault(word, []).append(i)
Bword_docs = {} for i, doc in enumerate(docs): for word in doc.split(): word_docs[word].append(i)
Cword_docs = {word: i for i, doc in enumerate(docs) for word in doc.lower().split()}
Dword_docs = {} for doc in docs: for word in doc.lower().split(): word_docs[word] = doc
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    Create a dictionary mapping each unique lowercase word to a list of document indices where it appears.
  2. Step 2: Analyze each option

    word_docs = {} for i, doc in enumerate(docs): for word in doc.lower().split(): word_docs.setdefault(word, []).append(i) uses setdefault to initialize lists and appends indices correctly with lowercase words. word_docs = {} for i, doc in enumerate(docs): for word in doc.split(): word_docs[word].append(i) misses initializing lists and ignores case. word_docs = {word: i for i, doc in enumerate(docs) for word in doc.lower().split()} creates a dict with last index only, not lists. word_docs = {} for doc in docs: for word in doc.lower().split(): word_docs[word] = doc overwrites values with document strings, not indices.
  3. Final Answer:

    word_docs = {} for i, doc in enumerate(docs): for word in doc.lower().split(): word_docs.setdefault(word, []).append(i) -> Option A
  4. Quick Check:

    Use setdefault and lowercase words for correct mapping [OK]
Quick Trick: Use setdefault to build lists for each word [OK]
Common Mistakes:
MISTAKES
  • Not initializing lists before appending
  • Ignoring case normalization
  • Overwriting dictionary values instead of appending

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes