Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help with code only after #your code here. Code will be tested with the great gatsby and war and peace txt . I would
Please help with code only after #your code here. Code will be tested with the great gatsby and war and peace txt I would also like to know how to fix the error TypeError: 'tuple' object is not callable
Problem : Using a Bloom Filter to Count Common Words.
In this problem, we will implement a Bloom filter to count how many elements of longe rwordswp the words of length or more in War and Peace
appear in the GreatGatsby novel. To do so we will do the following:
Instantiate a Bloom filter with number of bits and number of hash functions
Insert all words from greatgatsby into the filter.
For each word from war and peace, check membership in the Bloom filter and count the number of yes answers.def initself nbits, nhash: self.m nbits ## get k randdom hash functions# Function to insert a word in a Bloom filter. # your code here index hashfnword# Check if a word belongs to the Bloom Filter # your code here index hashfnword return True
In :
##o the exact countthis operation finishes very quickly.
allwordsgg setlongerwordsgg
exactcommonwc
for word in longerwordswp: exactcommonwc exactcommonwc
printfExact common word count exactcommonwc
Exact common word count
In :bf BloomFilter
for word in longerwordsgg:for word in longerwordsgg:commonwordcount
for word in longerwordswp: commonwordcount commonwordcount
printfNumber of common words of length equals : commonwordcount
assert commonwordcount exactcommonwc
printAll Tests Passed: points'
TypeError Traceback most recent call last
I have copied the entire thing from the previous expert but it does not work. It shows NameError: name 'nbits' is not defined import random
def getrandomhashfunctionseed None: random.seedseeddef hashfunctionword:return hashfunction
class BloomFilter: self.bits Falsenbits # Initialize all bits to fals self.k nhash self.hashfunreps getrandomhashfunction for i in rangeselfkdef insertself word: for hashfn in self.hashfunreps: self.bitsindex Truedef memberself word: for hashfn in self.hashfunreps: if not self.bitsindex: return True
In : #do the exact countthis operation finishes very quickly.
allwordsgg setlongerwordsgg
exactcommonwc
for word in longerwordswp: exactcommonwc exactcommonwc
printfExact common word count exactcommonwc
Exact common word count
In : # Try to use the same using a bloom filter.for word in longerwordsgg:for word in longerwordsgg:commonwordcount if bfmemberword:printfNumber of common words of length equals : commonwordcountprintAll Tests Passed: points'
NameError
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