{ "cells": [ { "cell_type": "code", "execution_count": 9, "id": "44b09756", "metadata": {}, "outputs": [], "source": [ "import tqdm" ] }, { "cell_type": "code", "execution_count": 1, "id": "2b881572b62f8ce1", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:27:58.446078900Z", "start_time": "2024-10-05T02:27:46.539697200Z" }, "collapsed": false }, "outputs": [], "source": [ "import json\n", "path = \"goodreads_reviews_children.json\"\n", "dict_edge = {} #example: 8842281e1d1347389f2ab93d60773d4d|23310161 : One of my favorite books.\n", "dict_num_to_id = {} # reorder the node's id # TODO\n", "edge_score = []\n", "count = 0\n", "with open(path) as f:\n", " for line in f:\n", " d = json.loads(line)\n", " edge = d[\"user_id\"] + \"|\" + d[\"book_id\"]\n", " dict_edge[edge] = d[\"review_text\"]\n", " edge_score.append(d[\"rating\"])\n", " if d[\"user_id\"] not in dict_num_to_id:\n", " dict_num_to_id[d[\"user_id\"]] = count\n", " count += 1\n", " if d[\"book_id\"] not in dict_num_to_id:\n", " dict_num_to_id[d[\"book_id\"]] = count\n", " count += 1" ] }, { "cell_type": "code", "execution_count": 3, "id": "c64f4d2d8949368f", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:28:48.893801500Z", "start_time": "2024-10-05T02:28:27.239080300Z" }, "collapsed": false }, "outputs": [], "source": [ "path = \"goodreads_book_genres_initial.json\"\n", "bookid_to_label = {}\n", "with open(path,'rb') as f:\n", " for line in f:\n", " d = json.loads(line)\n", " label_list = []\n", " for x in d[\"genres\"]:\n", " label_list.append(x)\n", " bookid_to_label[d[\"book_id\"]] = label_list" ] }, { "cell_type": "code", "execution_count": 4, "id": "8b0b220f7b0f42d2", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:28:48.915454200Z", "start_time": "2024-10-05T02:28:48.898878300Z" }, "collapsed": false }, "outputs": [], "source": [ "dict_month = {\n", " \"1\": \"January\", \n", " \"2\": \"February\", \n", " \"3\": \"March\", \n", " \"4\": \"April\", \n", " \"5\": \"May\", \n", " \"6\": \"June\", \n", " \"7\": \"July\", \n", " \"8\": \"August\", \n", " \"9\": \"September\", \n", " \"10\": \"October\", \n", " \"11\": \"November\", \n", " \"12\": \"December\"\n", "}\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "b32fd5e90ab106d", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:28:58.994242600Z", "start_time": "2024-10-05T02:28:48.916449100Z" }, "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "124082it [00:06, 18574.44it/s]\n" ] } ], "source": [ "path = \"goodreads_books_children.json\"\n", "bookid_to_text = {}\n", "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", "with open(path,'rb') as f:\n", " for line in tqdm.tqdm(f):\n", " d = json.loads(line)\n", " book_id = d[\"book_id\"]\n", " book_text = text.replace(\"[title]\", d[\"title\"])\n", " book_text = book_text.replace(\"[publisher]\", d[\"publisher\"])\n", " book_text = book_text.replace(\"[format]\", d[\"format\"])\n", " try:\n", " book_text = book_text.replace(\"[publication_month]\", dict_month[d[\"publication_month\"]])\n", " except:\n", " book_text = book_text.replace(\"[publication_month]\", \"Unknown Month\")\n", " book_text = book_text.replace(\"[publication_year]\", d[\"publication_year\"])\n", " book_text = book_text.replace(\"[description]\", d[\"description\"])\n", " book_text = book_text.replace(\"[num_pages]\", d[\"num_pages\"])\n", " bookid_to_text[book_id] = book_text" ] }, { "cell_type": "code", "execution_count": 19, "id": "5e69e274cb42bf36", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:28:59.093097Z", "start_time": "2024-10-05T02:28:59.065287600Z" }, "collapsed": false }, "outputs": [], "source": [ "edge1 = [] \n", "edge2 = [] # edge1 edge2 are to generate edge_index\n", "text_nodes = [None] * len(dict_num_to_id)\n", "text_edges = []\n", "text_node_labels = [-1] * len(dict_num_to_id)" ] }, { "cell_type": "code", "execution_count": 21, "id": "f2adedbc870feda", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:29:16.329596100Z", "start_time": "2024-10-05T02:29:13.594803Z" }, "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|██████████| 734640/734640 [00:02<00:00, 363991.85it/s]\n" ] } ], "source": [ "for edge, edge_text in tqdm.tqdm(dict_edge.items()):\n", " node1 = edge.split(\"|\")[0]\n", " node2 = edge.split(\"|\")[1]\n", " node1_id = int(dict_num_to_id[node1])\n", " node2_id = int(dict_num_to_id[node2])\n", " edge1.append(node1_id)\n", " edge2.append(node2_id)\n", " text_nodes[node1_id] = \"user\"\n", " text_nodes[node2_id] = bookid_to_text[node2]\n", " text_edges.append(edge_text)\n", " text_node_labels[node2_id] = bookid_to_label[node2]" ] }, { "cell_type": "code", "execution_count": 22, "id": "3305934f1a11caa7", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:30:27.810685400Z", "start_time": "2024-10-05T02:30:07.522283400Z" }, "collapsed": false }, "outputs": [], "source": [ "from torch_geometric.data import Data\n", "import torch" ] }, { "cell_type": "code", "execution_count": 23, "id": "5030fa8672f2b177", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:30:28.108006200Z", "start_time": "2024-10-05T02:30:28.066559800Z" }, "collapsed": false }, "outputs": [], "source": [ "edge_index = torch.tensor([edge1,edge2])" ] }, { "cell_type": "code", "execution_count": 25, "id": "21085a8a04df7062", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:30:28.123004600Z", "start_time": "2024-10-05T02:30:28.082105900Z" }, "collapsed": false }, "outputs": [], "source": [ "new_data = Data(\n", " edge_index=edge_index,\n", " text_nodes=text_nodes,\n", " text_edges=text_edges,\n", " text_node_labels=text_node_labels,\n", " edge_score=edge_score\n", ")" ] }, { "cell_type": "code", "execution_count": 29, "id": "0355133f", "metadata": {}, "outputs": [], "source": [ "new_data.edge_score = torch.tensor(edge_score, dtype=torch.long)" ] }, { "cell_type": "code", "execution_count": 30, "id": "d39601d90a0171c5", "metadata": { "ExecuteTime": { "end_time": "2024-10-05T02:31:58.346932900Z", "start_time": "2024-10-05T02:31:57.351248600Z" }, "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data saved to ../processed/children.pkl\n" ] } ], "source": [ "import pickle\n", "output_file_path = '../processed/children.pkl'\n", "with open(output_file_path, 'wb') as output_file:\n", " pickle.dump(new_data, output_file)\n", "\n", "print(f\"Data saved to {output_file_path}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }