import json
import os
from collections import defaultdict


def analyse():
    path = "../data/"
    files = os.listdir(path)
    stats = {
        "avg_length": defaultdict(lambda: 0),
        "missing": defaultdict(lambda: 0)
    }

    for filename in files:
        with open(path + filename) as f:
            content = json.load(f)
            artist = content["name"]
            songs = content["songs"]

            print("\n## %s ##" % artist)

            for song in songs:
                title = song["title"]
                if song["lyrics"] is not None:
                    lyrics = song["lyrics"]
                    stats["avg_length"][artist] += len(lyrics)
                else:
                    stats["missing"][artist] += 1

            stats["avg_length"][artist] /= len(songs)
            print("%s: Average song is %i long (%i missing)." % (
            artist, stats["avg_length"][artist], stats["missing"][artist]))


if __name__ == '__main__':
    analyse()
