PY-31

Record Token Positions by Document

  • Medium
  • Search Indexing
  • Python

Task

Write record_token_positions(documents). documents is a list of two-item tuples (document_id, tokens). Document IDs are unique. Each tokens value is a sequence of hashable tokens.

Return a dictionary whose outer keys are tokens. Each outer value is a dictionary mapping a document ID to a list of positions at which that token appears in the document. Positions are zero-based and must be strictly increasing. Include a document entry only when that token appears there.

Preserve the first token-encounter order in the outer dictionary and the source document order in each inner dictionary. Repeated tokens receive all their positions. Do not change documents or any token sequence.

Example

The first "red" creates the first outer key, while the second occurrence extends its existing position list. "blue" has entries for both documents, in document source order; "red" has no "d2" entry because it is absent.

Your implementation

Edit solution.py and keep this function name and signature:

Return new outer and inner dictionaries and new position lists. Do not add empty document entries, sort tokens or documents, print the result, or ask for input. An empty document list returns an empty dictionary.