You can use this assignment in your class!
Mastery Progress(100.0%)
i
Your instructor specifically marked this question as important.
imgEducator’s Choice

Get a leg up on add-one smoothing: Write a function that takes two inputs, a set of terms and a list of terms, and returns a Counter() object that represents one count for each of the terms in the set, plus an additional count for each time that each term occurs in the list.

Updating a Counter()
One of the tasks that we have to do on HW 7 would be nicely solved by using a Counter() object. Hopefully you've already worked with a Counter() to do things like count the terms in a document. But what about if you needed to count the number of terms in two different things? Maybe you have a set of terms in some collection (ie, a Vocabulary), and you also have a document as a list of terms, and you want to count all of the terms in the document, but add one to each term count for each term in the Vocabulary. (That's what add-one smoothing is, right?)
 
One of the things that might make this task easier is to be able to update a Counter(), or add to the counts that are already present--that is, start a Counter() with a count of 1 for every term, and then add to that the "real" counts for a document. Let's look at how to do that here. Write a function that takes two inputs, a set of terms and a list of terms, and returns a Counter() object that represents the total count of all terms in the set and in the list.

You answeredJUST NOW
  • Solution
  • Unit test #1
Python
from collections import Counter
def add_terms(someSet,someList):
```
A function that takes in a set of terms (as normalized strings) and
a list of terms (as normalized strings), and returns a Counter()
object that represents the count of all terms in the set
(where each term would be counted as "1") and all terms in the
list (where a term might occur more than once).
```
raise NotImplementedError
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
You passed all unit tests!
Unit test number
1
Expected STDOUT
Your STDOUT

Did you like this question?

(Voting helps us personalize your learning experience!)
imgInstructor solution
Eric Maurice JacksonNOV 29, 2023, 6:00:49 PM
Python
from collections import Counter
def add_terms(someSet,someList):
'''
A function that takes in a set of terms (as normalized strings) and
a list of terms (as normalized strings), and returns a Counter()
object that represents the count of all terms in the set
(where each term would be counted as "1") and all terms in the
list (where a term might occur more than once).
'''
if not (type(someSet) is set and type(someList) is list):
raise TypeError("Improper type of input parameters: {}, {}".format(type(someSet), type(someList)))
totalCount = Counter(someSet)
totalCount.update(someList)
return totalCount
# This is clearly intended to be one way to implement add-one
# smoothing, by starting our Counter() with a 1 in the position
# for every term in our Vocabulary, before we actually start
# tallying the counts for terms that occur in a document class.
# You may see the above code and note that what it involves is
# adding 1 to our Counter() for every term *before* we add any of
# the "real" term counts, instead of making the "real" counts
# first and adding 1 later for smoothing. Does it really
# *matter* whether we add 1 before or after we do the "real"
# counts? No.
# It would also be possible to convert someSet into a list,
# concatenate that list with somelist, and feed that resulting
# list into a Counter(). However, that would (1) not be as fast,
# and (2) might not be as easy for readers to see the point of
# what was going on, since writing the code this way makes the
# focus looks like it's building a list, not building a counter:
termList = [*someSet]
termList.extend(someList)
return Counter(termList)
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Was this helpful?

(Voting helps us personalize your learning experience!)
Think you've got it?

In the solution code, what is the purpose of the line totalCount.update(someList)?

Select one of the following options:
  • A.

    This line is actually problematic, since the value that is returned by the .update() method is not stored in a new object.

  • B.

    This line performs an update directly on the totalCount object, which is a Counter.

  • C.

    This line returns a value of 0, which indicates successful completion of this operation.

Submit answer

Was this helpful?

(Voting helps us personalize your learning experience!)

You may exit out of this review and return later without penalty.