File size: 16,304 Bytes
0e490a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"id": "2b881572b62f8ce1",
"metadata": {
"ExecuteTime": {
"end_time": "2024-10-09T13:27:12.237540800Z",
"start_time": "2024-10-09T13:26:27.495918500Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"import json\n",
"path = \"Movies_and_TV_5.json\"\n",
"dict_edge = {} #example: 8842281e1d1347389f2ab93d60773d4d|23310161 : One of my favorite books.\n",
"dict_num_to_id = {} # reorder the node's id\n",
"edge_score = []\n",
"count = 0\n",
"review_text = \"Reviewer [reviewerName] left a review on [reviewTime], giving the product [rating] stars. In his/her review, he/she wrote: [reviewText]. His/Her summary was [summary].\"\n",
"with open(path) as f:\n",
" for line in f:\n",
" d = json.loads(line)\n",
" edge = d[\"reviewerID\"] + \"|\" + d[\"asin\"]\n",
" try:\n",
" reviewtext = review_text.replace(\"[reviewerName]\", d[\"reviewerName\"])\n",
" except:\n",
" reviewtext = review_text.replace(\"[reviewerName]\", \"\")\n",
" if d[\"reviewTime\"] == \"\":\n",
" reviewtext = reviewtext.replace(\"[reviewTime]\", \"Unknown reviewtime\")\n",
" else:\n",
" reviewtext = reviewtext.replace(\"[reviewTime]\", d[\"reviewTime\"])\n",
" if d[\"overall\"] == \"\":\n",
" reviewtext = reviewtext.replace(\"[rating]\", \"Unknown\")\n",
" else:\n",
" reviewtext = reviewtext.replace(\"[rating]\", str(d[\"overall\"]))\n",
" reviewtext = reviewtext.replace(\"[reviewText]\", d[\"reviewText\"])\n",
" if d[\"summary\"] == \"\":\n",
" reviewtext = reviewtext.replace(\"[summary]\", \"Unknown\")\n",
" else:\n",
" reviewtext = reviewtext.replace(\"[summary]\", d[\"summary\"])\n",
" dict_edge[edge] = reviewtext\n",
" edge_score.append(d[\"overall\"])\n",
" if d[\"reviewerID\"] not in dict_num_to_id: # user node \n",
" dict_num_to_id[d[\"reviewerID\"]] = count\n",
" count += 1\n",
" if d[\"asin\"] not in dict_num_to_id: # goods node\n",
" dict_num_to_id[d[\"asin\"]] = count\n",
" count += 1\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "acb9e595af870544",
"metadata": {
"ExecuteTime": {
"start_time": "2024-10-09T13:27:12.279999300Z"
},
"collapsed": false,
"is_executing": true
},
"outputs": [],
"source": [
"import json\n",
"dict_id_to_text = {}\n",
"dictid_to_label = {}\n",
"nodes_texts = \"The product titled '[title]'. It features [feature] and is about [description], making it an excellent choice for [fit]. This product is priced at [price] and comes from the brand [brand]. It ranks [rank] and was released on [date].\"\n",
"# nodes_texts = \"The product titled '[title]' falls under the Movies & TV category. It features [feature] and is about [description], making it a great fit for [fit]. This product is sold for [price] and is from the brand [brand]. This product has a rank of [rank] and was released on [date]. For more details, check out the [imageURL] or the high-resolution image [imageURLHighRes].\"\n",
"with open(\"meta_Movies_and_TV.json\") as f:\n",
" for line in f:\n",
" d = json.loads(line)\n",
" label_list = []\n",
" for x in d[\"category\"]:\n",
" label_list.append(x)\n",
" dictid_to_label[d[\"asin\"]] = label_list\n",
" product_text = nodes_texts.replace(\"[title]\", d[\"title\"])\n",
" category_text = \", \".join(label_list[1:])\n",
" product_text = product_text.replace(\"[category]\", category_text)\n",
" if d[\"feature\"] == []:\n",
" product_text = product_text.replace(\"[feature]\",\"Unknown feature\")\n",
" else:\n",
" feature_text = \", \".join(d[\"feature\"])\n",
" product_text = product_text.replace(\"[feature]\",feature_text)\n",
" if d[\"description\"] == []:\n",
" product_text = product_text.replace(\"[description]\",\"Unknown description\")\n",
" else:\n",
" description_text = \", \".join(d[\"description\"])\n",
" product_text = product_text.replace(\"[description]\",description_text)\n",
" if d[\"fit\"] == \"\":\n",
" product_text = product_text.replace(\"[fit]\",\"Unknown fit\")\n",
" else:\n",
" product_text = product_text.replace(\"[fit]\",d[\"fit\"])\n",
" if d[\"price\"] == \"\" or d[\"price\"][0] != \"$\":\n",
" product_text = product_text.replace(\"[price]\",\"Unknown price\")\n",
" else:\n",
" product_text = product_text.replace(\"[price]\",d[\"price\"])\n",
" if d[\"brand\"] == \"\":\n",
" product_text = product_text.replace(\"[brand]\",\"Unknown brand\")\n",
" else:\n",
" product_text = product_text.replace(\"[brand]\",d[\"brand\"])\n",
" if d[\"rank\"] == \"\":\n",
" product_text = product_text.replace(\"[rank]\",\"Unknown rank\")\n",
" else:\n",
" try:\n",
" product_text = product_text.replace(\"[rank]\",d[\"rank\"])\n",
" product_text = product_text.replace(\"in Movies & TV (\",\"\")\n",
" except:\n",
" product_text = product_text.replace(\"[rank]\",\"Unknown rank\")\n",
" if d[\"date\"] == \"\":\n",
" product_text = product_text.replace(\"[date]\",\"Unknown date\")\n",
" else:\n",
" product_text = product_text.replace(\"[date]\",d[\"date\"])\n",
" if d[\"imageURL\"] == []:\n",
" product_text = product_text.replace(\"[imageURL]\",\"Unknown imageURL\")\n",
" else:\n",
" imageURL_text = \", \".join(d[\"imageURL\"])\n",
" product_text = product_text.replace(\"[imageURL]\",imageURL_text)\n",
" if d[\"imageURLHighRes\"] == []:\n",
" product_text = product_text.replace(\"[imageURLHighRes]\",\"Unknown imageURLHighRes\")\n",
" else:\n",
" imageURLHighRes_text = \", \".join(d[\"imageURLHighRes\"])\n",
" product_text = product_text.replace(\"[imageURLHighRes]\",imageURLHighRes_text)\n",
" dict_id_to_text[d[\"asin\"]] = product_text"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "5e69e274cb42bf36",
"metadata": {
"collapsed": false,
"is_executing": true
},
"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": null,
"id": "388c334a",
"metadata": {},
"outputs": [],
"source": [
"\"The product titled 'An American Christmas Carol VHS'. It features Unknown feature and is about In Depression-era New England, a miserly businessman named Benedict Slade receives a long-overdue attitude adjustment one Christmas eve when he is visited by three ghostly figures who resemble three of the people whose possessions Slade had seized to collect on unpaid loans. Assuming the roles of the Ghosts of Christmas Past, Present, and Future from Charles Dickens' classic story, the three apparitions force Slade to face the consequences of his skinflint ways, and he becomes a caring, generous, amiable man., making it an excellent choice for Unknown fit. This product is priced at .a-box-inner{background-color:#fff}#alohaBuyBoxWidget .selected{background-color:#fffbf3;border-color:#e77600;box-shadow:0 0 3px rgba(228,121,17,.5)}#alohaBuyBoxWidget .contract-not-available{color:gray}#aloha-cart-popover .aloha-cart{height:auto;overflow:hidden}#aloha-cart-popover #aloha-cartInfo{float:left}#aloha-cart-popover #aloha-cart-details{float:right;margin-top:1em}#aloha-cart-popover .deviceContainer{width:160px;float:left;padding-right:10px;border-right:1px solid #ddd}#aloha-cart-popover li:last-child{border-right:0}#aloha-cart-popover .aloha-device-title{height:3em;overflow:hidden}#aloha-cart-popover .aloha-thumbnail-container{height:100px;margin-bottom:1em;text-align:center}#aloha-cart-popover .aloha-price-container{text-align:center}#aloha-cart-popover .aloha-thumbnail-container img{height:inherit}#aloha-cart-popover .aloha-cart{border-top:1px solid #ddd;border-bottom:1px solid #ddd}#aloha-cart-popover #aloha-cart-info{margin-right:0}#alohaBuyBoxWidget .without-contract-subheading{margin-right:0}#aloha-bb-help-nodes .aloha-bb-contract-term-heading{color:gray;font-family:arial;margin-top:.5em;text-align:center;height:.7em;border-bottom:1px solid gray;margin-bottom:1.6em}#aloha-bb-help-nodes .aloha-bb-contract-term-heading span{background-color:#fff;padding:0 10px 0 10px}#alohaAvailabilityUS_feature_div .availability a{text-decoration:none}#alohaPricingWidget a{text-decoration:none}#alohaAvailabilityUS_feature_div .availability{margin-top:-4px;margin-bottom:0}#alohaBuyBoxWidget .select-transaction-alert .a-icon-alert{top:18px;left:3px}#alohaBuyBoxWidget .select-transaction-alert .a-alert-container{padding-left:39px;width:290px}#alohaBuyBoxUS_feature_div #alohaBuyBoxWidget .contract-container .contract-term-heading a{text-decoration:none}#alohaBuyBoxUS_feature_div #alohaBuyBoxWidget .annual-contract-box .a-icon-popover{display:none}#alohaBuyBoxUS_feature_div #alohaBuyBoxWidget .contract-container .annual-contract-box{cursor:pointer;cursor:hand}#alohaBuyBoxUS_feature_div #alohaBuyBoxWidget .aloha-buybox-price{font-size:15px}#alohaBuyBoxUS_feature_div #alohaBuyBoxWidget #linkOffSection a{text-decoration:none}#alohaBuyBoxUS_feature_div .lockedUsedBuyboxContainer{padding-left:3.5%}#alohaBuyBoxUS_feature_div .alohaBuyboxUtilsNoWrap{white-space:nowrap}.hidden{display:none}.simo-no-padding{padding:0}.carrier-reviews-cell{padding-left:10px}.carrier-reviews-bordered-cell{border:1px dotted #ccc}.carrier-reviews-selected-cell{background-color:#ffd}#aloha-carrier-compatibility-modal-table-description{margin-top:10px;margin-bottom:14px}.aloha-carrier-compatibility-sortable-header.carrier{min-width:97px}.aloha-carrier-compatibility-sortable-header.compatibility{min-width:156px}.aloha-carrier-compatibility-sortable-header div{float:left}.aloha-carrier-compatibility-sortable-header i.a-icon{margin-left:10px;margin-top:4px}#aloha-carrier-compatibility-overview-table.a-bordered.a-vertical-stripes td:nth-child(2n),#aloha-carrier-compatibility-overview-table.a-bordered.a-vertical-stripes th:nth-child(2n){background-color:initial}#aloha-carrier-compatibility-modal-table.a-bordered.a-vertical-stripes td:nth-child(2n),#aloha-carrier-compatibility-modal-table.a-bordered.a-vertical-stripes th:nth-child(2n){background-color:initial}#aloha-carrier-compatibility-table.a-bordered.a-vertical-stripes th:nth-child(2n),.aloha-carrier-compatibility-table.a-bordered.a-vertical-stripes td:nth-child(2n){background-color:transparent}.aloha-carrier-compatibility-column-gray{background-color:#f6f6f6}.aloha-carrier-compatibility-modal-table-row .aloha-carrier-compatibility-tech-text,.aloha-carrier-compatibility-modal-table-row .carrier-name,.aloha-carrier-compatibility-modal-table-row .carrier-rating-summary{min-height:27px;display:inline-block;cursor:default}.aloha-carrier-compatibility-modal-table-row .aloha-carrier-compatibility-tech-text:first-line,.aloha-carrier-compatibility-modal-table-row .carrier-name:first-line,.aloha-carrier-compatibility-modal-table-row .carrier-rating-summary:first-line{line-height:27px}.aloha-carrier-compatibility-modal-table-row .aloha-carrier-compatibility-icon{margin-top:6px}.aloha-carrier-compatibility-check-icon{width:30px;height:27px;background-position:-318px -35px;background-image:url(https://images-na.ssl-images-amazon.com/images/G/01/AUIClients/AmazonUIBaseCSS-sprite_2x-8e7ef370dc28a214b3f490c9620f4ac501d5a864._V2_.png);background-repeat:no-repeat;background-size:400px 650px;display:inline-block;vertical-align:top}.aloha-carrier-compatibility-hidden{display:none}.aloha-buybox-spaced-link{margin-top:12px;margin-bottom:7px;text-align:center}.popover-tab and comes from the brand Unknown brand. It ranks 704,028 in Movies & TV ( and was released on Unknown date.\""
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "f2adedbc870feda",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [],
"source": [
"i = 0\n",
"for edge, edge_text in 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] = \"reviewer\"\n",
" try:\n",
" text_nodes[node2_id] = dict_id_to_text[node2]\n",
" except:\n",
" text_nodes[node2_id] = \"item\"\n",
" text_edges.append(edge_text)\n",
" try:\n",
" text_node_labels[node2_id] = dictid_to_label[node2]\n",
" except:\n",
" text_node_labels[node2_id] = \"Unknown\""
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "3305934f1a11caa7",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [],
"source": [
"from torch_geometric.data import Data\n",
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "5030fa8672f2b177",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [],
"source": [
"edge_index = torch.tensor([edge1,edge2])"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "21085a8a04df7062",
"metadata": {
"collapsed": false,
"is_executing": true
},
"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": 35,
"id": "d39601d90a0171c5",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Data saved to ./processed/movie.pkl\n"
]
}
],
"source": [
"import pickle\n",
"output_file_path = '../processed/movie.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}\")"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "60f52e9317cfad61",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [
{
"data": {
"text/plain": [
"Data(edge_index=[2, 1697533], text_nodes=[174012], text_edges=[1697533], text_node_labels=[174012], edge_score=[1697533])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4aaa10c4d649044a",
"metadata": {
"collapsed": false,
"is_executing": true
},
"outputs": [],
"source": []
}
],
"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
}
|