import csv


def _build_dict_from_csv(csv_path):
    """ Builds the Emoji to Sentiment dictionary from the CSV file. """

    emoji_sentiment_rankings = {}

    # MrMindy:
    # Adding the encoding. At least on Windows, I tested it before, the terminal displays an error that implies
    # with the charset. Adding the UTF-8, everything runs smoothly.

    with open(csv_path, newline='', encoding='utf-8') as csv_file:
        csv_reader = csv.reader(csv_file)
        _header_row = next(csv_reader)
        for row in csv_reader:
            emoji = row[0]
            unicode_codepoint = row[1]
            occurrences = int(row[2])
            position = float(row[3])
            negative = float(row[4])
            neutral = float(row[5])
            positive = float(row[6])
            unicode_name = row[7]
            unicode_block = row[8]
            sentiment_score = float(
                '{:.3f}'.format((positive - negative) / occurrences)
            )

            emoji_sentiment_rankings[emoji] = {
                'unicode_codepoint': unicode_codepoint,
                'occurrences': occurrences,
                'position': position,
                'negative': negative,
                'neutral': neutral,
                'positive': positive,
                'unicode_name': unicode_name,
                'unicode_block': unicode_block,
                'sentiment_score': sentiment_score
            }

    return emoji_sentiment_rankings