Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Thank you in advance! In Python Please :) CODE GIVEN: PROBLEM: [] class AD (dict): definit__(self, *args, **kwargs) : This initializer simply passes all

Thank you in advance! In Python Please :)

CODE GIVEN:

image text in transcribed

image text in transcribed

image text in transcribed

PROBLEM: image text in transcribed

image text in transcribed

[] class AD (dict): definit__(self, *args, **kwargs) : "" "This initializer simply passes all arguments to dict, so that we can create an AD with the same ease with which we can create a dict. There is no need, indeed, to repeat the initializer, but we leave it here in case we want to create attributes specific to an AD later."" super() . _init__(*args, **kwargs) def _add_(self, other): return AD._binary_op(self, other, lambda x, y: x + y, 0) def sub_(self, other): return AD. _binary_op (self, other, lambda x, y: x - y, 0) @staticmethod def binary op (left, right, op, neutral): r = AD() 1_keys = set (left.keys()) if isinstance(left, dict) else set(). r_keys = set (right.keys()) if isinstance(right, dict) else set(). for k in i_keys | r_keys: # If the right (or left) element is a dictionary (or an AD), # we get the elements from the dictionary; else we use the right # or left value itself. This implements a sort of dictionary # broadcasting. l_val = left.get(k, neutral) if isinstance(left, dict) else left r_val = right.get(k, neutral) if isinstance(right, dict) else right r[k] = op(l_val, r_val) return r For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD. Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe""" return f'(self.name}: {self.ingredients}' def hash__(self): ""Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id."" return hash(self.name) def_eq_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) U return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): "" "We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def repr_(self): "" "To have a better representation of the recipe""" return f'{self.name}: {self.ingredients}' def _hash_(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id." return hash(self.name) def _eg__(self, other): """For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) To make sure everything is in order, we have some tests to ensure that our classes work. [] pantry = Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5,"lemon":6}) ramen = Recipe("Ramen", { "noodles":1, "soy sauce":2, "egg": 2, "bean sprouts":4}) assert_equal (pantry, Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5, "lemon":6})) assert_equal(ramen.name, "Ramen") assert_equal(ramen.ingredients, AD({ "noodles":1, "soy sauce":2, "egg":2, "bean sprouts":4})) + Code + Text - Problem 5: Bag-of-Words Model of Text In natural language processing, a bag-of-words (BOW) model of a piece of text is obtained by counting how many times each word appears in the text. We can easily represent BOW models using arithmetic dictionaries. For example, the BOW model of the string "I really, really, really like pizza" is the arithmetic dictionary {'i': 1, 'really': 3, 'like': 1, 'pizza': 1} For this exercise, you will write a function text_to_bow that takes a string as argument, and returns the BOW model of the string as an AD. To implement text_to_bow, you should use the split_text function, defined below, to split the input string into individual words. U import re # This is a regular expression defining all except A-Za-Z0-9 splitter = re.compile('\w+') def split_text(W): return splitter.split(w.lower()) # Make sure it's working. split_text("I like pizza with anchovies, really") [] def text_to_bow(text): " "Computes and returns the BOW model of a text string. The BOW model is represented by an AD. Can be implemented in 5 lines of code.""" # YOUR CODE HERE raise Not ImplementedError() [ ] # Tests for text_to_bow bowl = text to bow("I like pizza") assert isinstance (bowl, AD) assert_equal (bowl, AD(i=1, like=l, pizza=1)) bow2 = text_to_bow("I like that you like pizza") assert_equal (bow2, AD(i=1, like=2, that=l, you=1, pizza=1)) assert_equal(bowl + bow2, AD(i=2, like=3, pizza=2, that=1, you=1)) bow3 = text_to_bow("I really, really, really like pizza") assert_equal (bow3, AD(i=1, really=3, likeri, pizza=1)). [] class AD (dict): definit__(self, *args, **kwargs) : "" "This initializer simply passes all arguments to dict, so that we can create an AD with the same ease with which we can create a dict. There is no need, indeed, to repeat the initializer, but we leave it here in case we want to create attributes specific to an AD later."" super() . _init__(*args, **kwargs) def _add_(self, other): return AD._binary_op(self, other, lambda x, y: x + y, 0) def sub_(self, other): return AD. _binary_op (self, other, lambda x, y: x - y, 0) @staticmethod def binary op (left, right, op, neutral): r = AD() 1_keys = set (left.keys()) if isinstance(left, dict) else set(). r_keys = set (right.keys()) if isinstance(right, dict) else set(). for k in i_keys | r_keys: # If the right (or left) element is a dictionary (or an AD), # we get the elements from the dictionary; else we use the right # or left value itself. This implements a sort of dictionary # broadcasting. l_val = left.get(k, neutral) if isinstance(left, dict) else left r_val = right.get(k, neutral) if isinstance(right, dict) else right r[k] = op(l_val, r_val) return r For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD. Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe""" return f'(self.name}: {self.ingredients}' def hash__(self): ""Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id."" return hash(self.name) def_eq_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) U return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) class Pantry (AD): def __init__(self, ingredients): "" "We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . __init__(ingredients) class Recipe: def __init__(self, name, ingredients): """We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def repr_(self): "" "To have a better representation of the recipe""" return f'{self.name}: {self.ingredients}' def _hash_(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id." return hash(self.name) def _eg__(self, other): """For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) To make sure everything is in order, we have some tests to ensure that our classes work. [] pantry = Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5,"lemon":6}) ramen = Recipe("Ramen", { "noodles":1, "soy sauce":2, "egg": 2, "bean sprouts":4}) assert_equal (pantry, Pantry({ "noodles":5, "soy sauce":8, "pepper":10, "bean sprouts":12, "egg":5, "lemon":6})) assert_equal(ramen.name, "Ramen") assert_equal(ramen.ingredients, AD({ "noodles":1, "soy sauce":2, "egg":2, "bean sprouts":4})) + Code + Text - Problem 5: Bag-of-Words Model of Text In natural language processing, a bag-of-words (BOW) model of a piece of text is obtained by counting how many times each word appears in the text. We can easily represent BOW models using arithmetic dictionaries. For example, the BOW model of the string "I really, really, really like pizza" is the arithmetic dictionary {'i': 1, 'really': 3, 'like': 1, 'pizza': 1} For this exercise, you will write a function text_to_bow that takes a string as argument, and returns the BOW model of the string as an AD. To implement text_to_bow, you should use the split_text function, defined below, to split the input string into individual words. U import re # This is a regular expression defining all except A-Za-Z0-9 splitter = re.compile('\w+') def split_text(W): return splitter.split(w.lower()) # Make sure it's working. split_text("I like pizza with anchovies, really") [] def text_to_bow(text): " "Computes and returns the BOW model of a text string. The BOW model is represented by an AD. Can be implemented in 5 lines of code.""" # YOUR CODE HERE raise Not ImplementedError() [ ] # Tests for text_to_bow bowl = text to bow("I like pizza") assert isinstance (bowl, AD) assert_equal (bowl, AD(i=1, like=l, pizza=1)) bow2 = text_to_bow("I like that you like pizza") assert_equal (bow2, AD(i=1, like=2, that=l, you=1, pizza=1)) assert_equal(bowl + bow2, AD(i=2, like=3, pizza=2, that=1, you=1)) bow3 = text_to_bow("I really, really, really like pizza") assert_equal (bow3, AD(i=1, really=3, likeri, pizza=1))

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

Students also viewed these Databases questions