Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use Python programing. First please download all 4 files listed below into the s

ID: 3803086 • Letter: U

Question

Use Python programing.

First please download all 4 files listed below into the same folder. Start by reading the poemTranslator.pdf file to learn about the assignment. You will eventually be filling in code in the RPB.py file that reads from one or the other of the poem text files (your choice)!

(Note: if your browser opens files when you click on the links instead of letting you download them, right-click on the file link and select "Save As" or "Save Link As" to download and save the files.)

poem translator pdf -http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/poemTranslator.pdf

RPB.py - http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/RPB.py (after opening this link copy the whole thing and paste it on python to start working on it)

marry poem - http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/maryPoem.txt

Lost generation poem -http://www.cs.uni.edu/~diesburg/courses/cs1510_sp17/homework/PA07/LostGeneration.txt

Explanation / Answer


# import these modules to help me
import re
import codecs
import string
from random import shuffle

def main():
  
    poemFileName = "maryPoem.txt"
  
    print ("") # blank line
  
    #(1)
    print ("(1) Starting Reading Lines Backwards ... ")
    readingLinesBackwards( poemFileName )
  
    #(2)
    print ("(2) Starting Randomize Lines Backwards ... ")
    randomizeLines( poemFileName )
  
    #(3)
    print ("(3) Starting Reading Words Backwards ... ")
    readingWordsBackwards( poemFileName )
  
    #(4)
    print ("(4) Starting Reading last two words ... ")
  
    print ("*** Copies made in current Directory *** ")

    print (" Done. ")

# end main()
#---------------------------------------------------------

#-----------------------
# readingLinesBackwards
#---------------------------------------------------------
# This function accepts a filename as an argument
# of a (raw) text file that contains lines of a poem.
# The function reverses the lines of the poem and
# outputs the backward poem to a new file. The new
# filename will have "backwards_" prepended to the name.
#
# Each line# of the original poem will be printed to the
# beginning of each line in the backwards poem, thus the
# new file will have decreasing line numbers.
#---------------------------------------------------------
def readingLinesBackwards( poemFileName ):
    #this functions flips the poem upside down, and begin reading bottom lines.
  
    INPUT = open(poemFileName, 'r')
  
    newFileName = "linesBackwards_" + poemFileName
    OUTPUT = open(newFileName, 'w')

    # make an empty list to hold all the lines of a poem
    allLines = []
  
    poemTitle = INPUT.readline().strip()
    poemAuthor = INPUT.readline().strip()
  
    # skip blank line after the author name
    INPUT.readline()
  
    # now read each line of the poem and store the lines in a list
    for nextLine in INPUT:
        nextLine.strip()
        if (nextLine != ""):
            allLines.append(nextLine)
  
    # close the input (original) file
    INPUT.close()
  
    # now reverse the lines in the list
    allLines.reverse()
  
    # get the total number of lines in this poem
    totalLines = len(allLines)
  
    # print title and author
    OUTPUT.write("Lines Backwards ")
    OUTPUT.write('Title: {0} '.format( poemTitle ))
    OUTPUT.write('Author: {0} '.format( poemAuthor ))
  
    # i will start counting at the end
    i = totalLines
  
    for line in allLines:
        OUTPUT.write('{0} {1} '.format(i,line) )
        i = i-1
    # end of for loop
  
    OUTPUT.close()
  
# end readingLinesBackwards()
#---------------------------------------------------------


#----------------
# randomizeLines
#---------------------------------------------------------
# This function makes it possible to read the poem with random line order (stanzas),
#therefore there is no specific line structre from original poem.
#---------------------------------------------------------
def randomizeLines( poemFileName ):
  
  
    INPUT = open(poemFileName, 'r')
  
    newFileName = "Randomize_Lines_Backwards_" + poemFileName
    OUTPUT = open(newFileName, 'w')

    # make an empty list to hold all the lines of a poem
    allLines = []
  
    poemTitle = INPUT.readline().strip()
    poemAuthor = INPUT.readline().strip()
  
    # skip blank line after the author name
    INPUT.readline()
  
    # now read each line of the poem and store the lines in a list
    for nextLine in INPUT:
        nextLine.strip()
        if (nextLine != ""):
            allLines.append(nextLine)
  
    # close the input (original) file
    INPUT.close()
    # from here on out I need to call each cell index randomize word and
    # print the word

    shuffle(allLines)
  
    totalLines = len(allLines)
  
    # print title and author
    OUTPUT.write("Random Lines ")
    OUTPUT.write('Title: {0} '.format( poemTitle ))
    OUTPUT.write('Author: {0} '.format( poemAuthor ))
  
    # i will start counting at the end
    i = totalLines
  
    for line in allLines:
        OUTPUT.write(line)
  
    # end of for loop
  
    OUTPUT.close()
   
  
# end randomizeLines()
#---------------------------------------------------------
  
  
#-----------------------
# readingWordsBackwards
#---------------------------------------------------------
# this function make it possible to read the poem Backwards reading from right to left
# the poem begins from the bottom as well. if do not intend to read like that suggest removing
# allLines.reverse() in line 242.
#---------------------------------------------------------
def readingWordsBackwards( poemFileName ):

  
    # INPUT = open(poemFileName, 'r')
  
    #---------------------------------------------------------------

      
    INPUT = open(poemFileName, 'r')
  
    newFileName = "Words_Backward_" + poemFileName
    OUTPUT = open(newFileName, 'w')

    # make an empty list to hold all the lines of a poem
    allLines = []
  
    poemTitle = INPUT.readline().strip()
    poemAuthor = INPUT.readline().strip()
  
    # skip blank line after the author name
    INPUT.readline()
  
    # now read each line of the poem and store the lines in a list
    for nextLine in INPUT:
        nextLine.strip()
        if (nextLine != ""):
            allLines.append(nextLine)
          
    # close the input (original) file
    INPUT.close()      
    ReversePoem =[]
    # -----Funtion that Flips poem and reads backwords------}

    allLines.reverse()

    for lines in allLines:
        SLines = lines.split()
        SLines.reverse()
        sentence = ' '.join(SLines)
        sentence = sentence.lower()
        sentence = remove_punctuation(sentence)
        ReversePoem.append(sentence)

      
    #END LOOP
    totalLines = len(ReversePoem)
    # print title and author
    OUTPUT.write("backwords poem ")
    OUTPUT.write('Title: {0} '.format( poemTitle ))
    OUTPUT.write('Author: {0} '.format( poemAuthor ))
  
    # i will start counting at the end
    i = totalLines
  
    for line in ReversePoem:
        OUTPUT.write('{0} {1} '.format(i,line) )
        i = i-1
    # end of for loop
  
    OUTPUT.close()
# end readingWordsBackwards()

#-------------------------------
# Spell backwards read backwards
#---------------------------------------------------------
# This fucntion maked it possible to see the spelling of the
# backwards to catch any palindroms and to check any encryted message that
# may lie within the poem.
#---------------------------------------------------------
# (4)
  
    INPUT = open(poemFileName, 'r')
  
    newFileName = "Spell_Backwards_Reverse_" + poemFileName
    OUTPUT = open(newFileName, 'w')

    # make an empty list to hold all the lines of a poem
    allLines = []
  
    poemTitle = INPUT.readline().strip()
    poemAuthor = INPUT.readline().strip()
  
    # skip blank line after the author name
    INPUT.readline()
  
    # now read each line of the poem and store the lines in a list
    for nextLine in INPUT:
        nextLine.strip()
        if (nextLine != ""):
            allLines.append(nextLine)
  
    # close the input (original) file
    INPUT.close()
  
    allLines.reverse()
    RhymeScheme = []
    # now reverse the lines in the list
    for line in allLines:
        line.split()
        line = line.lower()
        line = remove_punctuation(line)
        RhymeScheme.append(line[::-1])
      

    # get the total number of lines in this poem

    totalLines = len(RhymeScheme)
    # print title and author
    OUTPUT.write("Lines Rhyme ")
    OUTPUT.write('Title: {0} '.format( poemTitle ))
    OUTPUT.write('Author: {0} '.format( poemAuthor ))
  
    # i will start counting at the end
    i = totalLines
  
    for line in RhymeScheme:
        OUTPUT.write(line)
    # end of for loop
  
    OUTPUT.close()


#end of Number (4)

#---------------------------------------------------------


#--------------------
# remove_punctuation
#---------------------------------------------------------
# This function accepts a string (e.g., line of a poem)
# and removes all occurances of the default list of
# punctuation symbols (found in Python's string.punctuation)
#---------------------------------------------------------
def remove_punctuation( s ):
    s_without_punct = ""
    for letter in s:
        if letter not in string.punctuation:
            s_without_punct += letter
          
    return s_without_punct
#---------------------------------------------------------


#-----------------------------------------------------
if __name__ == '__main__':
    main()
#-----------------------------------------------------
  

maryPoem.txt

Mary had a little lamb
Sarah Josepha Hale

Mary had a little lamb,
whose fleece was white as snow.

And everywhere that Mary went,
the lamb was sure to go.

He followed her to school one day
which was against the rule.

It made the children laugh and play,
to see a lamb at school.

And so the teacher turned it out,
but still it lingered near,

And waited patiently about,
till Mary did appear.

"Why does the lamb love Mary so?"
the eager children cry.

"Why, Mary loves the lamb, you know."
the teacher did reply.