import json

""" Input: the path of the dataset file, a list of song and line indices
    Output: List of tuples of words with highest tf-idf scores
    Given a list of song-line tuple (song_index, line_index),
    returns a list of a word-score tuple, with the word with highest score
    at the head of the list.
"""
def score_lookup(data_filename, songs_lst):
    fo = open(data_filename, 'r')
    lyrics = json.loads(fo.read())
    fo.close()

    tfidf_sum = {}
    tfidf_count = {}
    tfidf_scores = []
    for song, line in songs_lst:
        for word_idx in range(len(lyrics[song]['lyrics'][line])):
            word = lyrics[song]['lyrics'][line][word_idx]
            score = lyrics[song]['tfidf_scores'][line][word_idx]
            if word.isalpha():
                if tfidf_sum.has_key(word.lower()):
                    tfidf_sum[word.lower()] += score
                    tfidf_count[word.lower()] += 1
                else:
                    tfidf_sum[word.lower()] = score
                    tfidf_count[word.lower()] = 1
    for word, sum in tfidf_sum.items():
        tfidf_scores.append((word, sum / tfidf_count[word]))

    return sorted(tfidf_scores, key=lambda x: x[1], reverse=True)

import json

""" Input: Loaded dataset, a list of song and line indices
    Output: List of tuples of words with highest tf-idf scores
    Given a list of song-line tuple (song_index, line_index),
    returns a list of a word-score tuple, with the word with highest score
    at the head of the list.
"""
def score_lookup(dataset, songs_lst):
    tfidf_sum = {}
    tfidf_count = {}
    tfidf_scores = []
    for song, line in songs_lst:
        for word_idx in range(len(dataset[song]['lyrics'][line])):
            word = dataset[song]['lyrics'][line][word_idx]
            score = dataset[song]['tfidf_scores'][line][word_idx]
            if word.isalpha():
                if tfidf_sum.has_key(word.lower()):
                    tfidf_sum[word.lower()] += score
                    tfidf_count[word.lower()] += 1
                else:
                    tfidf_sum[word.lower()] = score
                    tfidf_count[word.lower()] = 1
    for word, sum in tfidf_sum.items():
        tfidf_scores.append((word, sum / tfidf_count[word]))

    return sorted(tfidf_scores, key=lambda x: x[1], reverse=True)

Update score lookup to not load dataset file
Update score lookup to not load dataset file
  import json
  
- """ Input: the path of the dataset file, a list of song and line indices
+ """ Input: Loaded dataset, a list of song and line indices
      Output: List of tuples of words with highest tf-idf scores
      Given a list of song-line tuple (song_index, line_index),
      returns a list of a word-score tuple, with the word with highest score
      at the head of the list.
  """
- def score_lookup(data_filename, songs_lst):
+ def score_lookup(dataset, songs_lst):
-     fo = open(data_filename, 'r')
-     lyrics = json.loads(fo.read())
-     fo.close()
- 
      tfidf_sum = {}
      tfidf_count = {}
      tfidf_scores = []
      for song, line in songs_lst:
-         for word_idx in range(len(lyrics[song]['lyrics'][line])):
+         for word_idx in range(len(dataset[song]['lyrics'][line])):
-             word = lyrics[song]['lyrics'][line][word_idx]
+             word = dataset[song]['lyrics'][line][word_idx]
-             score = lyrics[song]['tfidf_scores'][line][word_idx]
+             score = dataset[song]['tfidf_scores'][line][word_idx]
              if word.isalpha():
                  if tfidf_sum.has_key(word.lower()):
                      tfidf_sum[word.lower()] += score
                      tfidf_count[word.lower()] += 1
                  else:
                      tfidf_sum[word.lower()] = score
                      tfidf_count[word.lower()] = 1
      for word, sum in tfidf_sum.items():
          tfidf_scores.append((word, sum / tfidf_count[word]))
  
      return sorted(tfidf_scores, key=lambda x: x[1], reverse=True)
  
Update score lookup to not load dataset file
## Code Before:
import json

""" Input: the path of the dataset file, a list of song and line indices
    Output: List of tuples of words with highest tf-idf scores
    Given a list of song-line tuple (song_index, line_index),
    returns a list of a word-score tuple, with the word with highest score
    at the head of the list.
"""
def score_lookup(data_filename, songs_lst):
    fo = open(data_filename, 'r')
    lyrics = json.loads(fo.read())
    fo.close()

    tfidf_sum = {}
    tfidf_count = {}
    tfidf_scores = []
    for song, line in songs_lst:
        for word_idx in range(len(lyrics[song]['lyrics'][line])):
            word = lyrics[song]['lyrics'][line][word_idx]
            score = lyrics[song]['tfidf_scores'][line][word_idx]
            if word.isalpha():
                if tfidf_sum.has_key(word.lower()):
                    tfidf_sum[word.lower()] += score
                    tfidf_count[word.lower()] += 1
                else:
                    tfidf_sum[word.lower()] = score
                    tfidf_count[word.lower()] = 1
    for word, sum in tfidf_sum.items():
        tfidf_scores.append((word, sum / tfidf_count[word]))

    return sorted(tfidf_scores, key=lambda x: x[1], reverse=True)

## Instruction:
Update score lookup to not load dataset file
## Code After:
import json

""" Input: Loaded dataset, a list of song and line indices
    Output: List of tuples of words with highest tf-idf scores
    Given a list of song-line tuple (song_index, line_index),
    returns a list of a word-score tuple, with the word with highest score
    at the head of the list.
"""
def score_lookup(dataset, songs_lst):
    tfidf_sum = {}
    tfidf_count = {}
    tfidf_scores = []
    for song, line in songs_lst:
        for word_idx in range(len(dataset[song]['lyrics'][line])):
            word = dataset[song]['lyrics'][line][word_idx]
            score = dataset[song]['tfidf_scores'][line][word_idx]
            if word.isalpha():
                if tfidf_sum.has_key(word.lower()):
                    tfidf_sum[word.lower()] += score
                    tfidf_count[word.lower()] += 1
                else:
                    tfidf_sum[word.lower()] = score
                    tfidf_count[word.lower()] = 1
    for word, sum in tfidf_sum.items():
        tfidf_scores.append((word, sum / tfidf_count[word]))

    return sorted(tfidf_scores, key=lambda x: x[1], reverse=True)
