View the step-by-step solution to:

im really confused on this assignment i have all the files but Im not sure how im supposed to store them on my computer to make the program run. the

im really confused on this assignment i have all the files but Im not sure how im supposed to store them on my computer to make the program run. the teacher provided us a partially written program for us to complete. I complicated code but it doesnt work it has an error that says unintended does not match. bellow are the assignment instructions and the un finished code as well as the one i wrote.

instructions:  

Some scholars believe you can learn a great deal about an author by studying his or her use of pronouns. we will use a dictionary to count the pronouns in gb.txt. Complete the functions in the program in order to count the number of times each pronoun occurs in gb.txt. Complete the function show_counts() to print the number of times each pronoun occurs. For full credit, print the pronouns in alphabetical order and only print the ones that have a count greater than 0.


unfinished code:

import string #To get punctuation characters


def readfile(fname):

  '''Return contents of a text file as a lower-case string'''

  pass #You can take this out or leave it - it doesn't matter

  #Open the file for reading

  #Read the contents of the file as one long string

  #Close the file

  #Use string.punctuation to remove punctuation

  #Return the string (converting it to lower case)


def count_pronouns(word_list, pronoun_list):

  '''Returns a dictionary with words and their counts

    word_list is a list of words from the text file

    pronoun_list is a list of English pronouns'''

  pass #You can take this out or leave it

  #You are given two lists - you don't have to split the text

  #If you want to see what word_list and...

  #...pronoun_list look like you can print them here

  #Start with an empty dictionary

  #For each word in word_list

  # If the word is a pronoun then

  #  If the word is not in the dictionary

  #   Add it with a count of 1

  #  Otherwise,

  #   Add one to that word's count

  #Return the dictionary


def show_counts(pronoun_dict):

  '''Print prounouns and their counts; print only those with counts > 0

    pronoun_dict is the dictionary of pronouns with their counts'''

  pass #You can take this out

  #Get a list of the keys

  #Sort the list of keys

  #For each key in the key list

  #If the count is greater than 0

  # Print the key and the count

  #Don't return anything


#Don't change anything below this point

def main():

  text_filename = 'Data//gb.txt' #Count pronouns in this file

  pronoun_filename = 'Data//pronouns.txt' #List of pronouns

  gb = readfile(text_filename) #gb is the contents of the text file (a string)

  gb_words = gb.split() #Split file contents into a list of words

  pronouns = readfile(pronoun_filename)

  pronoun_list = pronouns.split()

  pronoun_dict = count_pronouns(gb_words, pronoun_list)

  show_counts(pronoun_dict)


main()





my code:

import string #To get punctuation characters


def readfile(fname):

  '''Return contents of a text file as a lower-case string'''

  f = open(fname,'r')        #Open the file for reading

  contents = f.read()         #Read the contents of the file as one long string

  f.close()              #Close the file

  for char in string.punctuation:   #Use string.punctuation to remove punctuation

    contents = contents.replace(char, '')

  return contents.lower()       #Return the string (converting it to lower case)


def count_pronouns(word_list, pronoun_list):

  '''Returns a dictionary with words and their counts

    word_list is a list of words from the text file

    pronoun_list is a list of English pronouns'''

 word_count = {}           #Start with an empty dictionary

 for word in word_list:        #For each word in word_list

   if word in pronoun_list:     #If the word is a pronoun then

     if word not in word_count:  #If the word is not in the dictionary

       word_count[word] = 1   #Add it with a count of 1

     else:            #Otherwise,

       word_count[word] += 1  #Add one to that word's count

 return word_count          #Return the dictionary


def show_counts(pronoun_dict):

  '''Print prounouns and their counts; print only those with counts > 0

    pronoun_dict is the dictionary of pronouns with their counts'''

  

  key_list = list(pronoun_dict.keys()) #Get a list of the keys

  key_list = key_list.sort()      #Sort the list of keys

  for word in word_list:        #For each key in the key list

  if pronoun_dict[word] > 0:     #If the count is greater than 0

  print(word, pronoun_dict[word]):     #Print the key and the count

  #Don't return anything


#Don't change anything below this point

def main():

  text_filename = 'Data//gb.txt' #Count pronouns in this file

  pronoun_filename = 'Data//pronouns.txt' #List of pronouns

  gb = readfile(text_filename) #gb is the contents of the text file (a string)

  gb_words = gb.split() #Split file contents into a list of words

  pronouns = readfile(pronoun_filename)

  pronoun_list = pronouns.split()

  pronoun_dict = count_pronouns(gb_words, pronoun_list)

  show_counts(pronoun_dict)


main()

Top Answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online