albertvillanova HF Staff commited on
Commit
b35d406
·
verified ·
1 Parent(s): cf90b5b

Parallelize data downloading

Browse files
Files changed (1) hide show
  1. wikipedia.py +23 -11
wikipedia.py CHANGED
@@ -1039,7 +1039,10 @@ CAT_ALIASES = {
1039
  "zh-yue": ["分类", "分類", "类", "類"],
1040
  }
1041
 
1042
- _BASE_URL_TMPL = "https://dumps.wikimedia.org/{lang}wiki/{date}/"
 
 
 
1043
  _INFO_FILE = "dumpstatus.json"
1044
 
1045
 
@@ -1049,14 +1052,19 @@ _VERSION = datasets.Version("4.0.0", "")
1049
  class WikipediaConfig(datasets.BuilderConfig):
1050
  """BuilderConfig for Wikipedia."""
1051
 
1052
- def __init__(self, language=None, date=None, version=_VERSION, **kwargs):
1053
  """BuilderConfig for Wikipedia.
1054
 
1055
  Args:
1056
- language: string, the language code for the Wikipedia dump to use.
1057
- date: string, date of the Wikipedia dump in YYYYMMDD format. A list of
1058
- available dates can be found at https://dumps.wikimedia.org/enwiki/.
1059
- **kwargs: keyword arguments forwarded to super.
 
 
 
 
 
1060
  """
1061
  super().__init__(
1062
  name=f"{date}.{language}",
@@ -1066,6 +1074,7 @@ class WikipediaConfig(datasets.BuilderConfig):
1066
  )
1067
  self.date = date
1068
  self.language = language
 
1069
 
1070
 
1071
  class Wikipedia(datasets.GeneratorBasedBuilder):
@@ -1090,13 +1099,15 @@ class Wikipedia(datasets.GeneratorBasedBuilder):
1090
  )
1091
 
1092
  def _get_base_url(self):
1093
- return _BASE_URL_TMPL.format(lang=self.config.language.replace("-", "_"), date=self.config.date)
 
 
1094
 
1095
  def _split_generators(self, dl_manager):
1096
  # Download dump status info file
1097
  info_url = self._get_base_url() + _INFO_FILE
1098
- downloaded_files = dl_manager.download_and_extract({"info": info_url})
1099
- with open(downloaded_files["info"], encoding="utf-8") as f:
1100
  dump_info = json.load(f)
1101
  multistream_dump_info = dump_info["jobs"]["articlesmultistreamdump"]
1102
  if multistream_dump_info["status"] != "done":
@@ -1115,11 +1126,12 @@ class Wikipedia(datasets.GeneratorBasedBuilder):
1115
  elif ".xml" in fname:
1116
  total_bytes += info["size"]
1117
  xml_urls.append(self._get_base_url() + fname)
1118
- downloaded_paths = dl_manager.download({"index": sorted(index_urls), "xml": sorted(xml_urls)})
 
1119
 
1120
  # Parallelize over concatenated multiple compressed streams (with 100 pages each)
1121
  filepaths, starts, ends = [], [], []
1122
- for index_path, xml_path in zip(downloaded_paths["index"], downloaded_paths["xml"]):
1123
  index = _extract_index(index_path)
1124
  for start, end in _pairwise(index):
1125
  filepaths.append(xml_path)
 
1039
  "zh-yue": ["分类", "分類", "类", "類"],
1040
  }
1041
 
1042
+ # Note that Wikimedia servers have rate limited downloaders and they are capping the number of per-ip connections to 2
1043
+ # Their mirror sites do not have this cap: https://dumps.wikimedia.org/mirrors.html
1044
+ _HOST = "https://dumps.wikimedia.org"
1045
+ _URL_PATH_SEGMENT = "/{lang}wiki/{date}/"
1046
  _INFO_FILE = "dumpstatus.json"
1047
 
1048
 
 
1052
  class WikipediaConfig(datasets.BuilderConfig):
1053
  """BuilderConfig for Wikipedia."""
1054
 
1055
+ def __init__(self, language=None, date=None, host=_HOST, version=_VERSION, **kwargs):
1056
  """BuilderConfig for Wikipedia.
1057
 
1058
  Args:
1059
+ language (str): Language code for the Wikipedia dump to use.
1060
+ date (str): Date of the Wikipedia dump in YYYYMMDD format. A list of
1061
+ available dates can be found at https://dumps.wikimedia.org/enwiki/.
1062
+ host (str, defaults to 'https://dumps.wikimedia.org'): URL of the server that hosts the Wikipedia dump.
1063
+ It defaults to the official Wikimedia host, but this has rate limited downloaders and is capping the
1064
+ number of per-IP connections to 2.
1065
+ To parallelize data download, use a mirror: https://dumps.wikimedia.org/mirrors.html
1066
+
1067
+ **kwargs: Keyword arguments forwarded to super.
1068
  """
1069
  super().__init__(
1070
  name=f"{date}.{language}",
 
1074
  )
1075
  self.date = date
1076
  self.language = language
1077
+ self.host = host.rstrip("/")
1078
 
1079
 
1080
  class Wikipedia(datasets.GeneratorBasedBuilder):
 
1099
  )
1100
 
1101
  def _get_base_url(self):
1102
+ return self.config.host + _URL_PATH_SEGMENT.format(
1103
+ lang=self.config.language.replace("-", "_"), date=self.config.date
1104
+ )
1105
 
1106
  def _split_generators(self, dl_manager):
1107
  # Download dump status info file
1108
  info_url = self._get_base_url() + _INFO_FILE
1109
+ info_path = dl_manager.download_and_extract(info_url)
1110
+ with open(info_path, encoding="utf-8") as f:
1111
  dump_info = json.load(f)
1112
  multistream_dump_info = dump_info["jobs"]["articlesmultistreamdump"]
1113
  if multistream_dump_info["status"] != "done":
 
1126
  elif ".xml" in fname:
1127
  total_bytes += info["size"]
1128
  xml_urls.append(self._get_base_url() + fname)
1129
+ data_urls = [*zip(sorted(index_urls), sorted(xml_urls))] # Parallelize data downloading
1130
+ data_paths = dl_manager.download(data_urls)
1131
 
1132
  # Parallelize over concatenated multiple compressed streams (with 100 pages each)
1133
  filepaths, starts, ends = [], [], []
1134
+ for index_path, xml_path in data_paths:
1135
  index = _extract_index(index_path)
1136
  for start, end in _pairwise(index):
1137
  filepaths.append(xml_path)