Johnnyeee commited on
Commit
fd3f8bb
1 Parent(s): 46d9432

Update Yelpdata_663.py

Browse files
Files changed (1) hide show
  1. Yelpdata_663.py +68 -68
Yelpdata_663.py CHANGED
@@ -59,79 +59,79 @@ _LICENSE = "https://s3-media0.fl.yelpcdn.com/assets/srv0/engineering_pages/f64cb
59
  # TODO: Add link to the official dataset URLs here
60
  # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
61
  # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
62
- _URL = "https://yelpdata.s3.us-west-2.amazonaws.com/"
63
- _URLS = {
64
- "business": _URL + "yelp_academic_dataset_business.json",
65
- "review": _URL + "yelp_academic_dataset_review.json",
66
- }
67
- # TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
68
- class YelpDataset(datasets.GeneratorBasedBuilder):
69
- """TODO: Short description of my dataset."""
70
-
71
- _URLS = _URLS
72
- VERSION = datasets.Version("1.1.0")
73
 
74
- def _info(self):
75
- return datasets.DatasetInfo(
76
- description=_DESCRIPTION,
77
- features=datasets.Features(
78
- {
79
- "business_id": datasets.Value("string"),
80
- "name": datasets.Value("string"),
81
- "address": datasets.Value("string"),
82
- "city": datasets.Value("string"),
83
- "state": datasets.Value("string"),
84
- "postal_code": datasets.Value("string"),
85
- "latitude": datasets.Value("float"),
86
- "longitude": datasets.Value("float"),
87
- "stars_x": datasets.Value("float"),
88
- "review_count": datasets.Value("float"),
89
- "is_open": datasets.Value("float"),
90
- "categories": datasets.Value("string"),
91
- "hours": datasets.Value("string"),
92
- "review_id": datasets.Value("string"),
93
- "user_id": datasets.Value("string"),
94
- "stars_y": datasets.Value("float"),
95
- "useful": datasets.Value("float"),
96
- "funny": datasets.Value("float"),
97
- "cool": datasets.Value("float"),
98
- "text": datasets.Value("string"),
99
- "date": datasets.Value("string"),
100
- "attributes": datasets.Value("string"),
101
- }),
102
- # No default supervised_keys (as we have to pass both question
103
- # and context as input).
104
- supervised_keys=None,
105
- homepage="https://www.yelp.com/dataset/download",
106
- citation=_CITATION,
107
- )
108
-
109
-
110
- def _generate_examples(self, filepaths):
111
- logging.info("Generating examples from = %s", filepaths)
112
-
113
- # Load JSON files into pandas DataFrames
114
- business_df = pd.read_json(filepaths['business'], lines=True)
115
- review_df = pd.read_json(filepaths['review'], lines=True)
116
-
117
- # Merge DataFrames on 'business_id'
118
- merged_df = pd.merge(business_df, review_df, on='business_id')
119
 
120
- # Filter out entries where 'categories' does not contain 'Restaurants'
121
- filtered_df = merged_df[merged_df['categories'].str.contains("Restaurants", na=False)]
122
 
123
- # Convert to CSV (optional step if you need CSV output)
124
- # filtered_df.to_csv('filtered_dataset.csv', index=False)
 
 
 
125
 
126
- # Generate examples
127
- for index, row in filtered_df.iterrows():
128
- # Handle missing values for float fields
129
- for key, value in row.items():
130
- if pd.isnull(value):
131
- row[key] = None # or appropriate handling of nulls based on your requirements
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
- # Yield each row as an example
134
- yield index, row.to_dict()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
 
137
 
 
59
  # TODO: Add link to the official dataset URLs here
60
  # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
61
  # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
62
+ import json
63
+ import datasets
 
 
 
 
 
 
 
 
 
64
 
65
+ class YelpDataset(datasets.GeneratorBasedBuilder):
66
+ """Yelp Dataset focusing on restaurant reviews."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
+ VERSION = datasets.Version("1.1.0")
 
69
 
70
+ BUILDER_CONFIGS = [
71
+ datasets.BuilderConfig(name="restaurants", version=VERSION, description="This part of my dataset covers a wide range of restaurants"),
72
+ ]
73
+
74
+ DEFAULT_CONFIG_NAME = "restaurants"
75
 
76
+ _URL = "https://yelpdata.s3.us-west-2.amazonaws.com/"
77
+ _URLS = {
78
+ "business": _URL + "yelp_academic_dataset_business.json",
79
+ "review": _URL + "yelp_academic_dataset_review.json",
80
+ }
81
+
82
+ def _info(self):
83
+ return datasets.DatasetInfo(
84
+ description=_DESCRIPTION,
85
+ features=datasets.Features(
86
+ {
87
+ "business_id": datasets.Value("string"),
88
+ "name": datasets.Value("string"),
89
+ "categories": datasets.Value("string"),
90
+ "review_id": datasets.Value("string"),
91
+ "user_id": datasets.Value("string"),
92
+ "stars": datasets.Value("float"),
93
+ "text": datasets.Value("string"),
94
+ "date": datasets.Value("string"),
95
+ }
96
+ ),
97
+ supervised_keys=None,
98
+ homepage="https://www.yelp.com/dataset/download",
99
+ citation=_CITATION,
100
+ )
101
+
102
+ def _split_generators(self, dl_manager: datasets.DownloadManager):
103
+ """Returns SplitGenerators."""
104
+ downloaded_files = dl_manager.download_and_extract(self._URLS)
105
 
106
+ return [
107
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"business_path": downloaded_files["business"], "review_path": downloaded_files["review"], "split": "train"}),
108
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"business_path": downloaded_files["business"], "review_path": downloaded_files["review"], "split": "test"}),
109
+ ]
110
+
111
+ def _generate_examples(self, business_path, review_path, split):
112
+ """Yields examples as (key, example) tuples."""
113
+
114
+ # Load businesses and filter for restaurants
115
+ with open(business_path, encoding="utf-8") as f:
116
+ businesses = {line['business_id']: line for line in (json.loads(line) for line in f) if "Restaurants" in line.get("categories", "")}
117
+
118
+ # Generate examples
119
+ with open(review_path, encoding="utf-8") as f:
120
+ for line in f:
121
+ review = json.loads(line)
122
+ business_id = review['business_id']
123
+ if business_id in businesses:
124
+ yield review['review_id'], {
125
+ "business_id": business_id,
126
+ "name": businesses[business_id]['name'],
127
+ "categories": businesses[business_id]['categories'],
128
+ "review_id": review['review_id'],
129
+ "user_id": review['user_id'],
130
+ "stars": review['stars'],
131
+ "text": review['text'],
132
+ "date": review['date'],
133
+ }
134
+
135
 
136
 
137