File size: 6,280 Bytes
6f241aa |
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
import csv
import os
import datasets
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """
"""
_URLS = {
"clothing": "https://drive.google.com/u/0/uc?id=1HP3EPX9Q8JffUUZz2czXD7qudzvitscq&export=download",
"electronics": "https://drive.google.com/u/0/uc?id=1W50FNd0707qK1CCktEF30nlDqsImLg3X&export=download",
"office": "https://drive.google.com/u/0/uc?id=1lsttnBIjFD4nQw9idZYQNUWKSzj5VibD&export=download",
}
_FIELDS = ["date", "rating", "reviewText", "summary"]
_RATINGS = ["1", "2", "3", "4", "5"]
class AmazonConfig(datasets.BuilderConfig):
def __init__(
self,
training_files,
testing_files,
url,
label_classes=_RATINGS,
**kwargs,
):
super().__init__(version=datasets.Version("1.0.0", ""), **kwargs)
self.label_classes = label_classes
self.training_files = training_files
self.testing_files = testing_files
self.url = url
class Amazon(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
AmazonConfig(
name="clothing_majorshift01",
description="",
url=_URLS["clothing"],
training_files=[
"201011.csv",
"201012.csv",
"201101.csv",
"201102.csv",
"201103.csv",
"201104.csv",
"201105.csv",
"201106.csv",
"201107.csv",
"201108.csv",
"201109.csv",
"201110.csv",
"201111.csv",
"201112.csv",
"201201.csv",
"201202.csv",
"201203.csv",
"201204.csv",
"201205.csv",
"201206.csv",
"201207.csv",
"201208.csv",
"201209.csv",
"201210.csv",
],
testing_files=[
"201211.csv",
"201212.csv",
"201301.csv",
"201302.csv",
"201303.csv",
"201304.csv",
],
),
AmazonConfig(
name="clothing_majorshift02",
description="",
url=_URLS["clothing"],
training_files=[
"200808.csv",
"200809.csv",
"200810.csv",
"200811.csv",
"200812.csv",
"200901.csv",
"200902.csv",
"200903.csv",
"200904.csv",
"200905.csv",
"200906.csv",
"200907.csv",
"200908.csv",
"200909.csv",
"200910.csv",
"200911.csv",
"200912.csv",
"201001.csv",
"201002.csv",
"201003.csv",
"201004.csv",
"201005.csv",
"201006.csv",
"201007.csv",
],
testing_files=[
"201008.csv",
"201009.csv",
"201010.csv",
"201011.csv",
"201012.csv",
"201101.csv",
],
),
AmazonConfig(
name="clothing_majorshift03",
description="",
url=_URLS["clothing"],
training_files=[
"201602.csv",
"201603.csv",
"201604.csv",
"201605.csv",
"201606.csv",
"201607.csv",
"201608.csv",
"201609.csv",
"201610.csv",
"201611.csv",
"201612.csv",
"201701.csv",
"201702.csv",
"201703.csv",
"201704.csv",
"201705.csv",
"201706.csv",
"201707.csv",
"201708.csv",
"201709.csv",
"201710.csv",
"201711.csv",
"201712.csv",
"201801.csv",
],
testing_files=[
"201802.csv",
"201803.csv",
"201804.csv",
"201805.csv",
"201806.csv",
"201807.csv",
],
),
]
def _info(self):
features = {
"date": datasets.Value("string"),
"id": datasets.Value("int32"),
"label": datasets.features.ClassLabel(names=self.config.label_classes),
"text": datasets.Value("string"),
}
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(features),
)
def _split_generators(self, dl_manager):
dirname = dl_manager.download_and_extract(self.config.url)
logger.info(str(dirname))
category = self.config.name.split("_")[
0
] # extract category name from the config
train_filepaths = tuple(
os.path.join(dirname, category, fname)
for fname in self.config.training_files
)
test_filepaths = tuple(
os.path.join(dirname, category, fname)
for fname in self.config.testing_files
)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"filepaths": train_filepaths},
),
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"filepaths": test_filepaths},
),
]
def _generate_examples(self, filepaths):
logger.info(f"generating examples from {len(filepaths)} files")
idx = 0
for filepath in filepaths:
with open(filepath, encoding="utf-8") as f:
reader = csv.DictReader(f, fieldnames=_FIELDS)
for row in reader:
yield idx, {
"date": row["date"],
"id": idx,
"label": row["rating"],
"text": row["reviewText"],
}
idx += 1
|