ZhuofengLi commited on
Commit
9c6d663
·
verified ·
1 Parent(s): 238eb78

Upload folder using huggingface_hub

Browse files
goodreads_children/goodreads.md CHANGED
@@ -10,4 +10,7 @@ The Goodreads datasets consist of four datasets, specifically labeled as Goodrea
10
  Link prediction in the Goodreads dataset involves predicting potential connections between users and books. The goal is to predict whether a user will review a book.
11
 
12
  ### Node Classification
13
- Node classification tasks in the Goodreads dataset include predicting the book's category.
 
 
 
 
10
  Link prediction in the Goodreads dataset involves predicting potential connections between users and books. The goal is to predict whether a user will review a book.
11
 
12
  ### Node Classification
13
+ Node classification tasks in the Goodreads dataset include predicting the book's category.
14
+
15
+ ## Dataset Source
16
+ https://mengtingwan.github.io/data/goodreads.html
goodreads_children/raw/download_data.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import gzip
3
+ import os
4
+
5
+ # 定义文件下载链接和对应的保存路径
6
+ files = {
7
+ #'goodreads_reviews_children.json.gz': 'https://datarepo.eng.ucsd.edu/mcauley_group/gdrive/goodreads/byGenre/goodreads_reviews_children.json.gz',
8
+ 'goodreads_books_history_biography.json.gz': 'https://datarepo.eng.ucsd.edu/mcauley_group/gdrive/goodreads/byGenre/goodreads_books_history_biography.json.gz',
9
+ 'goodreads_books_comics_graphic.json.gz': 'https://datarepo.eng.ucsd.edu/mcauley_group/gdrive/goodreads/byGenre/goodreads_books_comics_graphic.json.gz',
10
+ 'goodreads_books_mystery_thriller_crime.json.gz': 'https://datarepo.eng.ucsd.edu/mcauley_group/gdrive/goodreads/byGenre/goodreads_books_mystery_thriller_crime.json.gz'
11
+ }
12
+
13
+ download_folder_name = ["../../goodreads_history/raw", "../../goodreads_comics/raw", "../../goodreads_crime/raw"]
14
+
15
+ def download_and_extract(filename, url, folder):
16
+
17
+ if not os.path.exists(folder):
18
+ os.makedirs(folder)
19
+
20
+ response = requests.get(url)
21
+ gz_path = os.path.join(folder, filename)
22
+
23
+ with open(gz_path, 'wb') as f:
24
+ f.write(response.content)
25
+
26
+ json_path = gz_path.replace('.gz', '')
27
+ with gzip.open(gz_path, 'rb') as f_in:
28
+ with open(json_path, 'wb') as f_out:
29
+ f_out.write(f_in.read())
30
+
31
+ print(f"{filename} 下载并解压到 {folder} 完成")
32
+
33
+ for folder, url in zip(download_folder_name,files.values()):
34
+ folder_name = folder
35
+ download_and_extract(os.path.basename(url), url, folder_name)
goodreads_children/raw/process_final_goodreads.ipynb ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 9,
6
+ "id": "44b09756",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import tqdm"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": 1,
16
+ "id": "2b881572b62f8ce1",
17
+ "metadata": {
18
+ "ExecuteTime": {
19
+ "end_time": "2024-10-05T02:27:58.446078900Z",
20
+ "start_time": "2024-10-05T02:27:46.539697200Z"
21
+ },
22
+ "collapsed": false
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "import json\n",
27
+ "path = \"goodreads_reviews_children.json\"\n",
28
+ "dict_edge = {} #example: 8842281e1d1347389f2ab93d60773d4d|23310161 : One of my favorite books.\n",
29
+ "dict_num_to_id = {} # reorder the node's id # TODO\n",
30
+ "edge_score = []\n",
31
+ "count = 0\n",
32
+ "with open(path) as f:\n",
33
+ " for line in f:\n",
34
+ " d = json.loads(line)\n",
35
+ " edge = d[\"user_id\"] + \"|\" + d[\"book_id\"]\n",
36
+ " dict_edge[edge] = d[\"review_text\"]\n",
37
+ " edge_score.append(d[\"rating\"])\n",
38
+ " if d[\"user_id\"] not in dict_num_to_id:\n",
39
+ " dict_num_to_id[d[\"user_id\"]] = count\n",
40
+ " count += 1\n",
41
+ " if d[\"book_id\"] not in dict_num_to_id:\n",
42
+ " dict_num_to_id[d[\"book_id\"]] = count\n",
43
+ " count += 1"
44
+ ]
45
+ },
46
+ {
47
+ "cell_type": "code",
48
+ "execution_count": 3,
49
+ "id": "c64f4d2d8949368f",
50
+ "metadata": {
51
+ "ExecuteTime": {
52
+ "end_time": "2024-10-05T02:28:48.893801500Z",
53
+ "start_time": "2024-10-05T02:28:27.239080300Z"
54
+ },
55
+ "collapsed": false
56
+ },
57
+ "outputs": [],
58
+ "source": [
59
+ "path = \"goodreads_book_genres_initial.json\"\n",
60
+ "bookid_to_label = {}\n",
61
+ "with open(path,'rb') as f:\n",
62
+ " for line in f:\n",
63
+ " d = json.loads(line)\n",
64
+ " label_list = []\n",
65
+ " for x in d[\"genres\"]:\n",
66
+ " label_list.append(x)\n",
67
+ " bookid_to_label[d[\"book_id\"]] = label_list"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 4,
73
+ "id": "8b0b220f7b0f42d2",
74
+ "metadata": {
75
+ "ExecuteTime": {
76
+ "end_time": "2024-10-05T02:28:48.915454200Z",
77
+ "start_time": "2024-10-05T02:28:48.898878300Z"
78
+ },
79
+ "collapsed": false
80
+ },
81
+ "outputs": [],
82
+ "source": [
83
+ "dict_month = {\n",
84
+ " \"1\": \"January\", \n",
85
+ " \"2\": \"February\", \n",
86
+ " \"3\": \"March\", \n",
87
+ " \"4\": \"April\", \n",
88
+ " \"5\": \"May\", \n",
89
+ " \"6\": \"June\", \n",
90
+ " \"7\": \"July\", \n",
91
+ " \"8\": \"August\", \n",
92
+ " \"9\": \"September\", \n",
93
+ " \"10\": \"October\", \n",
94
+ " \"11\": \"November\", \n",
95
+ " \"12\": \"December\"\n",
96
+ "}\n"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "code",
101
+ "execution_count": 14,
102
+ "id": "b32fd5e90ab106d",
103
+ "metadata": {
104
+ "ExecuteTime": {
105
+ "end_time": "2024-10-05T02:28:58.994242600Z",
106
+ "start_time": "2024-10-05T02:28:48.916449100Z"
107
+ },
108
+ "collapsed": false
109
+ },
110
+ "outputs": [
111
+ {
112
+ "name": "stderr",
113
+ "output_type": "stream",
114
+ "text": [
115
+ "124082it [00:06, 18574.44it/s]\n"
116
+ ]
117
+ }
118
+ ],
119
+ "source": [
120
+ "path = \"goodreads_books_children.json\"\n",
121
+ "bookid_to_text = {}\n",
122
+ "text = \"This book tittled [title] is a [format] edition published by [publisher] in [publication_month] [publication_year] about [description], consisting of [num_pages] pages.\"\n",
123
+ "with open(path,'rb') as f:\n",
124
+ " for line in tqdm.tqdm(f):\n",
125
+ " d = json.loads(line)\n",
126
+ " book_id = d[\"book_id\"]\n",
127
+ " book_text = text.replace(\"[title]\", d[\"title\"])\n",
128
+ " book_text = book_text.replace(\"[publisher]\", d[\"publisher\"])\n",
129
+ " book_text = book_text.replace(\"[format]\", d[\"format\"])\n",
130
+ " try:\n",
131
+ " book_text = book_text.replace(\"[publication_month]\", dict_month[d[\"publication_month\"]])\n",
132
+ " except:\n",
133
+ " book_text = book_text.replace(\"[publication_month]\", \"Unknown Month\")\n",
134
+ " book_text = book_text.replace(\"[publication_year]\", d[\"publication_year\"])\n",
135
+ " book_text = book_text.replace(\"[description]\", d[\"description\"])\n",
136
+ " book_text = book_text.replace(\"[num_pages]\", d[\"num_pages\"])\n",
137
+ " bookid_to_text[book_id] = book_text"
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "execution_count": 19,
143
+ "id": "5e69e274cb42bf36",
144
+ "metadata": {
145
+ "ExecuteTime": {
146
+ "end_time": "2024-10-05T02:28:59.093097Z",
147
+ "start_time": "2024-10-05T02:28:59.065287600Z"
148
+ },
149
+ "collapsed": false
150
+ },
151
+ "outputs": [],
152
+ "source": [
153
+ "edge1 = [] \n",
154
+ "edge2 = [] # edge1 edge2 are to generate edge_index\n",
155
+ "text_nodes = [None] * len(dict_num_to_id)\n",
156
+ "text_edges = []\n",
157
+ "text_node_labels = [-1] * len(dict_num_to_id)"
158
+ ]
159
+ },
160
+ {
161
+ "cell_type": "code",
162
+ "execution_count": 21,
163
+ "id": "f2adedbc870feda",
164
+ "metadata": {
165
+ "ExecuteTime": {
166
+ "end_time": "2024-10-05T02:29:16.329596100Z",
167
+ "start_time": "2024-10-05T02:29:13.594803Z"
168
+ },
169
+ "collapsed": false
170
+ },
171
+ "outputs": [
172
+ {
173
+ "name": "stderr",
174
+ "output_type": "stream",
175
+ "text": [
176
+ "100%|██████████| 734640/734640 [00:02<00:00, 363991.85it/s]\n"
177
+ ]
178
+ }
179
+ ],
180
+ "source": [
181
+ "for edge, edge_text in tqdm.tqdm(dict_edge.items()):\n",
182
+ " node1 = edge.split(\"|\")[0]\n",
183
+ " node2 = edge.split(\"|\")[1]\n",
184
+ " node1_id = int(dict_num_to_id[node1])\n",
185
+ " node2_id = int(dict_num_to_id[node2])\n",
186
+ " edge1.append(node1_id)\n",
187
+ " edge2.append(node2_id)\n",
188
+ " text_nodes[node1_id] = \"user\"\n",
189
+ " text_nodes[node2_id] = bookid_to_text[node2]\n",
190
+ " text_edges.append(edge_text)\n",
191
+ " text_node_labels[node2_id] = bookid_to_label[node2]"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": 22,
197
+ "id": "3305934f1a11caa7",
198
+ "metadata": {
199
+ "ExecuteTime": {
200
+ "end_time": "2024-10-05T02:30:27.810685400Z",
201
+ "start_time": "2024-10-05T02:30:07.522283400Z"
202
+ },
203
+ "collapsed": false
204
+ },
205
+ "outputs": [],
206
+ "source": [
207
+ "from torch_geometric.data import Data\n",
208
+ "import torch"
209
+ ]
210
+ },
211
+ {
212
+ "cell_type": "code",
213
+ "execution_count": 23,
214
+ "id": "5030fa8672f2b177",
215
+ "metadata": {
216
+ "ExecuteTime": {
217
+ "end_time": "2024-10-05T02:30:28.108006200Z",
218
+ "start_time": "2024-10-05T02:30:28.066559800Z"
219
+ },
220
+ "collapsed": false
221
+ },
222
+ "outputs": [],
223
+ "source": [
224
+ "edge_index = torch.tensor([edge1,edge2])"
225
+ ]
226
+ },
227
+ {
228
+ "cell_type": "code",
229
+ "execution_count": 25,
230
+ "id": "21085a8a04df7062",
231
+ "metadata": {
232
+ "ExecuteTime": {
233
+ "end_time": "2024-10-05T02:30:28.123004600Z",
234
+ "start_time": "2024-10-05T02:30:28.082105900Z"
235
+ },
236
+ "collapsed": false
237
+ },
238
+ "outputs": [],
239
+ "source": [
240
+ "new_data = Data(\n",
241
+ " edge_index=edge_index,\n",
242
+ " text_nodes=text_nodes,\n",
243
+ " text_edges=text_edges,\n",
244
+ " text_node_labels=text_node_labels,\n",
245
+ " edge_score=edge_score\n",
246
+ ")"
247
+ ]
248
+ },
249
+ {
250
+ "cell_type": "code",
251
+ "execution_count": 29,
252
+ "id": "0355133f",
253
+ "metadata": {},
254
+ "outputs": [],
255
+ "source": [
256
+ "new_data.edge_score = torch.tensor(edge_score, dtype=torch.long)"
257
+ ]
258
+ },
259
+ {
260
+ "cell_type": "code",
261
+ "execution_count": 30,
262
+ "id": "d39601d90a0171c5",
263
+ "metadata": {
264
+ "ExecuteTime": {
265
+ "end_time": "2024-10-05T02:31:58.346932900Z",
266
+ "start_time": "2024-10-05T02:31:57.351248600Z"
267
+ },
268
+ "collapsed": false
269
+ },
270
+ "outputs": [
271
+ {
272
+ "name": "stdout",
273
+ "output_type": "stream",
274
+ "text": [
275
+ "Data saved to ../processed/children.pkl\n"
276
+ ]
277
+ }
278
+ ],
279
+ "source": [
280
+ "import pickle\n",
281
+ "output_file_path = '../processed/children.pkl'\n",
282
+ "with open(output_file_path, 'wb') as output_file:\n",
283
+ " pickle.dump(new_data, output_file)\n",
284
+ "\n",
285
+ "print(f\"Data saved to {output_file_path}\")"
286
+ ]
287
+ }
288
+ ],
289
+ "metadata": {
290
+ "kernelspec": {
291
+ "display_name": "Python 3",
292
+ "language": "python",
293
+ "name": "python3"
294
+ },
295
+ "language_info": {
296
+ "codemirror_mode": {
297
+ "name": "ipython",
298
+ "version": 3
299
+ },
300
+ "file_extension": ".py",
301
+ "mimetype": "text/x-python",
302
+ "name": "python",
303
+ "nbconvert_exporter": "python",
304
+ "pygments_lexer": "ipython3",
305
+ "version": "3.10.12"
306
+ }
307
+ },
308
+ "nbformat": 4,
309
+ "nbformat_minor": 5
310
+ }