Programming Assignment 4 (Song Lyrics)

SONG LYRICS

In this problem you will read a text file containing the lyrics of a song (assume no blank lines in the lyrics) and store each word that appears in the song in a dictionary whose key is the word and the value is a list of positions where the word appears in the lyric (starting at 1). If the word appears at the end of a line, you should store the negative of the position to indicate end of line. Then, using the dictionary you will produce the following output:
  • display the dictionary
  • display the lyrics (you must use the information in the dictionary to reconstruct the lyrics and not simply save the file contents in a variable and print it)
  • display the total number of unique words in the song
  • display the most frequent word (if there is a tie, display all of them)
Here is a sample file, song1.txt:
What have I
What have I
What have I done to deserve this
and here is a sample run of the program:
$ python3 SongLyrics.py song1.txt 

Dictionary:

WHAT    [1, 4, 7]
HAVE    [2, 5, 8]
I       [-3, -6, 9]
DONE    [10]
TO      [11]
DESERVE [12]
THIS    [-13]

Song:

WHAT HAVE I
WHAT HAVE I
WHAT HAVE I DONE TO DESERVE THIS

The number of unique words in the lyric are:  7

Most frequent word(s):  WHAT, HAVE, I

$
The following are two examples of input files:

song1.dat

song2.dat

NOTE: When you display the dictionary, you should print it in two columns (as shown). This should work for any song with different length words. You may use the ljust() method of String objects to left justify the word and fill remaining spots with spaces. Please look up this method on Google or Python documentation.

Submit SongLyrics.py.