albertvillanova HF staff commited on
Commit
6d98a07
1 Parent(s): 791c5c3

Support streaming xtreme dataset for PAN-X config (#4135)

Browse files

Commit from https://github.com/huggingface/datasets/commit/9977ade72191ff0b6907ec63935448c6269a91a1

Files changed (1) hide show
  1. xtreme.py +61 -51
xtreme.py CHANGED
@@ -471,32 +471,12 @@ class Xtreme(datasets.GeneratorBasedBuilder):
471
  )
472
  if self.config.name.startswith("PAWS-X"):
473
  features = PawsxParser.features
474
- if self.config.name == "XNLI":
475
  features["gold_label"] = datasets.Value("string")
476
-
477
- if self.config.name.startswith("udpos"):
478
  features = UdposParser.features
479
-
480
- if self.config.name.startswith("PAN-X"):
481
- features = datasets.Features(
482
- {
483
- "tokens": datasets.Sequence(datasets.Value("string")),
484
- "ner_tags": datasets.Sequence(
485
- datasets.features.ClassLabel(
486
- names=[
487
- "O",
488
- "B-PER",
489
- "I-PER",
490
- "B-ORG",
491
- "I-ORG",
492
- "B-LOC",
493
- "I-LOC",
494
- ]
495
- )
496
- ),
497
- "langs": datasets.Sequence(datasets.Value("string")),
498
- }
499
- )
500
  return datasets.DatasetInfo(
501
  # This is the description that will appear on the datasets page.
502
  description=self.config.description + "\n" + _DESCRIPTION,
@@ -642,27 +622,7 @@ class Xtreme(datasets.GeneratorBasedBuilder):
642
  ]
643
 
644
  if self.config.name.startswith("PAN-X"):
645
- panx_dl_dir = dl_manager.download_and_extract(self.config.data_url)
646
- lang = self.config.name.split(".")[1]
647
- lang_folder = dl_manager.extract(os.path.join(panx_dl_dir, lang + ".tar.gz"))
648
-
649
- return [
650
- datasets.SplitGenerator(
651
- name=datasets.Split.VALIDATION,
652
- # These kwargs will be passed to _generate_examples
653
- gen_kwargs={"filepath": os.path.join(lang_folder, "dev")},
654
- ),
655
- datasets.SplitGenerator(
656
- name=datasets.Split.TEST,
657
- # These kwargs will be passed to _generate_examples
658
- gen_kwargs={"filepath": os.path.join(lang_folder, "test")},
659
- ),
660
- datasets.SplitGenerator(
661
- name=datasets.Split.TRAIN,
662
- # These kwargs will be passed to _generate_examples
663
- gen_kwargs={"filepath": os.path.join(lang_folder, "train")},
664
- ),
665
- ]
666
 
667
  def _generate_examples(self, filepath=None, **kwargs):
668
  """Yields examples."""
@@ -784,20 +744,70 @@ class Xtreme(datasets.GeneratorBasedBuilder):
784
  if self.config.name.startswith("udpos"):
785
  yield from UdposParser.generate_examples(config=self.config, filepath=filepath, **kwargs)
786
  if self.config.name.startswith("PAN-X"):
787
- guid_index = 1
788
- with open(filepath, encoding="utf-8") as f:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
789
  tokens = []
790
  ner_tags = []
791
  langs = []
792
- for line in f:
 
793
  if line == "" or line == "\n":
794
  if tokens:
795
- yield guid_index, {
796
  "tokens": tokens,
797
  "ner_tags": ner_tags,
798
  "langs": langs,
799
  }
800
- guid_index += 1
801
  tokens = []
802
  ner_tags = []
803
  langs = []
@@ -813,7 +823,7 @@ class Xtreme(datasets.GeneratorBasedBuilder):
813
  # examples have no label in test set
814
  ner_tags.append("O")
815
  if tokens:
816
- yield guid_index, {
817
  "tokens": tokens,
818
  "ner_tags": ner_tags,
819
  "langs": langs,
 
471
  )
472
  if self.config.name.startswith("PAWS-X"):
473
  features = PawsxParser.features
474
+ elif self.config.name == "XNLI":
475
  features["gold_label"] = datasets.Value("string")
476
+ elif self.config.name.startswith("udpos"):
 
477
  features = UdposParser.features
478
+ elif self.config.name.startswith("PAN-X"):
479
+ features = PanxParser.features
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  return datasets.DatasetInfo(
481
  # This is the description that will appear on the datasets page.
482
  description=self.config.description + "\n" + _DESCRIPTION,
 
622
  ]
623
 
624
  if self.config.name.startswith("PAN-X"):
625
+ return PanxParser.split_generators(dl_manager=dl_manager, config=self.config)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626
 
627
  def _generate_examples(self, filepath=None, **kwargs):
628
  """Yields examples."""
 
744
  if self.config.name.startswith("udpos"):
745
  yield from UdposParser.generate_examples(config=self.config, filepath=filepath, **kwargs)
746
  if self.config.name.startswith("PAN-X"):
747
+ yield from PanxParser.generate_examples(filepath=filepath, **kwargs)
748
+
749
+
750
+ class PanxParser:
751
+
752
+ features = datasets.Features(
753
+ {
754
+ "tokens": datasets.Sequence(datasets.Value("string")),
755
+ "ner_tags": datasets.Sequence(
756
+ datasets.features.ClassLabel(
757
+ names=[
758
+ "O",
759
+ "B-PER",
760
+ "I-PER",
761
+ "B-ORG",
762
+ "I-ORG",
763
+ "B-LOC",
764
+ "I-LOC",
765
+ ]
766
+ )
767
+ ),
768
+ "langs": datasets.Sequence(datasets.Value("string")),
769
+ }
770
+ )
771
+
772
+ @staticmethod
773
+ def split_generators(dl_manager=None, config=None):
774
+ data_dir = dl_manager.download_and_extract(config.data_url)
775
+ lang = config.name.split(".")[1]
776
+ archive = os.path.join(data_dir, lang + ".tar.gz")
777
+ split_filenames = {
778
+ datasets.Split.TRAIN: "train",
779
+ datasets.Split.VALIDATION: "dev",
780
+ datasets.Split.TEST: "test",
781
+ }
782
+ return [
783
+ datasets.SplitGenerator(
784
+ name=split,
785
+ gen_kwargs={
786
+ "filepath": dl_manager.iter_archive(archive),
787
+ "filename": split_filenames[split],
788
+ },
789
+ )
790
+ for split in split_filenames
791
+ ]
792
+
793
+ @staticmethod
794
+ def generate_examples(filepath=None, filename=None):
795
+ idx = 1
796
+ for path, file in filepath:
797
+ if path.endswith(filename):
798
  tokens = []
799
  ner_tags = []
800
  langs = []
801
+ for line in file:
802
+ line = line.decode("utf-8")
803
  if line == "" or line == "\n":
804
  if tokens:
805
+ yield idx, {
806
  "tokens": tokens,
807
  "ner_tags": ner_tags,
808
  "langs": langs,
809
  }
810
+ idx += 1
811
  tokens = []
812
  ner_tags = []
813
  langs = []
 
823
  # examples have no label in test set
824
  ner_tags.append("O")
825
  if tokens:
826
+ yield idx, {
827
  "tokens": tokens,
828
  "ner_tags": ner_tags,
829
  "langs": langs,