
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.
Instructor solution
Think you've got it?
In the solution code, what is the purpose of the line totalCount.update(someList)
?
- 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.
You may exit out of this review and return later without penalty.