Answered step by step
Verified Expert Solution
Link Copied!

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 2: Using a Bloom Filter to Count Common Words.
In this problem, we will implement a Bloom filter to count how many elements of longe r_words_wp (the words of length 5 or more in War and Peace)
appear in the Great-Gatsby novel. To do so, we will do the following:
Instantiate a Bloom filter with number of bits n and number of hash functions k.
Insert all words from great-gatsby into the filter.
For each word from war and peace, check membership in the Bloom filter and count the number of yes answers.def __init__(self, nbits, nhash): self.m = nbits ## get k randdom hash functions# Function to insert a word in a Bloom filter. # your code here index = hash_fn(word)# Check if a word belongs to the Bloom Filter # your code here index = hash_fn(word) return True
In [11]:
##o the exact countthis operation finishes very quickly.
all_words_gg = set(longer_words_gg)
exact_common_wc =0
for word in longer_words_wp: exact_common_wc = exact_common_wc +1
print(f'Exact common word count ={exact_common_wc}')
Exact common word count =124595
In [16]:bf = BloomFilter(100000,5)
for word in longer_words_gg:for word in longer_words_gg:common_word_count =0
for word in longer_words_wp: common_word_count= common_word_count +1
print(f'Number of common words of length >==5 equals : {common_word_count}
assert ( common_word_count >= exact_common_wc)
print('All. Tests Passed: 10 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 get_random_hash_function(seed = None): random.seed(seed)def hash_function(word):return hash_function
class BloomFilter: self.bits =[False]*nbits # Initialize all bits to fals self.k = nhash self.hash_fun_reps =[get_random_hash_function() for i in range(self.k)]def insert(self, word): for hash_fn in self.hash_fun_reps: self.bits[index]= Truedef member(self, word): for hash_fn in self.hash_fun_reps: if not self.bits[index]: return True
In [24]: #do the exact countthis operation finishes very quickly.
all_words_gg = set(longer_words_gg)
exact_common_wc =0
for word in longer_words_wp: exact_common_wc = exact_common_wc +1
print(f'Exact common word count ={exact_common_wc}')
Exact common word count =124595
In [27]: # Try to use the same using a bloom filter.for word in longer_words_gg:for word in longer_words_gg:common_word_count =0 if bf.member(word):print(f'Number of common words of length >=5 equals : {common_word_count}')print('All Tests Passed: 10 points')
NameError
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions

Question

Write out the details of the proof of Theorem 2.1.3.

Answered: 1 week ago