#!/usr/bin/env python

"""
Parse musixmatch dataset with bag of words in json format and output a
Apache Feather file where each row is a song in dense format using
word embeddings the dense vector is constructed as the sum of
count * embedding for each word in the song. Some options are available
to custom this behavior
"""
from pathlib import Path
import argparse
import logging
import logging.config
import pandas as pd

from song_lyrics.parse import json_glove
from song_lyrics.transform import bows2embeddings
from song_lyrics.util import load_logging_config_file


def main():
    # use docstring at the top as description for the script
    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument('path_to_data', type=str,
                        help='Path to file with bag of words to use as input, '
                             'must be a feather file where every row is a '
                             'song, one column is track_id and the rest are '
                             'words')
    parser.add_argument('path_to_glove', type=str,
                        help='Path to the GLOVE json data')
    parser.add_argument('path_to_output', type=str,
                        help='Output path for the feather file')
    args = parser.parse_args()

    logging_config = load_logging_config_file()

    folder = Path(args.path_to_output).parent
    filename = '{}.log'.format(Path(__file__).name)
    logging_config['handlers']['file']['filename'] = str(folder / filename)

    logging.config.dictConfig(logging_config)

    logger = logging.getLogger(__name__)

    logger.info('Log will be saved at: {}'.format(str(folder / filename)))

    # load word embeddings subset
    glove = json_glove('data/clean/embeddings_subset.json')

    logger.info('Loaded {:,} embeddings...'.format(len(glove)))

    # load musixmatch data
    mxm = pd.read_feather(args.path_to_data)

    track_ids = mxm.track_id.tolist()
    mxm.drop(columns=['track_id'], inplace=True)
    words = list(mxm)
    bows = [row.to_dict() for idx, row in mxm.iterrows()]

    # convert bag of words to dense representation using embeddings
    df = bows2embeddings(bows, track_ids, glove)

    # feather doesn't like ints as column names
    df.rename(columns={c: 'D'+str(c) for c in df.columns
                       if c != 'track_id'}, inplace=True)

    logger.info('Saving file in {}'.format(args.path_to_output))
    df.to_feather(args.path_to_output)


if __name__ == "__main__":
    main()
