Question: You are to write simple Python functions to solve the following problems. Define auxiliary functions where appropriate. (d) (5 marks) There is a text file

You are to write simple Python functions to solve the following problems. Define auxiliary functions where appropriate.You are to write simple Python functions to solve the following problems.

(d) (5 marks) There is a text file containing sentences about animals. You are to count the number of times each animal is mentioned. For simplicity, all words in the file are in lower case and there are no punctuation marks. Write a Python function countAnimal (filename) to read from a file and return a sorted list of tuples with the number of occurrences of each animal. The list of animals could be found in a variable called Animallist. def countAnimal (filename) : A function to open and read a file and count the number of occurrences of animals Input: the name of a text file Output: return a sorted list of tuples of animals and counts Sample testing: AnimalList = ["dog","cat", "whale","parrot", "chicken", "sparrow", "fly","butterfly", "bee"] print (countAnimal ("passage.txt")) Sample output: [('bee', 1), ('butterfly', 1), ('cat', 3), ('chicken', 2), ('dog', 3), ('fly', 3), ('parrot', 0), ('sparrow', 2), ('whale', 1)] (e) [5 marks] Based on (d), suppose that animals are classified into different categories, such as mammal. The category information is given as a dictionary. Making use of the function in (d), or otherwise, write a Python function animal Category (filename, category) to read from a file and return a sorted list of categories with an aggregate count of those animals. def animal Category (filename, category): A function to read a file and count the number of occurrences of animals according to their category Input: the name of a text file and a dictionary of category Output: return a sorted list of tuples of animal categories and counts Sample testing: categoryDict = {"mammal":["dog","cat", "whale"], "bird": ["parrot", "chicken", "sparrow"],"insect":["fly", "butterfly", "bee"]} print (animal Category ("passage.txt", categoryDict)) Sample output: [('bird', 4), ('insect', 5), ('mammal', 7)] Content of the file passage.txt is shown below: there is a dog chasing a cat and watching the whale cat is avoiding dog and going for chicken and sparrow chicken tries to catch a fly and a bee fly wonders why fly and butterfly look so different sparrow is also chased behind by cat and sometimes by dog print("Part d") Animallist = ["dog","cat", "whale", "parrot", "chicken", "sparrow", "fly", "butterfly", "bee"] print (countAnimal ("passage.txt")) print("=========") print("Part e") categoryDict = {"mammal":["dog","cat", "whale"), "bird": ["parrot","chicken","sparrow"],"insect":["fly", "butterfly", "bee"]} print (animalCategory ("passage.txt", categoryDict))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!