Datasets:
Update Yelpdata_663.py
Browse files- Yelpdata_663.py +39 -34
Yelpdata_663.py
CHANGED
|
@@ -46,17 +46,17 @@ _HOMEPAGE = "https://www.yelp.com/dataset/download"
|
|
| 46 |
_LICENSE = ""
|
| 47 |
|
| 48 |
|
| 49 |
-
|
| 50 |
import json
|
| 51 |
import datasets
|
|
|
|
| 52 |
|
| 53 |
class YelpDataset(datasets.GeneratorBasedBuilder):
|
| 54 |
-
"""Yelp Dataset focusing on restaurant reviews."""
|
| 55 |
|
| 56 |
VERSION = datasets.Version("1.1.0")
|
| 57 |
|
| 58 |
BUILDER_CONFIGS = [
|
| 59 |
-
datasets.BuilderConfig(name="restaurants", version=VERSION, description="This part of
|
| 60 |
]
|
| 61 |
|
| 62 |
DEFAULT_CONFIG_NAME = "restaurants"
|
|
@@ -70,21 +70,34 @@ class YelpDataset(datasets.GeneratorBasedBuilder):
|
|
| 70 |
def _info(self):
|
| 71 |
return datasets.DatasetInfo(
|
| 72 |
description=_DESCRIPTION,
|
| 73 |
-
features=datasets.Features(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
supervised_keys=None,
|
| 86 |
homepage="https://www.yelp.com/dataset/download",
|
| 87 |
citation=_CITATION,
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
|
@@ -92,20 +105,17 @@ class YelpDataset(datasets.GeneratorBasedBuilder):
|
|
| 92 |
downloaded_files = dl_manager.download_and_extract(self._URLS)
|
| 93 |
|
| 94 |
return [
|
| 95 |
-
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"
|
| 96 |
-
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"
|
| 97 |
]
|
| 98 |
|
| 99 |
-
def _generate_examples(self,
|
| 100 |
"""Yields examples as (key, example) tuples."""
|
|
|
|
| 101 |
|
| 102 |
-
# Load businesses
|
| 103 |
with open(business_path, encoding="utf-8") as f:
|
| 104 |
-
businesses = {}
|
| 105 |
-
for line in f:
|
| 106 |
-
business = json.loads(line)
|
| 107 |
-
if business.get("categories") and "Restaurants" in business["categories"]:
|
| 108 |
-
businesses[business['business_id']] = business
|
| 109 |
|
| 110 |
# Generate examples
|
| 111 |
with open(review_path, encoding="utf-8") as f:
|
|
@@ -113,13 +123,8 @@ class YelpDataset(datasets.GeneratorBasedBuilder):
|
|
| 113 |
review = json.loads(line)
|
| 114 |
business_id = review['business_id']
|
| 115 |
if business_id in businesses:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
"user_id": review['user_id'],
|
| 122 |
-
"stars": review['stars'],
|
| 123 |
-
"text": review['text'],
|
| 124 |
-
"date": review['date'],
|
| 125 |
-
}
|
|
|
|
| 46 |
_LICENSE = ""
|
| 47 |
|
| 48 |
|
|
|
|
| 49 |
import json
|
| 50 |
import datasets
|
| 51 |
+
from random import random
|
| 52 |
|
| 53 |
class YelpDataset(datasets.GeneratorBasedBuilder):
|
| 54 |
+
"""Yelp Dataset focusing on restaurant reviews and business information."""
|
| 55 |
|
| 56 |
VERSION = datasets.Version("1.1.0")
|
| 57 |
|
| 58 |
BUILDER_CONFIGS = [
|
| 59 |
+
datasets.BuilderConfig(name="restaurants", version=VERSION, description="This part of the dataset covers a wide range of restaurants"),
|
| 60 |
]
|
| 61 |
|
| 62 |
DEFAULT_CONFIG_NAME = "restaurants"
|
|
|
|
| 70 |
def _info(self):
|
| 71 |
return datasets.DatasetInfo(
|
| 72 |
description=_DESCRIPTION,
|
| 73 |
+
features=datasets.Features({
|
| 74 |
+
"business_id": datasets.Value("string"),
|
| 75 |
+
"name": datasets.Value("string"),
|
| 76 |
+
"address": datasets.Value("string"),
|
| 77 |
+
"city": datasets.Value("string"),
|
| 78 |
+
"state": datasets.Value("string"),
|
| 79 |
+
"postal_code": datasets.Value("string"),
|
| 80 |
+
"latitude": datasets.Value("float"),
|
| 81 |
+
"longitude": datasets.Value("float"),
|
| 82 |
+
"stars_x": datasets.Value("float"),
|
| 83 |
+
"review_count": datasets.Value("float"),
|
| 84 |
+
"is_open": datasets.Value("float"),
|
| 85 |
+
"categories": datasets.Value("string"),
|
| 86 |
+
"hours": datasets.Value("string"),
|
| 87 |
+
"review_id": datasets.Value("string"),
|
| 88 |
+
"user_id": datasets.Value("string"),
|
| 89 |
+
"stars_y": datasets.Value("float"),
|
| 90 |
+
"useful": datasets.Value("float"),
|
| 91 |
+
"funny": datasets.Value("float"),
|
| 92 |
+
"cool": datasets.Value("float"),
|
| 93 |
+
"text": datasets.Value("string"),
|
| 94 |
+
"date": datasets.Value("string"),
|
| 95 |
+
"attributes": datasets.Value("string"),
|
| 96 |
+
}),
|
| 97 |
supervised_keys=None,
|
| 98 |
homepage="https://www.yelp.com/dataset/download",
|
| 99 |
citation=_CITATION,
|
| 100 |
+
license=_LICENSE,
|
| 101 |
)
|
| 102 |
|
| 103 |
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
|
|
|
| 105 |
downloaded_files = dl_manager.download_and_extract(self._URLS)
|
| 106 |
|
| 107 |
return [
|
| 108 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": downloaded_files, "split": "train"}),
|
| 109 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": downloaded_files, "split": "test"}),
|
| 110 |
]
|
| 111 |
|
| 112 |
+
def _generate_examples(self, files, split):
|
| 113 |
"""Yields examples as (key, example) tuples."""
|
| 114 |
+
business_path, review_path = files["business"], files["review"]
|
| 115 |
|
| 116 |
+
# Load businesses
|
| 117 |
with open(business_path, encoding="utf-8") as f:
|
| 118 |
+
businesses = {line['business_id']: line for line in (json.loads(line) for line in f) if "Restaurants" in line.get("categories", "")}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Generate examples
|
| 121 |
with open(review_path, encoding="utf-8") as f:
|
|
|
|
| 123 |
review = json.loads(line)
|
| 124 |
business_id = review['business_id']
|
| 125 |
if business_id in businesses:
|
| 126 |
+
business = businesses[business_id]
|
| 127 |
+
example = {**business, **review} # Merge business and review details
|
| 128 |
+
# Randomly assign to split based on an 80/20 ratio
|
| 129 |
+
if (split == 'train' and random() < 0.8) or (split == 'test' and random() >= 0.8):
|
| 130 |
+
yield review['review_id'], example
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|