Question: A partially completed RandomSampler class is given to you below. Your job is to complete the following two functions: _process_new_item: it receives a single item

A partially completed RandomSampler class is given to you below. Your job is to complete the following two functions:

_process_new_item: it receives a single item and decides whether the item should be added to self.sample. It also ensures self.counts always has the most updated counts of emojis that are extracted from the tweets in self.sample.
do_sampling: it receives a stream object and iterates over the stream. During each iteration, it processes a new item as specified by the Random Sampling algorithm. Finally it returns a copy of self.sample and self.counts for grading at every iteration, which you don't need to worry about. However, please do make sure you don't inadvertently change the indentation of the yield statement.
At the end of every iteration, the autograder checks the content of your self.sample and self.counts. Below is an example content of both.

self.sample:
['Lmaoooooo love you allll',
'RT @kaseykreated: BEST CITY IN MISSOURI! Let's argue ???????? https://t.co/p7DWK5OAd5',
'Hubble Hooks a One-Arm Galaxy via NASA https://t.co/csOJhfJMpj https://t.co/Aer6ILkskg',
'RT @makio_elecom: 先日はアンジュさんをネタにしてしまい、大変申し訳ございませんでした。 https://t.co/9cO6IPV3hB',
'#tell حبيبتي شكراًًً????????????']

self.counts:
defaultdict(, {'????': 2, '????': 3})
 

from collections import defaultdict

class RandomSampler:
   
   def __init__(self, in_sample_prob, seed=None):
       
       self.in_sample_prob = in_sample_prob
       self.random = HistPresvRandom(seed) # used whenever randomness is needed in your solution
       self.sample, self.counts = list(), defaultdict(int) # recommended to use defaultdict, but an ordinary dict works fine too
   
   def _process_new_item(self, item):
       """
       Applies random sampling to a newly arrived item
       """

       # YOUR CODE HERE

   
   def do_sampling(self, stream):
       """
       Iterates over a stream and performs random sampling
       """
       
       self.sample.clear() # clear the existing sample
       self.counts.clear() # clear the existing counts
       
       for item in stream: # iterate over the stream
           
           # YOUR CODE HERE

           
           # returns a copy of sample and counts at the end of every iteration for grading - code given
           yield self.sample.copy(), self.counts.copy()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres the completed RandomSampler class with the processnewitem and dosampling functions filled in p... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!