Question: 2. (5 pts) Complete the class Misspellings, which should be able to correct the misspelling of attribute names: both for getting the value of an
2. (5 pts) Complete the class Misspellings, which should be able to correct the misspelling of attribute names: both for getting the value of an attribute and setting the value of an attribute. See the exact specifications below. The distance_helper module defines a Memoize class and a min_dist function. Leave these definitions exactly as they are. Do not worry what the Memoize class does nor how it works: we will study it in a few weeks. Do not worry how the min_dist function works, but understand that it recursively computes the minimum distance between two strings. The more dissimilar the strings, the bigger the value it returns. The distance between strings increases by 1 for every addition, deletion, or substitution it takes to convert one string into the other. For example min_dist ("able", "camel") is 4: to transform "able" into "camel" we can add a c at the front, substitute an m for a b, add an e between the m and l, and delete the e at the end Going the other way, we can delete the c at the front, substitute a b for an m, add an l between the m and e, and delete the l at the end. There are other ways to convert in 4 changes, but no ways to do it in 3: so 4 is the minimum distance. The __init__ method in Misspelling has a parameter (whose default value is False) that is used to set the fix_when_setting attribute (see below for how this attribute is used). It calls initialize_attributes to initialize the attribute names that may be misspelled in tests. For testing purposes, this is easier to do outside of __init__. You can change the inside of either method, but __init__ must call initialize_attributes as its last statement. 1. Write a method named closest_matches; its parameters are self (an object from the class Misspelling) and name (a str); it returns a list of all the attribute names in that Misspelling object that have the same minimum distance from name, so long at that minimum distance is half of the length of name (so, not too distant from name). The returned list may be empty (no attributes names are close), or contain one or more values (multiple attribute names all having the same minimum distance). 2. Write the __getattr__ method; its parameters are self (an object from the class Misspelling) and name (a str that is not the correct spelling of any attribute in the object); it returns the value of the attribute name in self that is closest to name (use the list of attribute names returned by closest_matches). Do this only if there is exactly one attribute name that is the minimum distance from name: if there are none or more than one, this method should raise a NameError exception with an appropriate message. Remember that __getattr__ is called by Python only if name is not an actual attribute in an object. 3. Write the __setattr__ method; its parameters are self (an object from the class Misspelling), name (a str that may or may not be the name of an attribute), and value (the new value to be bound to the attribute name). This method should allow (a) the binding of any attribute names in __init__ and initialize_attributes; (b) the rebinding of any attribute name that is already stored in the object. Otherwise (c) if name is not an attribute name and the fix_when_setting attribute is True, then value should be bound to the unique name that most closely matches name (use the list of attribute names returned by closest_matches). if none or more than one attribute names match with the minimum distance (or fix_when_setting is False) then this method should raise a NameError exception with an appropriate message. Hint: Use code like the History class example in the notes to ensure all the attribute names in __init__ and initialize_attributes get bound correctly. Read the comment before initialize_attributes, which guarantees the last attribute name bound in that method is last. Here is an example of a run of the code in misspelling.py (allowing fixing spelling in __setattr__). Allow fixing mispelling in __setattr__[True]: {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.babel 3 {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.babe 3 {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.least 4 {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.colorado ('Misspelling.__getattr__: name(colorado) not found; matches =', []) {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.amorx ('Misspelling.__getattr__: name(amorx) not found; matches =', ['amoral', 'more']) {'fix_when_setting': True, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.babel = 5 {'fix_when_setting': True, 'amoral': 1, 'more': 2, 'babel': 5, 'last': 4} Enter test: o.babe = 6 {'fix_when_setting': True, 'amoral': 1, 'more': 2, 'babel': 6, 'last': 4} Enter test: o.mora = 7 {'fix_when_setting': True, 'amoral': 1, 'more': 7, 'babel': 6, 'last': 4} Enter test: o.least = 8 {'fix_when_setting': True, 'amoral': 1, 'more': 7, 'babel': 6, 'last': 8} Enter test: o.amorx = 9 ('Misspelling.__setattr__: name(amorx) not found; matches =', ['amoral', 'more']) {'fix_when_setting': True, 'amoral': 1, 'more': 7, 'babel': 6, 'last': 8} Enter test: o.amoralandmuchmuchmore = 10 ('Misspelling.__setattr__: name(amoralandmuchmuchmore) not found; matches =', []) Here is an example of a run of the code in misspellings.py (not allowing fixing spelling in __setattr__). Allow fixing mispelling in __setattr__[True]: False {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.least 4 {'fix_when_setting': False, 'amoral': 1, 'more': 2, 'babel': 3, 'last': 4} Enter test: o.least = 5 Misspelling.__setattr__: name(least) not found and spelling correction disabled
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
