jq one-liner
jq '
(. as $d
| [.document.scenes[].body[]
| select(.type == "dialogue")]) as $lines
| [.characters[]
| { name: .name,
id: .id,
lines: ([$lines[] | select(.character == .id)] | length) }]
| sort_by(-.lines)' screenplay.json
Output:
[
{ "name": "MARA", "id": "8d2f-…", "lines": 87 },
{ "name": "ELLIS", "id": "3b1a-…", "lines": 54 },
{ "name": "ORNA", "id": "a9c2-…", "lines": 33 }
]
In Python
import json
from collections import Counter
with open('screenplay.json') as f:
doc = json.load(f)
by_char = {c['id']: c['name'] for c in doc['characters']}
counts = Counter()
for scene in doc['document']['scenes']:
for el in scene['body']:
if el.get('type') == 'dialogue':
counts[el['character']] += 1
for cid, n in counts.most_common():
print(f"{by_char[cid]:<20} {n}")