PY-38

Build Overlapping Token Groups

  • Medium
  • Text Processing
  • Python

Task

Write build_token_groups(documents, group_size). documents is a list of two-item tuples (doc_id, tokens), where tokens is a list of strings. IDs are unique, and group_size is positive.

For each document, an overlapping token group is an exact tuple of group_size consecutive tokens. Starting at every possible token position, collect the group into a set. Return one dictionary mapping each doc_id to that set. A document shorter than group_size maps to an empty set. Keep the document insertion order in the dictionary and do not modify documents or any token list.

You may call a group a shingle: here it means only the exact consecutive tuple defined above, not a normalized or approximate match.

Example

The groups overlap: the second group starts one token after the first. Repeated groups collapse because the value for each document is a set.

Your implementation

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

Return a dictionary with one entry per input document, including short and empty documents. Each group must be a tuple, and no input collection may be changed.