Introduction to Computer Programming - CSCI-UA.0002 - Summer 2017

lousy_plasma.py

Write a program that takes the lyrics to Taylor Swift's song, Bad Blood, and replaces a half of its words with synonyms. With enough of the song changed, you can create your own hit song and roll in the money! 💰💰💰

The last parts of this assignment depend on a thesaurus file and a lyrics file that you'll have to download (right-click and save as…) to the same directory that your program is in.

This assignment will cover recent material on:

  • dictionaries
  • file i/o

Part 0

Start off with creating a thesaurus by using a Python dictionary. The thesaurus only has 2 entries in it:

thesaurus = { 
    "happy": "glad",
    "sad"  : "bleak" 
}

The dictionary contains two keys - "happy" and "sad". Each of these keys holds a single synonym, a string, for that key.

Write a program that asks the user for a phrase. Then compare the words in that phrase to the keys in the thesaurus. If the key can be found, you should replace the original word with a random synonym for that word. Words that are changed in this way should be printed in UPPERCASE letters. Make sure to remove all punctuation, but keep letters/numbers and spaces from your initial phrase so that you can find all possible matches (numbers probably don't matter that much, though). Here's a sample running of your program:

Example Interaction

Enter a phrase
> Happy Birthday! exclaimed the sad, sad kitten
GLAD birthday exclaimed the BLEAK BLEAK kitten

Notice that the 1st sad was substituted, and the comma was removed.

Hints

  • Removing punctuation and keeping letters, numbers and spaces
    • One way to do this is to accumulate all of the characters that are alphanumeric (my_character.isalnum()) or space
    • While it's not required, it might be useful to move this functionality into a separate function (maybe call it remove_punctuation … have it take a string and return a new string
  • Breaking apart a string into single words
    • Multiples ways of doing this
    • Here are a couple of suggestions:
      • Accumulate until you see space, then you know you have a word
      • OR simply use split, and then join at the end

Part 1

Modify your program above so that the thesaurus word can have more than one synonym for each word:

thesaurus = { 
    "happy":["glad",  "blissful", "ecstatic", "at ease"], 
    "sad":["bleak", "blue", "depressed"] 
}

In this version, the dictionary contains two keys - "happy" and "sad". Each of these keys holds a list that contains synonyms of that key.

Change your original program by replacing the original word with a random synonym for that word. Again, words that are changed in this way should be printed in UPPERCASE letters. Here's a sample running of your program:

Example Interaction

Enter a phrase
> Happy Birthday! exclaimed the sad, sad kitten
ECSTATIC birthday exclaimed the DEPRESSED BLEAK kitten

Part 2

Rather than use a hard-coded dictionary as your thesaurus, use an external thesaurus file (right-click and save as… to where your Python program is) to populate the keys and values in your dictionary. Use the following line to read in your file (do not use absolute paths):

f = open('thesaurus.txt', 'r') 

The data in the file is in the following format:

word1,synonym1,synonym2,...,synonymN-1,synonymN
word2,synonym1,synonym2,...,synonymN-1,synonymN
word3,synonym1,synonym2,...,synonymN-1,synonymN

Every word occupies its own line followed by a comma-separated list of synonyms. Every word can have a potentially unlimited # of synonyms. Your task for this part is to open this file and parse it into a Python dictionary object so that it functions just like the simple thesaurus from the previous part. Here's a sample running of your program:

Example Interaction

Enter a phrase
> happy birthday said the sad kitten
CONTENT ANNUAL HOLIDAY PRONOUNCED the DOLOROUS CHESHIRE CAT

Hints

Generating the thesaurus will be the trickiest part:

  1. For each word in the thesaurus you will need to create a new list that contains all of the synonyms for that word.
  2. You can then store this list in the dictionary using the 1st word as the key.
  3. Again, split is incredibly helpful here!

Part 3

Finally, modify your above program to write a song for you. It'll use another external file, the lyrics to Taylor Swift's Bad Blood, as a source for lyrics.

  1. Download the lyrics to Bad Blood (right-click and save as to the same directory that your program is in)
  2. Instead of asking the user for a phrase… open the lyrics file (bad_blood.txt), and read the entire contents as a string using file_object.read()
  3. Using the same algorithm that you used in the previous program, change the words in the song to a random word from the thesaurus
    • you'll have to lean out all punctuation again, and only keep letters/numbers and spaces
    • break apart into single words
    • substitute with a random word from thesaurus
  4. However, only do this in 1 out of every 2 words that are found in the thesaurus…
    • that is, if you a word from the lyrics is found in the thesaurus
    • there is a 50% chance that it will be substituted with the thesaurus word
  5. Again, words that are swapped should be printed in all UPPERCASE letters.
  6. Print out the new lyrics!
  7. Profit!!!

See a sample running of the program below (note that you can simply remove all punctuation from the lyrics once you've read them in from the source file):

Example Output

Cause baby now we got DISAPPROBATION blood
You know it use to be mad love
So PLEASURABLENESS a ATTENTION SOCIALITY what youve done
Cause INFANT now we got bad blood ...

(Optional) Part 4

If you're on OSX, you won't even have to sing the lyrics that you "wrote"! Instead, you can have the computer sing it for you. Use the following lines of code at the end of your programming, substituting the variable lyrics with your own variable that contains lyrics:

# lyrics is the variable that contains the new lyrics to your song
from os import system
system("say -i -v Fiona " + "\"" + lyrics + "\"")

CSCI-UA.0002 - Introduction to Computer Programming - Summer 2017