Could you use counts(i) and counts(i)(j), instead of the val resourceContextCounts? I think that would be clearer.

Same with token Counts: qc(counts(i)(j))