File size: 4,742 Bytes
94ba28b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd3f8bb
 
6e9a192
fd3f8bb
 
46d9432
fd3f8bb
46d9432
fd3f8bb
 
 
 
 
46d9432
fd3f8bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d9432
fd3f8bb
 
 
 
 
 
 
 
 
 
3495eca
 
 
 
 
fd3f8bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# TODO: Address all TODOs and remove all explanatory comments
"""TODO: Add a description here."""

!pip install datasets

import csv
import json
import os
from typing import List
import datasets
import logging

# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {A great new dataset},
author={huggingface, Inc.
},
year={2020}
}
"""

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """\
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://www.yelp.com/dataset/download"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""



import json
import datasets

class YelpDataset(datasets.GeneratorBasedBuilder):
    """Yelp Dataset focusing on restaurant reviews."""
    
    VERSION = datasets.Version("1.1.0")
    
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="restaurants", version=VERSION, description="This part of my dataset covers a wide range of restaurants"),
    ]

    DEFAULT_CONFIG_NAME = "restaurants"
    
    _URL = "https://yelpdata.s3.us-west-2.amazonaws.com/"
    _URLS = {
        "business": _URL + "yelp_academic_dataset_business.json",
        "review": _URL + "yelp_academic_dataset_review.json",
    }

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "business_id": datasets.Value("string"),
                    "name": datasets.Value("string"),
                    "categories": datasets.Value("string"),
                    "review_id": datasets.Value("string"),
                    "user_id": datasets.Value("string"),
                    "stars": datasets.Value("float"),
                    "text": datasets.Value("string"),
                    "date": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://www.yelp.com/dataset/download",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        """Returns SplitGenerators."""
        downloaded_files = dl_manager.download_and_extract(self._URLS)
        
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"business_path": downloaded_files["business"], "review_path": downloaded_files["review"], "split": "train"}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"business_path": downloaded_files["business"], "review_path": downloaded_files["review"], "split": "test"}),
        ]

    def _generate_examples(self, business_path, review_path, split):
        """Yields examples as (key, example) tuples."""
        
        # Load businesses and filter for restaurants
        with open(business_path, encoding="utf-8") as f:
            businesses = {}
            for line in f:
                business = json.loads(line)
                if business.get("categories") and "Restaurants" in business["categories"]:
                    businesses[business['business_id']] = business
        
        # Generate examples
        with open(review_path, encoding="utf-8") as f:
            for line in f:
                review = json.loads(line)
                business_id = review['business_id']
                if business_id in businesses:
                    yield review['review_id'], {
                        "business_id": business_id,
                        "name": businesses[business_id]['name'],
                        "categories": businesses[business_id]['categories'],
                        "review_id": review['review_id'],
                        "user_id": review['user_id'],
                        "stars": review['stars'],
                        "text": review['text'],
                        "date": review['date'],
                    }