mathiascreutz commited on
Commit ·
48f737e
1
Parent(s): 201a4b4
Added comments
Browse files105 unmodified lines10610710810911011111217 unmodified lines13013113213313413513613713813914014114214314414514614714814915015115215339 unmodified lines19319419519619719819920010 unmodified lines211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253105 unmodified lines"sent2": datasets.Value("string"),"annot_score": datasets.Value("float"),"gem_id": datasets.Value("string"),#"quality": datasets.Value("uint8")})17 unmodified linesdef _split_generators(self, dl_manager):"""Returns SplitGenerators."""# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archiveif self.config.quality < 70:# We need to retrieve the largest training set file# containing the full training set for the desired language_URLs["train"] = "train_{0}.60.jsonl.bz2".format(self.config.lang)elif self.config.quality <= 95:# We can do with a smaller version of the training set# for the desired language_URLs["train"] = "train_{0}.70.jsonl.bz2".format(self.config.lang)# Otherwise, if the desired quality is above 95, we do not# download any training data, because there is no matching datadata_dir = dl_manager.download_and_extract(_URLs)splits = [39 unmodified lines),]if self.config.quality <= 95:# We do have training data as wellsplits.append(datasets.SplitGenerator(name=datasets.Split.TRAIN,10 unmodified linesreturn splitsdef _generate_examples(self, lang, quality, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`):""" Yields examples as (key, example) tuples. """# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.# The `key` is here for legacy reason (tfds) and is not important in itself.if split == datasets.Split.TRAIN:with bz2.open(filepath, "rt", encoding="utf-8") as f:# We know that this file only contains the desired language,# because for the training sets the languages are in separate# files, and only the desired language has been downloadedfor id_, row in enumerate(f):data = json.loads(row)if data["quality"] < quality:# The rest of this file contains too low quality databreakyield id_, {"lang": data["lang"],"sent1": data["sent1"],"sent2": data["sent2"],"annot_score": 0.0,"gem_id": data["gem_id"],#"quality": data["quality"],}else:keep_all = (split == "validation.full" or split == "test.full")with open(filepath, encoding="utf-8") as f:for id_, row in enumerate(f):data = json.loads(row)if data["lang"] == lang:if keep_all or data["annot_score"] >= 3.0:yield id_, {"lang": data["lang"],"sent1": data["sent1"],"sent2": data["sent2"],"annot_score": data["annot_score"],"gem_id": data["gem_id"],#"quality": 100,}105 unmodified lines10610710810911011117 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416539 unmodified lines20520620720820921021121221321421521621710 unmodified lines228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283105 unmodified lines"sent2": datasets.Value("string"),"annot_score": datasets.Value("float"),"gem_id": datasets.Value("string"),})17 unmodified linesdef _split_generators(self, dl_manager):"""Returns SplitGenerators."""# This method is tasked with downloading/extracting the data# and defining the splits depending on the configuration.# Several configurations are possible (listed in# BUILDER_CONFIGS), and the configuration selected by the user# is in self.config.name, which consists of two fields# separated by a period, containing the values of# self.config.lang and self.config.quality.# Select which file of the training data contains the matching data:if self.config.quality < 70:# We need to retrieve the largest training set file# containing the full training set for the desired language_URLs["train"] = "train_{0}.60.jsonl.bz2".format(self.config.lang)elif self.config.quality <= 95:# We can do with a smaller version of the training set# for the desired language_URLs["train"] = "train_{0}.70.jsonl.bz2".format(self.config.lang)# Otherwise, if the desired quality is above 95, we do not# download any training data, because there is no matching data.# The validation and test sets are so small that we do not perform# any filtering or optimization at this stage.# dl_manager is a datasets.download.DownloadManager, which# downloads and extracts the URLs# (It can accept any type or nested list/dict and will give# back the same structure with the url replaced with path to# local files. By default the archives will be extracted and# a path to a cached folder where they are extracted is# returned instead of the archive.)data_dir = dl_manager.download_and_extract(_URLs)splits = [39 unmodified lines),]# If the desired quality value is 100, no subset of the# training set is good enough, and we only produce validation# and test sets, in order to save space and time.if self.config.quality <= 95:# In this case there is matching training data, so we produce# a train split.splits.append(datasets.SplitGenerator(name=datasets.Split.TRAIN,10 unmodified linesreturn splitsdef _generate_examples(self, lang, quality, filepath, split# method parameters are unpacked from `gen_kwargs` as given in# `_split_generators`):""" Yields examples as (key, example) tuples. """# This method handles input defined in _split_generators to# yield (key, example) tuples from the dataset.# The `key` is here for legacy reason (tfds) and is not important in itself.if split == datasets.Split.TRAIN:# Training sets are in compressed bz2 files.# They contain a field "quality" missing from the validation and test sets.# We also know that this file only contains the desired language,# because for the training sets the languages are in separate# files, and only the desired language has been downloaded.with bz2.open(filepath, "rt", encoding="utf-8") as f:for id_, row in enumerate(f):data = json.loads(row)if data["quality"] < quality:# The rest of this file contains too low quality data,# because the data is sorted best firstbreakyield id_, {"lang": data["lang"],"sent1": data["sent1"],"sent2": data["sent2"],"annot_score": 0.0, # means there is no annotation"gem_id": data["gem_id"],}else:# The validation and test sets are in jsonl files.# They contain the fields "lang" and "quality" that we filter on.# If we ask for the full sets, we will keep all data entries, also# the sentence pairs that were not considered paraphrases by the# annotators:keep_all = (split == "validation.full" or split == "test.full")with open(filepath, encoding="utf-8") as f:for id_, row in enumerate(f):data = json.loads(row)if data["lang"] == lang: # only keep desired languageif keep_all or data["annot_score"] >= 3.0:# for full sets keep all;# for standard test and validation sets, keep only# the paraphrases (annot_score >= 3.0 means "good# or mostly good example of paraphrases")yield id_, {"lang": data["lang"],"sent1": data["sent1"],"sent2": data["sent2"],"annot_score": data["annot_score"],"gem_id": data["gem_id"],}
opusparcus.py+49-19