import json
from collections import Counter
from pathlib import Path
with open('graphify-out/.graphify_detect.json', 'r', encoding='utf-8') as f:
    d = json.load(f)
dirs = Counter()
base = Path('.').resolve()
for k, v in d.get('files', {}).items():
    for filepath in v:
        try:
            p = Path(filepath)
            # get relative path
            if p.is_absolute():
                rel = p.relative_to(base)
            else:
                rel = p
            # top level dir
            parts = rel.parts
            if len(parts) > 1:
                dirs[parts[0]] += 1
            else:
                dirs['.'] += 1
        except ValueError:
            pass
print("Top 5 subdirectories by file count:")
for k, v in dirs.most_common(5):
    print(f"  {k}: {v} files")
