Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.image text in transcribed

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago

Question

List the components of the strategic management process. page 72

Answered: 1 week ago