File size: 22,791 Bytes
708a196 |
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "65f8e86024294a6794a4bb3550623b8a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading readme: 0%| | 0.00/537 [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "0ee2de5bf53248d8a4079029a9504bdb",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Downloading data: 0%| | 0.00/4.79G [00:00<?, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "74c255d685774394927b0aa2baddff72",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating train split: 0 examples [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from datasets import load_dataset\n",
"\n",
"ds = load_dataset(\"do-me/Eurovoc_en\")\n",
"df = ds[\"train\"].to_pandas()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>date</th>\n",
" <th>eurovoc_concepts</th>\n",
" <th>url</th>\n",
" <th>lang</th>\n",
" <th>formats</th>\n",
" <th>text</th>\n",
" <th>__index_level_0__</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>96/493/EC: Council Decision of 29 March 1996 c...</td>\n",
" <td>1996-03-29</td>\n",
" <td>[UNO, forest conservation, international agree...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[fmx4, html, pdfa1b, print, xhtml]</td>\n",
" <td>L_1996208EN. 01000101. xml 17. 8. 1996 EN O...</td>\n",
" <td>29</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Proposal for a COUNCIL AND COMMISSION DECISION...</td>\n",
" <td>1996-03-29</td>\n",
" <td>[Ukraine, accession to the European Union, coo...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>COMMISSION OI l l ll I UKOIM AN COMMUNIMI S Br...</td>\n",
" <td>48</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Proposal for a COUNCIL AND COMMISSION DECISION...</td>\n",
" <td>1996-03-29</td>\n",
" <td>[EU relations, Moldova, accession to the Europ...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>, * ir ; COMMISSION OF THE EUROPEAN COMMUNITIE...</td>\n",
" <td>54</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Amended proposal for a COUNCIL REGULATION (EC)...</td>\n",
" <td>1996-03-28</td>\n",
" <td>[Antarctic Ocean, authorised catch, conservati...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[html, pdf, pdfa1b, print]</td>\n",
" <td>COMMISSION OF THE bUROPEAN COMMUNITIES Brussel...</td>\n",
" <td>73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Proposal for a COUNCIL DIRECTIVE amending Dire...</td>\n",
" <td>1996-03-27</td>\n",
" <td>[case law (EU), gender equality, job access, p...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[html, pdf, pdfa1b, print]</td>\n",
" <td>COMMISSION OF THE EUROPEAN COMMUNITIES Brussel...</td>\n",
" <td>108</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>352006</th>\n",
" <td>Non-opposition to a notified concentration (Ca...</td>\n",
" <td>2023-12-04</td>\n",
" <td>[India, economic concentration, electric vehic...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[fmx4, pdfa2a, xhtml]</td>\n",
" <td>C_202301503EN. 000101. fmx. xml Official Journ...</td>\n",
" <td>21936</td>\n",
" </tr>\n",
" <tr>\n",
" <th>352007</th>\n",
" <td>REPORT - on the proposal for a regulation of t...</td>\n",
" <td>2023-12-04</td>\n",
" <td>[EU statistics, European Statistical System, d...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[URI]</td>\n",
" <td>None</td>\n",
" <td>21940</td>\n",
" </tr>\n",
" <tr>\n",
" <th>352008</th>\n",
" <td>Steps in the asylum procedure.</td>\n",
" <td>2023-12-02</td>\n",
" <td>[asylum seeker, external border of the EU, imm...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf, pdfx4, print]</td>\n",
" <td>Steps in the asylum procedure Arrival 1 Applyi...</td>\n",
" <td>21945</td>\n",
" </tr>\n",
" <tr>\n",
" <th>352009</th>\n",
" <td>Do you need support? :</td>\n",
" <td>2023-12-02</td>\n",
" <td>[aid to refugees, asylum seeker, external bord...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>Do you need support? lleuaa EUROPEAN UNION AGE...</td>\n",
" <td>21946</td>\n",
" </tr>\n",
" <tr>\n",
" <th>352010</th>\n",
" <td>Do you fear for your safety in your country? :...</td>\n",
" <td>2023-12-02</td>\n",
" <td>[aid to refugees, asylum seeker, external bord...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf, pdfx4, print]</td>\n",
" <td>Do you fear for your safety in your country? T...</td>\n",
" <td>21947</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>352011 rows × 8 columns</p>\n",
"</div>"
],
"text/plain": [
" title date \\\n",
"0 96/493/EC: Council Decision of 29 March 1996 c... 1996-03-29 \n",
"1 Proposal for a COUNCIL AND COMMISSION DECISION... 1996-03-29 \n",
"2 Proposal for a COUNCIL AND COMMISSION DECISION... 1996-03-29 \n",
"3 Amended proposal for a COUNCIL REGULATION (EC)... 1996-03-28 \n",
"4 Proposal for a COUNCIL DIRECTIVE amending Dire... 1996-03-27 \n",
"... ... ... \n",
"352006 Non-opposition to a notified concentration (Ca... 2023-12-04 \n",
"352007 REPORT - on the proposal for a regulation of t... 2023-12-04 \n",
"352008 Steps in the asylum procedure. 2023-12-02 \n",
"352009 Do you need support? : 2023-12-02 \n",
"352010 Do you fear for your safety in your country? :... 2023-12-02 \n",
"\n",
" eurovoc_concepts \\\n",
"0 [UNO, forest conservation, international agree... \n",
"1 [Ukraine, accession to the European Union, coo... \n",
"2 [EU relations, Moldova, accession to the Europ... \n",
"3 [Antarctic Ocean, authorised catch, conservati... \n",
"4 [case law (EU), gender equality, job access, p... \n",
"... ... \n",
"352006 [India, economic concentration, electric vehic... \n",
"352007 [EU statistics, European Statistical System, d... \n",
"352008 [asylum seeker, external border of the EU, imm... \n",
"352009 [aid to refugees, asylum seeker, external bord... \n",
"352010 [aid to refugees, asylum seeker, external bord... \n",
"\n",
" url lang \\\n",
"0 http://publications.europa.eu/resource/cellar/... eng \n",
"1 http://publications.europa.eu/resource/cellar/... eng \n",
"2 http://publications.europa.eu/resource/cellar/... eng \n",
"3 http://publications.europa.eu/resource/cellar/... eng \n",
"4 http://publications.europa.eu/resource/cellar/... eng \n",
"... ... ... \n",
"352006 http://publications.europa.eu/resource/cellar/... eng \n",
"352007 http://publications.europa.eu/resource/cellar/... eng \n",
"352008 http://publications.europa.eu/resource/cellar/... eng \n",
"352009 http://publications.europa.eu/resource/cellar/... eng \n",
"352010 http://publications.europa.eu/resource/cellar/... eng \n",
"\n",
" formats \\\n",
"0 [fmx4, html, pdfa1b, print, xhtml] \n",
"1 [pdf] \n",
"2 [pdf] \n",
"3 [html, pdf, pdfa1b, print] \n",
"4 [html, pdf, pdfa1b, print] \n",
"... ... \n",
"352006 [fmx4, pdfa2a, xhtml] \n",
"352007 [URI] \n",
"352008 [pdf, pdfx4, print] \n",
"352009 [pdf] \n",
"352010 [pdf, pdfx4, print] \n",
"\n",
" text __index_level_0__ \n",
"0 L_1996208EN. 01000101. xml 17. 8. 1996 EN O... 29 \n",
"1 COMMISSION OI l l ll I UKOIM AN COMMUNIMI S Br... 48 \n",
"2 , * ir ; COMMISSION OF THE EUROPEAN COMMUNITIE... 54 \n",
"3 COMMISSION OF THE bUROPEAN COMMUNITIES Brussel... 73 \n",
"4 COMMISSION OF THE EUROPEAN COMMUNITIES Brussel... 108 \n",
"... ... ... \n",
"352006 C_202301503EN. 000101. fmx. xml Official Journ... 21936 \n",
"352007 None 21940 \n",
"352008 Steps in the asylum procedure Arrival 1 Applyi... 21945 \n",
"352009 Do you need support? lleuaa EUROPEAN UNION AGE... 21946 \n",
"352010 Do you fear for your safety in your country? T... 21947 \n",
"\n",
"[352011 rows x 8 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = ds[\"train\"].to_pandas()\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from datasets import load_dataset\n",
"from model2vec import StaticModel\n",
"\n",
"model = StaticModel.from_pretrained(\"minishlab/M2V_base_output\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"df[\"text\"] = df[\"text\"].fillna(\"\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 17min 49s, sys: 16.3 s, total: 18min 6s\n",
"Wall time: 1min 36s\n"
]
}
],
"source": [
"%%time \n",
"embeddings = model.encode(df.text)\n",
"df[\"embeddings\"] = embeddings.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"del df[\"__index_level_0__\"]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"del df[\"cos_sim\"]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"df = df.sort_index()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"df.to_parquet(\"CELLAR_EN_16_05_2024.parquet\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test query"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from model2vec import StaticModel\n",
"\n",
"model = StaticModel.from_pretrained(\"minishlab/M2V_base_output\")"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from sklearn.metrics.pairwise import cosine_similarity \n",
"\n",
"query = \"social democracy\"\n",
"quer_emb = model.encode(query)\n",
"\n",
"embeddings_matrix = np.stack(df['embeddings'].to_numpy())\n",
"df[\"cos_sim\"] = cosine_similarity(embeddings_matrix, quer_emb.reshape(1, -1))[:, 0]\n",
"df = df.sort_values(\"cos_sim\", ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>title</th>\n",
" <th>date</th>\n",
" <th>eurovoc_concepts</th>\n",
" <th>url</th>\n",
" <th>lang</th>\n",
" <th>formats</th>\n",
" <th>text</th>\n",
" <th>embeddings</th>\n",
" <th>cos_sim</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>295413</th>\n",
" <td>10 trends shaping democracy in a volatile world.</td>\n",
" <td>2019-11-21</td>\n",
" <td>[democracy, digital technology, election, popu...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>31 October 2019 10 TRENDS SHAPING DEMOCRACY IN...</td>\n",
" <td>[-0.7749167680740356, 5.183741569519043, -4.15...</td>\n",
" <td>0.497865</td>\n",
" </tr>\n",
" <tr>\n",
" <th>351006</th>\n",
" <td>Defence of democracy.</td>\n",
" <td>2023-12-20</td>\n",
" <td>[action programme, civic engagement, democracy...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>Defence of Democracy #EUDemocracy 12 December ...</td>\n",
" <td>[-0.4795585572719574, 6.859988212585449, -5.87...</td>\n",
" <td>0.451415</td>\n",
" </tr>\n",
" <tr>\n",
" <th>314572</th>\n",
" <td>EU citizenship report 2020 : factsheet.</td>\n",
" <td>2021-04-20</td>\n",
" <td>[European citizenship, European election, acce...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>EU Citizenship Report 2020 F A C T S H E E T 1...</td>\n",
" <td>[-0.9046934247016907, 5.682852745056152, -4.97...</td>\n",
" <td>0.432170</td>\n",
" </tr>\n",
" <tr>\n",
" <th>286936</th>\n",
" <td>Opinion of the European Economic and Social Co...</td>\n",
" <td>2019-03-20</td>\n",
" <td>[Liberalism, civil society, democracy, extremi...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[fmx4, pdfa2a, xhtml]</td>\n",
" <td>C_2019228EN. 01002401. xml 5. 7. 2019 EN Of...</td>\n",
" <td>[-0.5640900135040283, 3.6944658756256104, -4.5...</td>\n",
" <td>0.420833</td>\n",
" </tr>\n",
" <tr>\n",
" <th>330997</th>\n",
" <td>Making participatory and deliberative democrac...</td>\n",
" <td>2022-06-16</td>\n",
" <td>[EU research policy, green economy, innovation...</td>\n",
" <td>http://publications.europa.eu/resource/cellar/...</td>\n",
" <td>eng</td>\n",
" <td>[pdf]</td>\n",
" <td>#HorizonEU June 2022 MAKING PARTICIPATORY AND ...</td>\n",
" <td>[-0.27760544419288635, 5.397331237792969, -5.9...</td>\n",
" <td>0.419882</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" title date \\\n",
"295413 10 trends shaping democracy in a volatile world. 2019-11-21 \n",
"351006 Defence of democracy. 2023-12-20 \n",
"314572 EU citizenship report 2020 : factsheet. 2021-04-20 \n",
"286936 Opinion of the European Economic and Social Co... 2019-03-20 \n",
"330997 Making participatory and deliberative democrac... 2022-06-16 \n",
"\n",
" eurovoc_concepts \\\n",
"295413 [democracy, digital technology, election, popu... \n",
"351006 [action programme, civic engagement, democracy... \n",
"314572 [European citizenship, European election, acce... \n",
"286936 [Liberalism, civil society, democracy, extremi... \n",
"330997 [EU research policy, green economy, innovation... \n",
"\n",
" url lang \\\n",
"295413 http://publications.europa.eu/resource/cellar/... eng \n",
"351006 http://publications.europa.eu/resource/cellar/... eng \n",
"314572 http://publications.europa.eu/resource/cellar/... eng \n",
"286936 http://publications.europa.eu/resource/cellar/... eng \n",
"330997 http://publications.europa.eu/resource/cellar/... eng \n",
"\n",
" formats \\\n",
"295413 [pdf] \n",
"351006 [pdf] \n",
"314572 [pdf] \n",
"286936 [fmx4, pdfa2a, xhtml] \n",
"330997 [pdf] \n",
"\n",
" text \\\n",
"295413 31 October 2019 10 TRENDS SHAPING DEMOCRACY IN... \n",
"351006 Defence of Democracy #EUDemocracy 12 December ... \n",
"314572 EU Citizenship Report 2020 F A C T S H E E T 1... \n",
"286936 C_2019228EN. 01002401. xml 5. 7. 2019 EN Of... \n",
"330997 #HorizonEU June 2022 MAKING PARTICIPATORY AND ... \n",
"\n",
" embeddings cos_sim \n",
"295413 [-0.7749167680740356, 5.183741569519043, -4.15... 0.497865 \n",
"351006 [-0.4795585572719574, 6.859988212585449, -5.87... 0.451415 \n",
"314572 [-0.9046934247016907, 5.682852745056152, -4.97... 0.432170 \n",
"286936 [-0.5640900135040283, 3.6944658756256104, -4.5... 0.420833 \n",
"330997 [-0.27760544419288635, 5.397331237792969, -5.9... 0.419882 "
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py3.11",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|