The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called within main and the file pointer is used to read lines in the file. Create a dictionary called 'dict_of_words' that have words as 'keys' and (integer) counters as values. The counter values keep a count of the number of times a word has appeared in the text file. In the end, store the dictionary key, values in a list, sort and print the list on the screen.
Note that the counts are not case-sensitive, that is, 'Word' is the same as 'word' or 'wORd'.
Also, note that your program should account for if a ',' (comma) separates two words, e.g. 'food, water, electricity'
Example:
Contents of input text file:
I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success such emotions make a man forget food sleep friends love everything.
Nikola Tesla

Output:
[('a', 1), ('any', 1), ('as', 1), ('brain', 1), ('by', 1), ('can', 1), ('creation', 1), ('do', 1), ('emotions', 1), ('everything', 1), ('felt', 1), ('food', 1), ('forget', 1), ('friends', 1), ('go', 1), ('he', 1), ('heart', 1), ('human', 1), ('i', 1), ('inventor', 1), ('is', 1), ('like', 1), ('love', 1), ('make', 1), ('man', 1), ('nikola', 1), ('not', 1), ('of', 1), ('sees', 1), ('sleep', 1), ('some', 1), ('success', 1), ('such', 1), ('tesla', 1), ('that', 2), ('the', 3), ('there', 1), ('think', 1), ('thrill', 1), ('through', 1), ('to', 1), ('unfolding', 1)]

Use the code below:

def open_file():
fpointer = open('example.txt')
return fpointer

def main():
dictlist = []
fp = open_file()
#loop to iterate over lines in file

for key, value in dict_of_words.items():
temp = (key,value)
dictlist.append(temp)
print(sorted(dictlist))

main()

Respuesta :

Answer:

Code is given as below:

Explanation:

def open_file():

   fpointer = open('example.txt')

   return fpointer

def main():

   dictlist = []

   dict_of_words = dict()

   fp = open_file()

   # loop to iterate over lines in file

   for line in fp:

       for word in line.split():

           if(not dict_of_words.get(word)):

               dict_of_words[word] = 1

           else:

               dict_of_words[word] += 1

   for key, value in dict_of_words.items():

       temp = (key.lower(), value)

       dictlist.append(temp)

   print(sorted(dictlist))

main()

In this exercise we have to use the computer language knowledge in python to write the code as:

the code is in the attached image.

In a more easy way we have that the code will be:

def open_file():

  fpointer = open('example.txt')

  return fpointer

def main():

  dictlist = []

  dict_of_words = dict()

  fp = open_file()

  for line in fp:

      for word in line.split():

          if(not dict_of_words.get(word)):

              dict_of_words[word] = 1

          else:

              dict_of_words[word] += 1

  for key, value in dict_of_words.items():

      temp = (key.lower(), value)

      dictlist.append(temp)

  print(sorted(dictlist))

main()

See more about python at brainly.com/question/26104476

Ver imagen lhmarianateixeira