|
| 1 | +# Copyright 2026 The Cornac Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================ |
| 15 | +""" |
| 16 | +Amazon Product Review datasets. |
| 17 | +
|
| 18 | +There are three versions: '2014', '2018', and '2023' available. |
| 19 | +'2014' is the version used in the Semantic-ID literature (e.g., TIGER). |
| 20 | +
|
| 21 | +Source: https://cseweb.ucsd.edu/~jmcauley/datasets/amazon/links.html |
| 22 | +""" |
| 23 | + |
| 24 | +import gzip |
| 25 | +import json |
| 26 | +import os |
| 27 | +from typing import List |
| 28 | + |
| 29 | +from ..data import Reader |
| 30 | +from ..utils import cache |
| 31 | + |
| 32 | +# category -> reviews_<cat>_5.json.gz |
| 33 | +_CATEGORY_FILES = { |
| 34 | + "beauty": "Beauty", |
| 35 | + "sports": "Sports_and_Outdoors", |
| 36 | + "toys": "Toys_and_Games", |
| 37 | +} |
| 38 | + |
| 39 | +_BASE_URL = "https://snap.stanford.edu/data/amazon/productGraph/categoryFiles" |
| 40 | + |
| 41 | + |
| 42 | +def _preprocess(gz_path: str, csv_path: str) -> None: |
| 43 | + """Parse the raw 5-core reviews into ``user,item,rating,timestamp`` rows. |
| 44 | +
|
| 45 | + Only mechanical cleaning is applied (drop rows with a missing field); |
| 46 | + rows are sorted chronologically per user so downstream sequential builders |
| 47 | + receive time-ordered sessions. |
| 48 | + """ |
| 49 | + rows = [] |
| 50 | + with gzip.open(gz_path, "rt", encoding="utf-8") as f: |
| 51 | + for line in f: |
| 52 | + r = json.loads(line) |
| 53 | + user = r.get("reviewerID") |
| 54 | + item = r.get("asin") |
| 55 | + rating = r.get("overall") |
| 56 | + timestamp = r.get("unixReviewTime") |
| 57 | + if user is None or item is None or rating is None or timestamp is None: |
| 58 | + continue |
| 59 | + rows.append((user, item, float(rating), int(timestamp))) |
| 60 | + |
| 61 | + rows.sort(key=lambda x: (x[0], x[3])) # (user, timestamp) |
| 62 | + |
| 63 | + with open(csv_path, "w") as f: |
| 64 | + for user, item, rating, timestamp in rows: |
| 65 | + f.write(f"{user},{item},{rating},{timestamp}\n") |
| 66 | + |
| 67 | + |
| 68 | +def load_feedback(category: str, version: str = "2014", fmt: str = "UIRT", reader: Reader = None) -> List: |
| 69 | + """Load the user-item review feedback, chronologically ordered per user. |
| 70 | +
|
| 71 | + Parameters |
| 72 | + ---------- |
| 73 | + category: str, required |
| 74 | + One of ``'beauty'``, ``'sports'``, ``'toys'`` -- the three categories |
| 75 | + used by TIGER and subsequent Semantic-ID papers. |
| 76 | +
|
| 77 | + version: str, default: '2014' |
| 78 | + Dataset version. Only ``'2014'`` (McAuley 5-core) is currently supported; |
| 79 | + 2018 and 2023 are available, but 2014 is the version used throughout the Semantic-ID literature. |
| 80 | +
|
| 81 | + fmt: str, default: 'UIRT' |
| 82 | + Data format; the underlying file has user, item, rating, and timestamp |
| 83 | + columns, so ``'UIR'`` and ``'UI'`` are also valid. |
| 84 | +
|
| 85 | + reader: `obj:cornac.data.Reader`, default: None |
| 86 | + Reader object used to read the data. |
| 87 | +
|
| 88 | + Returns |
| 89 | + ------- |
| 90 | + data: array-like |
| 91 | + Data in the form of a list of tuples (user, item, rating, timestamp). |
| 92 | + """ |
| 93 | + if category not in _CATEGORY_FILES: |
| 94 | + raise ValueError(f"category='{category}' not supported; " f"choose one of {sorted(_CATEGORY_FILES)}") |
| 95 | + if version != "2014": |
| 96 | + raise ValueError(f"version='{version}' not supported; only '2014' (McAuley 5-core) " "is available") |
| 97 | + |
| 98 | + stem = _CATEGORY_FILES[category] |
| 99 | + gz_path = cache( |
| 100 | + url=f"{_BASE_URL}/reviews_{stem}_5.json.gz", |
| 101 | + relative_path=f"amazon_review/{category}_{version}.json.gz", |
| 102 | + ) |
| 103 | + csv_path = f"{gz_path[:-len('.json.gz')]}.csv" |
| 104 | + if not os.path.exists(csv_path): |
| 105 | + _preprocess(gz_path, csv_path) |
| 106 | + |
| 107 | + reader = Reader() if reader is None else reader |
| 108 | + return reader.read(csv_path, fmt=fmt, sep=",") |
0 commit comments