Datasets:
Update Yelpdata_663.py
Browse files- Yelpdata_663.py +37 -8
Yelpdata_663.py
CHANGED
|
@@ -109,22 +109,51 @@ class YelpDataset(datasets.GeneratorBasedBuilder):
|
|
| 109 |
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": downloaded_files, "split": "test"}),
|
| 110 |
]
|
| 111 |
|
|
|
|
| 112 |
def _generate_examples(self, files, split):
|
| 113 |
-
|
| 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 = {
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
with open(review_path, encoding="utf-8") as f:
|
| 122 |
for line in 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 = {
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
if (split == 'train' and random() < 0.8) or (split == 'test' and random() >= 0.8):
|
| 130 |
yield review['review_id'], example
|
|
|
|
| 109 |
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": downloaded_files, "split": "test"}),
|
| 110 |
]
|
| 111 |
|
| 112 |
+
|
| 113 |
def _generate_examples(self, files, split):
|
| 114 |
+
"""Yields examples as (key, example) tuples."""
|
| 115 |
business_path, review_path = files["business"], files["review"]
|
| 116 |
+
|
| 117 |
+
# Load businesses and filter for restaurants
|
| 118 |
with open(business_path, encoding="utf-8") as f:
|
| 119 |
+
businesses = {}
|
| 120 |
+
for line in f:
|
| 121 |
+
business = json.loads(line)
|
| 122 |
+
# Check if 'categories' is not None and contains "Restaurants"
|
| 123 |
+
if business.get("categories") and "Restaurants" in business["categories"]:
|
| 124 |
+
businesses[business['business_id']] = business
|
| 125 |
+
|
| 126 |
+
# Generate examples with an attempted 80/20 split for train/test
|
| 127 |
with open(review_path, encoding="utf-8") as f:
|
| 128 |
for line in f:
|
| 129 |
review = json.loads(line)
|
| 130 |
business_id = review['business_id']
|
| 131 |
if business_id in businesses:
|
| 132 |
business = businesses[business_id]
|
| 133 |
+
example = {
|
| 134 |
+
"business_id": business['business_id'],
|
| 135 |
+
"name": business.get("name", ""),
|
| 136 |
+
"address": business.get("address", ""),
|
| 137 |
+
"city": business.get("city", ""),
|
| 138 |
+
"state": business.get("state", ""),
|
| 139 |
+
"postal_code": business.get("postal_code", ""),
|
| 140 |
+
"latitude": business.get("latitude", None),
|
| 141 |
+
"longitude": business.get("longitude", None),
|
| 142 |
+
"stars_x": business.get("stars", None),
|
| 143 |
+
"review_count": business.get("review_count", None),
|
| 144 |
+
"is_open": business.get("is_open", None),
|
| 145 |
+
"categories": business.get("categories", ""),
|
| 146 |
+
"hours": json.dumps(business.get("hours", {})), # Storing hours as a JSON string
|
| 147 |
+
"review_id": review.get("review_id", ""),
|
| 148 |
+
"user_id": review.get("user_id", ""),
|
| 149 |
+
"stars_y": review.get("stars", None),
|
| 150 |
+
"useful": review.get("useful", None),
|
| 151 |
+
"funny": review.get("funny", None),
|
| 152 |
+
"cool": review.get("cool", None),
|
| 153 |
+
"text": review.get("text", ""),
|
| 154 |
+
"date": review.get("date", ""),
|
| 155 |
+
"attributes": json.dumps(business.get("attributes", {})), # Storing attributes as a JSON string
|
| 156 |
+
}
|
| 157 |
+
# Randomly assign to split based on an 80/20 ratio
|
| 158 |
if (split == 'train' and random() < 0.8) or (split == 'test' and random() >= 0.8):
|
| 159 |
yield review['review_id'], example
|