Answered step by step
Verified Expert Solution
Question
1 Approved Answer
need help writing the topNmatches function in python. it is supposed to use my fastED function that i have already made. please use map and
need help writing the topNmatches function in python. it is supposed to use my fastED function that i have already made.
please use map and not list comprehension
def fastED (S1, S2, memo): "Returns the edit distance between 51 and 52 using memoization if (S1, S2) in memo: return memo [(S1, S2)] elif S1 =="": return len(S2) elif S2== ": return len(S1) elif S1[0] == S2[0]: result =fastED (S1[1:), S2(1:), memo), memo [(S1, S2)]=result return result else: substitution = 1 + fastED (S1[1:), S2(1:), memo), deletion = 1 + fastED (51[1:], S2, memo), insertion = 1 + fastED (S1, S2[1:], memo), result =min( substitution, deletion, insertion), memo [(S1, S2)]=result return result topNmatches(word, nummatches, ListOfWords) Next, you'll write a function whose signature is def topmatches (word, nummatches, ListofWords ) which accepts three arguments: word, a string which is the word to match (using fastED) nummatches, an integer which is O or greater Listof words, a list of strings against which to match word and, from there, top matches should output the alphabetically sorted list of a total of nummatches words from Listof words that have the lowest edit distance scores with the input word: . You should be sure to return only N words. If there is a tie at the last place among those N, there will be an ambiguity as to which words to return. . We will not test it in those "tle" cases, however, so you're welcome to handle that ambiguity as you see nt. Here are a couple of examples: In [1]: topmatches ( "spam", 3, ["spam", "seam", "wow", "cs5blackrocks","span", "synecdoche" ]) Out[1]: ['seam', 'spam', 'span'] In [2]: topmatches ( "spam", 1, ["spam", "seam", "wow", "c85blackrocks", "span", "synecdoche")) Out [2] : [ 'spam'1 # We would not test the above example with nummatches == 2, since the output would be ambiguous And here are a few details that may be of help: You will need to find the score (edit distance) for each word in the master list of words. One way to do this is to construct another list that is just like your master list, except that each entry in that new list will be a tuple of the form (score, word). For example, the tuple (42, "spam") would mean that the word "spam" has edit distance 42 from the word that the user entered. You can use map or a list comprehensions to build this list of tuples! You'll need to sort the words by score. While mergesort is very fast, the amount of recursion that it requires will likely exceed the recursion limit permitted by your computer. Therefore, use the very fast sorting algorithm built into Python, which is named sort. This function is used as follows: If L is the name of your list (it can have any name you like) then the syntax L. sort() will modify L by sorting it in increasing order. This will sort L but will not return anything. So, use the line L. sort() rather than sortedList = L.sort(). It's a strange syntax, but it works (and we'll talk about the reason for this syntax in a few weeks). You may wish to sort a list of items, each of which is a list or a tuple. For example, if you have a list that looks like this: [[42, "hello"). [15, "spam" ], [7, "chocolate" 1) and you do L. sort(), it will sort Lusing the first element in each list as the sorting key. So, L.sort() in this case will change to the list [[7, "chocolate"). [15, "spam"). [42, "hello"]). . You'll use sort again to get your ultimate list of words into alphabetical order Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started