import gzip
import json


def load_reviews(filepath, revs_to_keep=10):
    """
    :param filepath: file to 5-core amazon review file
    :param revs_to_keep: sorts reviews by length and only keeps revs_to_keen longest ones
    :return: dict of iid: list of reviews
    """
    reviews = {}
    with gzip.open(filepath, 'r') as f:
        for line in map(json.loads, f):
            iid = line["asin"]
            this_rev = [line.get("summary"), line.get("reviewText")]
            this_rev = ". ".join([x for x in this_rev if x])
            if iid in reviews:
                reviews[iid].append(this_rev)
            else:
                reviews[iid] = [this_rev]
    # sorts reviews by length to filter out short ones
    for iid in reviews:
        this_revs = sorted(reviews[iid], key=lambda r: len(r), reverse=True)
        reviews[iid] = this_revs[:revs_to_keep]
    return reviews