Programming Homework Help

Concord University Python Dictionary Structure Project Lab Report

 

Assignment #10

This assignment uses a great deal of text. We will focus on the Python dictionary structure which you worked on in module 10. We will use data based on an online copyright free version of Roget’s thesaurus on Gutenberg: http://www.gutenberg.org/cache/epub/10681/pg10681.txt – you can download a copy of the thesauraus here: python_asg10_Roget_Thesaurus.txt

Part 0

You have been hired by a small software company to create a “thesaurus” program that replaces words with their synonyms. The company has set you up with a sample thesaurus stored in a Python dictionary object. Here’s the code that represents the thesaurus:

# define our simple thesaurus
thesaurus = {
              "happy": "glad",
              "sad"  : "bleak"
            }

The dictionary contains two keys – “happy” and “sad”. Each of these keys holds a single synonym 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 the synonym for that word. Words that are changed in this way should be printed in UPPERCASE letters. Make sure to remove all punctuation from your initial phrase so that you can find all possible matches. Here’s a sample running of your program:

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

Part 1

Your company realizes that they probably need more than one synonym for each word, so they updated your thesaurus accordingly:

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

The dictionary contains two keys – “happy” and “sad”. Each of these keys holds a list that contains synonyms of 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. Here’s a sample running of your program:

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

Part 2

The company quickly realized that the simple two-word thesaurus was far too small. In order to address this shortcoming they have provided you with a very large text file that contains over 30,000 words (the file is included in the downloadable data files above – the filename for the thesaurus is “python_asg10_Roget_Thesaurus.txt“).

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:

Total words in thesaurus: 33054

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

Hint: For each word you will need to create a new list that contains all the synonyms for that word. You can then store this list in the dictionary using the word as the key.

Part 3

Your program is a hit! In fact, some famous recording artists have come to you to help them re-write some of their most popular songs so that they aren’t so “repetitive”. Your first customer is Mr. Justin Bieber, who’s hit song “Baby” needs a little bit of work:

The lyrics to this song is stored in a text file named “bieber_baby.txt” which you can download here: bieber_baby.txt

Justin doesn’t want you to change ALL of his lyrics – just a few here and there to add some variety. Begin by asking the user for a “chance” value – this value dictates the % chance that a given word will be substituted with one in the thesaurus. Finally, open the file and swap out words using the “chance” value supplied (i.e. the word is “happy” and the chance is 0.5 – this word is in the thesauraus, so there is a 50% chance that we would swap it with a random word from the thesaurus and a 50% chance that we will leave it as-is). Words that are swapped should be printed in all UPPERCASE letters. Here’s a sample running of your program (note that you can simply remove all puntuation from the source file for this program):

Total words in thesaurus: 33054

Enter a % chance to change a word:  0.5

oh whoa 
oh whoa 
oh whoa 
 
you know you REGARD me i know ETHICAL SELF care 
just shout whenever and ill be there 
you are my love you are my INNER NATURE 
and we will NOT A BIT IN ANY WAY ever be RETIRED 
 
are we an INSTANCE SUBTEENER ABJURE WAGERING 
were just friends what are you TEACHING 
say theres another and look right in my eyes 
my INAUGURAL YOUNG MAN DEAD my heart for the first MARCH TEMPO 
and i was IN KIND 
 
LAMBKIN baby TOMATO oooh 
DOTE ON baby DAMSEL baby nooo 
PHONY FLEDGLING baby baby oooh 
i thought youd INFLEXIBLY GO ON mine 
 
HOPEFUL YOUNGER SWEETKINS oooh 
UXORIOUSNESS baby COMPACT DOTE nooo 
AFTER THIS FASHION INVERTEBRATE baby DIMINUTIVE oooh 
i ADVERTENCY youd ALL ALONG SUBSIST EXCAVATE 
 
oh IN THAT you i would have THROUGH WITH whatever 
and i UNINFLUENCED cant SENSE we aint together 
and i wanna play it DIMINISH but im losin you 
SINFUL HOLD WITH you anything ill buy you ANYTHING COCKPIT 
and im in pieces LOVER STABILIZE me 
and just shake SUBCONSCIOUS SELF til SUBLIMINAL SELF TAILPIECE OTHER SELF from this bad dream 
im RELEASE GROUND down down down 
and i UNPREPOSSESSED cant ALLOW my COMMENCEMENT ADORATION wont OCCUR NEAR ENOUGH TO 
 
and im CLOSE COPY 
RECRUIT baby baby oooh 
RIVAL baby DRY-NURSE baby nooo 
IN THAT WAY baby YEARLING PRECIOUS HEART oooh 
i thought youd EVER AND ANON OCCUR mine 
 
GAL FLAME baby oooh 
like baby baby baby nooo 
like baby DUCKLING baby oooh 
i SYMPATHY youd DAILY AND HOURLY BE MET WITH FIRETRAP 
 
luda JUST THE SAME i was  i had my first WANT 
there was nobody that compared to my baby 
and nobody came between us or could ever come ABOVE AND BEYOND 
she had me SOMATIC DEATH crazy oh i was starstruck 
she woke me up daily dont DEPRIVATION DECIDING VOTE starbucks 
FEMALE BEING made my heart pound I MYSELF skipped a beat ALTHOUGH i see MY HUMBLE SELF in the PAVED ROAD and 
at VIEW on the playground but i EXACTLY wanna see her on the PUT IN TIME 
she knows she got me dazing cause she was STRAIGHT amazing 
and ADVANCED my heart is breaking ENTIRELY i BARELY THE HOLE WHEREBY NOTE 
 
baby baby baby oooh 
AT WHAT PRICE baby baby baby nooo 
CHICKEN LIVER SISSY baby PONY oooh 
i IDEATIVE youd RAPIDLY HOLD CONVERT 
 
WEANLING DEBUTANT JELLYFISH oooh 
AT PAR baby SUBDEB baby nooo 
CLOSE NEONATAL baby SCHOOLMAID oooh 
i thought youd always BE THE CASE mine 
 
im ASLEEP yeah yeah yeah yeah yeah yeah 
THIS NIGHT im INTEGRATED gone yeah yeah yeah yeah yeah yeah 
IN THESE DAYS im all IMBECILE yeah yeah yeah yeah yeah yeah 
now im BODILY IMBECILE gone gone gone 
im FLABBY