hyp1231 commited on
Commit
75a50cf
1 Parent(s): 981749a

Add script for raw metadata

Browse files
Files changed (1) hide show
  1. Amazon-Review-2023.py +66 -0
Amazon-Review-2023.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import datasets
4
+
5
+
6
+ _AMAZON_REVIEW_2023_DESCRIPTION = """\
7
+ Amazon Review 2023 is an updated version of the Amazon Review 2018 dataset.
8
+ This dataset mainly includes reviews (ratings, text) and item metadata (desc-
9
+ riptions, category information, price, brand, and images). Compared to the pre-
10
+ vious versions, the 2023 version features larger size, newer reviews (up to Sep
11
+ 2023), richer and cleaner meta data, and finer-grained timestamps (from day to
12
+ milli-second).
13
+
14
+ """
15
+
16
+
17
+ class RawMetaAmazonReview2023Config(datasets.BuilderConfig):
18
+ def __init__(self, **kwargs):
19
+ super(RawMetaAmazonReview2023Config, self).__init__(**kwargs)
20
+
21
+ self.suffix = 'jsonl'
22
+ self.domain = self.name[len(f'raw_meta_'):]
23
+ self.description = f'This is a subset for items in domain: {self.domain}.'
24
+ self.data_dir = f'raw/meta_categories/meta_{self.domain}.jsonl'
25
+
26
+
27
+ class AmazonReview2023(datasets.GeneratorBasedBuilder):
28
+ BUILDER_CONFIGS = [
29
+ RawMetaAmazonReview2023Config(
30
+ name='raw_meta_All_Beauty'
31
+ )
32
+ ]
33
+
34
+ def _info(self):
35
+ return datasets.DatasetInfo(
36
+ description=_AMAZON_REVIEW_2023_DESCRIPTION + self.config.description
37
+ )
38
+
39
+ def _split_generators(self, dl_manager):
40
+ dl_dir = dl_manager.download_and_extract(self.config.data_dir)
41
+ return [
42
+ datasets.SplitGenerator(
43
+ name='full',
44
+ gen_kwargs={"filepath": dl_dir}
45
+ )
46
+ ]
47
+
48
+ def _generate_examples(self, filepath):
49
+ with open(filepath, 'r', encoding='utf-8') as file:
50
+ for idx, line in enumerate(file):
51
+ if self.config.suffix == 'jsonl':
52
+ try:
53
+ dp = json.loads(line)
54
+ """
55
+ For item metadata, 'details' is free-form structured data
56
+ Here we dump it to string to make huggingface datasets easy
57
+ to store.
58
+ """
59
+ if isinstance(self.config, RawMetaAmazonReview2023Config) and \
60
+ 'details' in dp:
61
+ dp['details'] = json.dumps(dp['details'])
62
+ except:
63
+ continue
64
+ else:
65
+ raise ValueError(f'Unknown suffix {self.config.suffix}.')
66
+ yield idx, dp