Skip to content

Instantly share code, notes, and snippets.

@kabirahuja2431
Last active February 23, 2020 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kabirahuja2431/0c6deea3af9b06b35cbc81f7370586de to your computer and use it in GitHub Desktop.
Save kabirahuja2431/0c6deea3af9b06b35cbc81f7370586de to your computer and use it in GitHub Desktop.
class CustomIterableDatasetv2(IterableDataset):
def __init__(self, filename_en, filename_gm):
#Store the filenames in object's memory
self.filename_en = filename_en
self.filename_gm = filename_gm
#And that's it, we no longer need to store the contents in the memory
def preprocess(self, text):
### Do something with text here
text_pp = text.split()
###
return text_pp
def line_mapper(self, line):
#We only have the text in the file for this case
text = line
text = self.preprocess(text)
return text
def __iter__(self):
#Create an iterator
en_itr = open(self.filename_en)
gm_itr = open(self.filename_gm)
#Map each element using the line_mapper
mapped_en_itr = map(self.line_mapper, en_itr)
mapped_gm_itr = map(self.line_mapper, gm_itr)
#Zip both iterators
zipped_itr = zip(mapped_en_itr, mapped_gm_itr)
return zipped_itr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment