|
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.""" |
|
|
|
|
|
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 |
|
|
|
|
|
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'], |
|
} |
|
|