1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
import os.path
from functools import partial
from torchtext._internal.module_utils import is_module_available
from torchtext.data.datasets_utils import (
_create_dataset_directory,
)
if is_module_available("torchdata"):
from torchdata.datapipes.iter import FileOpener, IterableWrapper
from torchtext._download_hooks import HttpReader
URL = "http://data.statmt.org/cc-100/%s.txt.xz"
VALID_CODES = {
"am",
"ar",
"as",
"az",
"be",
"bg",
"bn",
"bn_rom",
"br",
"bs",
"ca",
"cs",
"cy",
"da",
"de",
"el",
"en",
"eo",
"es",
"et",
"eu",
"fa",
"ff",
"fi",
"fr",
"fy",
"ga",
"gd",
"gl",
"gn",
"gu",
"ha",
"he",
"hi",
"hi_rom",
"hr",
"ht",
"hu",
"hy",
"id",
"ig",
"is",
"it",
"ja",
"jv",
"ka",
"kk",
"km",
"kn",
"ko",
"ku",
"ky",
"la",
"lg",
"li",
"ln",
"lo",
"lt",
"lv",
"mg",
"mk",
"ml",
"mn",
"mr",
"ms",
"my",
"my_zaw",
"ne",
"nl",
"no",
"ns",
"om",
"or",
"pa",
"pl",
"ps",
"pt",
"qu",
"rm",
"ro",
"ru",
"sa",
"si",
"sc",
"sd",
"sk",
"sl",
"so",
"sq",
"sr",
"ss",
"su",
"sv",
"sw",
"ta",
"ta_rom",
"te",
"te_rom",
"th",
"tl",
"tn",
"tr",
"ug",
"uk",
"ur",
"ur_rom",
"uz",
"vi",
"wo",
"xh",
"yi",
"yo",
"zh-Hans",
"zh-Hant",
"zu",
}
NUM_LINES = None
MD5 = None
DATASET_NAME = "CC100"
def _filepath_fn(root, url, _=None):
return os.path.join(root, os.path.basename(url))
def _decompressed_filepath_fn(root, x):
return os.path.join(root, os.path.basename(x).rstrip(".xz"))
def _modify_res(language_code, x):
return language_code, x
@_create_dataset_directory(dataset_name=DATASET_NAME)
def CC100(root: str, language_code: str = "en"):
"""CC100 Dataset
.. warning::
using datapipes is still currently subject to a few caveats. if you wish
to use this dataset with shuffling, multi-processing, or distributed
learning, please see :ref:`this note <datapipes_warnings>` for further
instructions.
For additional details refer to https://data.statmt.org/cc-100/
Args:
root: Directory where the datasets are saved. Default: os.path.expanduser('~/.torchtext/cache')
language_code: the language of the dataset
:returns: DataPipe that yields tuple of language code and text
:rtype: (str, str)
"""
if language_code not in VALID_CODES:
raise ValueError(f"Invalid language code {language_code}")
url = URL % language_code
url_dp = IterableWrapper([url])
cache_compressed_dp = url_dp.on_disk_cache(filepath_fn=partial(_filepath_fn, root, url))
cache_compressed_dp = HttpReader(cache_compressed_dp)
cache_compressed_dp = cache_compressed_dp.end_caching(mode="wb", same_filepath_fn=True)
cache_decompressed_dp = cache_compressed_dp.on_disk_cache(filepath_fn=partial(_decompressed_filepath_fn, root))
cache_decompressed_dp = FileOpener(cache_decompressed_dp, mode="b").load_from_xz()
cache_decompressed_dp = cache_decompressed_dp.end_caching(mode="wb")
data_dp = FileOpener(cache_decompressed_dp, encoding="utf-8").readlines(return_path=False)
return data_dp.map(partial(_modify_res, language_code)).shuffle().set_shuffle(False).sharding_filter()
|