Spaces:
Sleeping
Sleeping
File size: 72,177 Bytes
e62e0c5 |
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 |
{
"cells": [
{
"cell_type": "markdown",
"id": "73ef8baa",
"metadata": {},
"source": [
"# Query Marqo Index"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "dee4e8d3",
"metadata": {
"code_folding": []
},
"outputs": [],
"source": [
"## Import packages\n",
"import marqo as mq\n",
"import pandas as pd\n",
"import ipywidgets as widgets\n",
"from IPython.display import display, HTML\n",
"from IPython.core.display import Javascript\n",
"from pprint import pprint"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e80297f9",
"metadata": {
"code_folding": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connected to onit-sonnini-DHd2025-prep.\n"
]
}
],
"source": [
"## Connect to Marqo\n",
"\n",
"MARQO_URL = \"http://your.ip:port\"\n",
"marqoClient = mq.Client(url=MARQO_URL)\n",
"#pprint(marqoClient.get_indexes())\n",
"\n",
"## DHd 2025 ##\n",
"indexName = \"onit-sonnini-DHd2025-prep\" ## index with LLM-corrected texts as tensor field\n",
"#indexName = \"onit-sonnini-DHd2025-clean\" ## index with cleaned texts as tensor field\n",
"print(f'Connected to {indexName}.')\n",
"\n",
"# Load corpus data\n",
"bc_corpus = pd.read_csv(\"data/ONiT_barcodes_ALL_metadata_ONB_status_2024-05-23.csv\")\n",
"bc_corpus = bc_corpus.drop_duplicates(subset='barcode', keep='last')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "353081b8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'message': 'Welcome to Marqo', 'version': '2.5.1'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"marqoClient.index(indexName).get_marqo()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c9ceb6de",
"metadata": {
"code_folding": []
},
"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>barcode</th>\n",
" <th>page</th>\n",
" <th>iiif_link</th>\n",
" <th>text_clean</th>\n",
" <th>text_orig</th>\n",
" <th>text_prep</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Z166069305</td>\n",
" <td>5</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>C. S.' Sonnin is,\\nehemaligen Dffiziers uub In...</td>\n",
" <td>!\\n\\nC. S.' Sonnin i’s,\\n\\n;\\nehemaligen Dffiz...</td>\n",
" <td>C. S.' Sonnini's,\\nehemaligen Offiziers und In...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Z166069305</td>\n",
" <td>6</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>2125 murid\\ngobiothers\\nconale\\nKOENISE\\nKAISE...</td>\n",
" <td>2125 murid\\n\\ngobiothers\\n\\nconale\\n\\nܪܝ\\n\\n، ...</td>\n",
" <td>2255 Murdoch \\nGouverneurs\\nComte\\nKOENIGSE\\nK...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Z166069305</td>\n",
" <td>7</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>V o r re o e\\nDe 6 u i berpe Bet,$.\\nundteichl...</td>\n",
" <td>V o r re o e\\nDė 6 u i berpe Bét,$.\\n\\nundteic...</td>\n",
" <td>Vorrede.\\n\\nDeutschland hat in betreff seiner ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Z166069305</td>\n",
" <td>8</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Welt auf fich gezogen haben. Viele feiner Denk...</td>\n",
" <td>IV\\n\\n.\\n\\nWelt auf fich gezogen haben. Viele ...</td>\n",
" <td>Welt auf sich gezogen haben. Viele seiner Denk...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Z166069305</td>\n",
" <td>9</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>und auslandische Sklaven genossen die Vortheil...</td>\n",
" <td>)\\n\\n-\\nI\\n\\nI\\n\\n1\\n\\n11\\n\\nr.\\n\\nund ausländ...</td>\n",
" <td>Und ausländische Sklaven genossen die Vortheil...</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",
" </tr>\n",
" <tr>\n",
" <th>486</th>\n",
" <td>Z166069305</td>\n",
" <td>494</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td><empty page></td>\n",
" <td>+\\n\\nè³½\\n\\n1\\n\\n\\n</td>\n",
" <td><empty page></td>\n",
" </tr>\n",
" <tr>\n",
" <th>487</th>\n",
" <td>Z166069305</td>\n",
" <td>495</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Riedel fo\\nBedminenzelt</td>\n",
" <td>.7.\\n\\n.3.6\\n\\n>\\n\\n1 0\\n\\nRiedel fo\\n\\n(\\n\\n(...</td>\n",
" <td>Riedel von Eisenbach</td>\n",
" </tr>\n",
" <tr>\n",
" <th>488</th>\n",
" <td>Z166069305</td>\n",
" <td>499</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Osterreichische Nationalbibliothek\\n+ Z166069305</td>\n",
" <td>Österreichische Nationalbibliothek\\n\\n+ Z1660...</td>\n",
" <td>Please provide the faulty OCR texts generated ...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>489</th>\n",
" <td>Z166069305</td>\n",
" <td>503</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td><empty page></td>\n",
" <td>{\\n \"status code\" : 404,\\n \"message\" : \"The ...</td>\n",
" <td><empty page></td>\n",
" </tr>\n",
" <tr>\n",
" <th>490</th>\n",
" <td>Z166069305</td>\n",
" <td>504</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td><empty page></td>\n",
" <td>{\\n \"status code\" : 404,\\n \"message\" : \"The ...</td>\n",
" <td><empty page></td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>491 rows × 6 columns</p>\n",
"</div>"
],
"text/plain": [
" barcode page iiif_link \\\n",
"0 Z166069305 5 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"1 Z166069305 6 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"2 Z166069305 7 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"3 Z166069305 8 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"4 Z166069305 9 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
".. ... ... ... \n",
"486 Z166069305 494 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"487 Z166069305 495 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"488 Z166069305 499 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"489 Z166069305 503 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"490 Z166069305 504 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"\n",
" text_clean \\\n",
"0 C. S.' Sonnin is,\\nehemaligen Dffiziers uub In... \n",
"1 2125 murid\\ngobiothers\\nconale\\nKOENISE\\nKAISE... \n",
"2 V o r re o e\\nDe 6 u i berpe Bet,$.\\nundteichl... \n",
"3 Welt auf fich gezogen haben. Viele feiner Denk... \n",
"4 und auslandische Sklaven genossen die Vortheil... \n",
".. ... \n",
"486 <empty page> \n",
"487 Riedel fo\\nBedminenzelt \n",
"488 Osterreichische Nationalbibliothek\\n+ Z166069305 \n",
"489 <empty page> \n",
"490 <empty page> \n",
"\n",
" text_orig \\\n",
"0 !\\n\\nC. S.' Sonnin i’s,\\n\\n;\\nehemaligen Dffiz... \n",
"1 2125 murid\\n\\ngobiothers\\n\\nconale\\n\\nܪܝ\\n\\n، ... \n",
"2 V o r re o e\\nDė 6 u i berpe Bét,$.\\n\\nundteic... \n",
"3 IV\\n\\n.\\n\\nWelt auf fich gezogen haben. Viele ... \n",
"4 )\\n\\n-\\nI\\n\\nI\\n\\n1\\n\\n11\\n\\nr.\\n\\nund ausländ... \n",
".. ... \n",
"486 +\\n\\nè³½\\n\\n1\\n\\n\\n \n",
"487 .7.\\n\\n.3.6\\n\\n>\\n\\n1 0\\n\\nRiedel fo\\n\\n(\\n\\n(... \n",
"488 Österreichische Nationalbibliothek\\n\\n+ Z1660... \n",
"489 {\\n \"status code\" : 404,\\n \"message\" : \"The ... \n",
"490 {\\n \"status code\" : 404,\\n \"message\" : \"The ... \n",
"\n",
" text_prep \n",
"0 C. S.' Sonnini's,\\nehemaligen Offiziers und In... \n",
"1 2255 Murdoch \\nGouverneurs\\nComte\\nKOENIGSE\\nK... \n",
"2 Vorrede.\\n\\nDeutschland hat in betreff seiner ... \n",
"3 Welt auf sich gezogen haben. Viele seiner Denk... \n",
"4 Und ausländische Sklaven genossen die Vortheil... \n",
".. ... \n",
"486 <empty page> \n",
"487 Riedel von Eisenbach \n",
"488 Please provide the faulty OCR texts generated ... \n",
"489 <empty page> \n",
"490 <empty page> \n",
"\n",
"[491 rows x 6 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Preview data\n",
"import pandas as pd\n",
"\n",
"index_DHd = pd.read_csv(\"C:/onit_rag/data/DHd_index-cleaned.csv\")\n",
"index_DHd"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "203aa5ea",
"metadata": {},
"outputs": [],
"source": [
"## Query parameter\n",
"\n",
"query = \"Pferd, Pferde\"\n",
"limit = 1000 # max limit = 1000\n",
"threshold = 0.7 # set threshold\n",
"filter_string = \"barcode:(Z166069305)\" # Sonnini Bd. 1"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "693ec044",
"metadata": {
"code_folding": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Output tensor search: 491\n",
"Output lexical search: 28\n",
"Output tensor search after filtering vectors < 5 tokens: 447\n",
"Output tensor search with threshold: 447\n"
]
}
],
"source": [
"## Query the index\n",
"\n",
"#results = marqoClient.index(indexName).search(q=query, limit=limit) # basic search\n",
"results_tensor = marqoClient.index(indexName).search(q=query, limit=limit, filter_string=filter_string) # tensor search\n",
"print('Output tensor search: ', len(results_tensor['hits']))\n",
"results_lexical = marqoClient.index(indexName).search(q=query, limit=limit, filter_string=filter_string, search_method=\"LEXICAL\") # keyword search (BM25)\n",
"print('Output lexical search: ', len(results_lexical['hits']))\n",
"\n",
"## Load into dataframes\n",
"\n",
"# results tensor search\n",
"output1 = pd.DataFrame(results_tensor[\"hits\"])\n",
"# Filter rows where _highlights are 5 tokens or more\n",
"output1 = output1[output1[\"_highlights\"].apply(lambda x: len(str(x).split()) >= 5)]\n",
"print('Output tensor search after filtering vectors < 5 tokens: ', len(output1))\n",
"### Filter rows where _score is >= threshold\n",
"output1 = output1[output1[\"_score\"] >= threshold]\n",
"print('Output tensor search with threshold: ', len(output1))\n",
"# Add corpus metadata\n",
"# Merge the two DataFrames on the 'barcode' column to add 'corpus'\n",
"output1 = output1.merge(bc_corpus, on='barcode', how='inner')\n",
"# Add index +1 as a new column called 'rank'\n",
"output1['rank'] = output1.index + 1\n",
"# Add document identifier\n",
"output1['document'] = output1.apply(lambda row: f\"{row['barcode']}_{row['page']}\", axis=1)\n",
"\n",
"# results keyword search\n",
"if len(results_lexical['hits']) == 0:\n",
" output2 = output1\n",
" print(\"No lexical search results! Using only tensor results.\")\n",
" \n",
"else:\n",
" output2 = pd.DataFrame(results_lexical[\"hits\"])\n",
" # Add corpus metadata\n",
" # Merge the two DataFrames on the 'barcode' column to add 'corpus'\n",
" output2 = output2.merge(bc_corpus, on='barcode', how='inner')\n",
" # Add index +1 as a new column called 'rank'\n",
" output2['rank'] = output2.index + 1#apply(lambda row: output2.index + 1, axis=1)\n",
" # Add document identifier\n",
" output2['document'] = output2.apply(lambda row: f\"{row['barcode']}_{row['page']}\", axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "545badce",
"metadata": {
"code_folding": [
0
]
},
"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>document</th>\n",
" <th>rrf_score</th>\n",
" <th>barcode</th>\n",
" <th>page</th>\n",
" <th>iiif_link</th>\n",
" <th>text_orig</th>\n",
" <th>text_clean</th>\n",
" <th>text_prep</th>\n",
" <th>_id</th>\n",
" <th>_highlights</th>\n",
" <th>_score</th>\n",
" <th>rank</th>\n",
" <th>corpus</th>\n",
" <th>rerank</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Z166069305_430</td>\n",
" <td>0.032522</td>\n",
" <td>Z166069305</td>\n",
" <td>430</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>402 befand mich in einiger Entfernung davon, ...</td>\n",
" <td>befand mich in einiger Entfernung davon, und d...</td>\n",
" <td>befand mich in einiger Entfernung davon, und d...</td>\n",
" <td>7e2b21a1-4cdb-4b6a-b1c0-75c30bfe13bb</td>\n",
" <td>[{'text_prep': 'befand mich in einiger Entfern...</td>\n",
" <td>0.892140</td>\n",
" <td>1/2</td>\n",
" <td>D19</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Z166069305_10</td>\n",
" <td>0.030118</td>\n",
" <td>Z166069305</td>\n",
" <td>10</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>VI große Flinten, die ihm ſeine Diener zur Se...</td>\n",
" <td>grose Flinten, die ihm seine Diener zur Seite ...</td>\n",
" <td>große Flinten, die ihm seine Diener zur Seite ...</td>\n",
" <td>ab0905d4-6ca7-4e94-8fb2-3d2081632d6d</td>\n",
" <td>[{'text_prep': 'Sein letztes Hulfritt trifft z...</td>\n",
" <td>0.872264</td>\n",
" <td>4/9</td>\n",
" <td>D19</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Z166069305_399</td>\n",
" <td>0.029324</td>\n",
" <td>Z166069305</td>\n",
" <td>399</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>1 1 1 , 571 Plinius berichtet nach dem Xen...</td>\n",
" <td>Plinius berichtet nach dem Xenophon, die Camel...</td>\n",
" <td>Plinius berichtet nach dem Xenophon, die Camel...</td>\n",
" <td>feb1b47b-effe-4e9d-be0f-7a749fed5ec0</td>\n",
" <td>[{'text_prep': 'Ich muss hier auch bemerken, d...</td>\n",
" <td>0.865306</td>\n",
" <td>13/4</td>\n",
" <td>D19</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Z166069305_415</td>\n",
" <td>0.028860</td>\n",
" <td>Z166069305</td>\n",
" <td>415</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>587 nen. Ich ritt auf fie los, aber Hufrein l...</td>\n",
" <td>nen. Ich ritt auf fie los, aber Hufrein lies m...</td>\n",
" <td>Ich ritt auf sie los, aber Hufrein lies mich r...</td>\n",
" <td>02bb750e-0993-4341-ab74-934d28fc523e</td>\n",
" <td>[{'text_prep': 'Ehe wir noch an dieser Ore anl...</td>\n",
" <td>0.864078</td>\n",
" <td>17/3</td>\n",
" <td>D19</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Z166069305_220</td>\n",
" <td>0.028485</td>\n",
" <td>Z166069305</td>\n",
" <td>220</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>' 92 lungen und Bewegungen. Mit einer Phyſiog...</td>\n",
" <td>lungen und Bewegungen. Mit einer Physiognomie,...</td>\n",
" <td>Lungen und Bewegungen. Mit einer Physiognomie,...</td>\n",
" <td>7949fe4e-5534-4522-b151-35b57a733650</td>\n",
" <td>[{'text_prep': 'Die Haustiere finden nirgends ...</td>\n",
" <td>0.869331</td>\n",
" <td>6/15</td>\n",
" <td>D19</td>\n",
" <td>5</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",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>478</th>\n",
" <td>Z166069305_493</td>\n",
" <td>0.001901</td>\n",
" <td>Z166069305</td>\n",
" <td>493</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Pl. 5. Fig.2. Fig.3. Fig. 1 B Riedel fee...</td>\n",
" <td>Pl. 5. Fig.2. Fig.3. Fig. 1 Riedel fee. Fig. 1...</td>\n",
" <td>Pl. 5. Fig. 2. Fig. 3. Fig. 1 Riedel fee. Fig....</td>\n",
" <td>131e6f12-db13-422f-96e4-0924fe19026b</td>\n",
" <td>[{'text_prep': 'Fig. 1 Riedel fee.'}]</td>\n",
" <td>0.819874</td>\n",
" <td>466</td>\n",
" <td>D19</td>\n",
" <td>479</td>\n",
" </tr>\n",
" <tr>\n",
" <th>479</th>\n",
" <td>Z166069305_484</td>\n",
" <td>0.001898</td>\n",
" <td>Z166069305</td>\n",
" <td>484</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>1 - 1 Seite 109 Zeile I v. u. Abanſoon lies...</td>\n",
" <td>Seite 109 Zeile I v. u. Abansoon lies as anson...</td>\n",
" <td>Seite 109 Zeile I v. u. Abends sonnig, ansonst...</td>\n",
" <td>da3ced63-91a8-4710-81b6-40da8a1cf407</td>\n",
" <td>[{'text_prep': 'Seite 109 Zeile I v. u. Abends...</td>\n",
" <td>0.818248</td>\n",
" <td>467</td>\n",
" <td>D19</td>\n",
" <td>480</td>\n",
" </tr>\n",
" <tr>\n",
" <th>480</th>\n",
" <td>Z166069305_489</td>\n",
" <td>0.001894</td>\n",
" <td>Z166069305</td>\n",
" <td>489</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Th. PL 3 ஏப்ரதம் THUNDE 1 4 Ironis ту C...</td>\n",
" <td>Th. PL 3 THUNDE Ironis Cine kanclerte tatue be...</td>\n",
" <td>Theodor Pl. 3 Thundersturm Ironische cine kan...</td>\n",
" <td>228e98a9-2d29-4f34-a0c2-adcdd2eeff09</td>\n",
" <td>[{'text_prep': 'Theodor Pl. 3 Thundersturm Ir...</td>\n",
" <td>0.818178</td>\n",
" <td>468</td>\n",
" <td>D19</td>\n",
" <td>481</td>\n",
" </tr>\n",
" <tr>\n",
" <th>481</th>\n",
" <td>Z166069305_482</td>\n",
" <td>0.001890</td>\n",
" <td>Z166069305</td>\n",
" <td>482</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>ie \" !!! 3 iC; 1: 0 Wis . bici\" ..) .\"., 6...</td>\n",
" <td>iC; 1: 0 Wis . bici\" ..) .\"., 6,7, ART 9 * noi...</td>\n",
" <td>Ich kam also um die neunte Stunde zu Wismar.</td>\n",
" <td>9e9b9254-c675-4427-a20e-88b8d723aec3</td>\n",
" <td>[{'text_prep': 'Ich kam also um die neunte Stu...</td>\n",
" <td>0.812620</td>\n",
" <td>469</td>\n",
" <td>D19</td>\n",
" <td>482</td>\n",
" </tr>\n",
" <tr>\n",
" <th>482</th>\n",
" <td>Z166069305_499</td>\n",
" <td>0.001887</td>\n",
" <td>Z166069305</td>\n",
" <td>499</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>Österreichische Nationalbibliothek + Z166069305</td>\n",
" <td>Osterreichische Nationalbibliothek + Z166069305</td>\n",
" <td>Please provide the faulty OCR texts generated ...</td>\n",
" <td>93723fd2-f359-4251-8621-513c4f6e1128</td>\n",
" <td>[{'text_prep': '(Please paste the text)'}]</td>\n",
" <td>0.805837</td>\n",
" <td>470</td>\n",
" <td>D19</td>\n",
" <td>483</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>483 rows × 14 columns</p>\n",
"</div>"
],
"text/plain": [
" document rrf_score barcode page \\\n",
"0 Z166069305_430 0.032522 Z166069305 430 \n",
"1 Z166069305_10 0.030118 Z166069305 10 \n",
"2 Z166069305_399 0.029324 Z166069305 399 \n",
"3 Z166069305_415 0.028860 Z166069305 415 \n",
"4 Z166069305_220 0.028485 Z166069305 220 \n",
".. ... ... ... ... \n",
"478 Z166069305_493 0.001901 Z166069305 493 \n",
"479 Z166069305_484 0.001898 Z166069305 484 \n",
"480 Z166069305_489 0.001894 Z166069305 489 \n",
"481 Z166069305_482 0.001890 Z166069305 482 \n",
"482 Z166069305_499 0.001887 Z166069305 499 \n",
"\n",
" iiif_link \\\n",
"0 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"1 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"2 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"3 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"4 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
".. ... \n",
"478 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"479 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"480 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"481 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"482 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"\n",
" text_orig \\\n",
"0 402 befand mich in einiger Entfernung davon, ... \n",
"1 VI große Flinten, die ihm ſeine Diener zur Se... \n",
"2 1 1 1 , 571 Plinius berichtet nach dem Xen... \n",
"3 587 nen. Ich ritt auf fie los, aber Hufrein l... \n",
"4 ' 92 lungen und Bewegungen. Mit einer Phyſiog... \n",
".. ... \n",
"478 Pl. 5. Fig.2. Fig.3. Fig. 1 B Riedel fee... \n",
"479 1 - 1 Seite 109 Zeile I v. u. Abanſoon lies... \n",
"480 Th. PL 3 ஏப்ரதம் THUNDE 1 4 Ironis ту C... \n",
"481 ie \" !!! 3 iC; 1: 0 Wis . bici\" ..) .\"., 6... \n",
"482 Österreichische Nationalbibliothek + Z166069305 \n",
"\n",
" text_clean \\\n",
"0 befand mich in einiger Entfernung davon, und d... \n",
"1 grose Flinten, die ihm seine Diener zur Seite ... \n",
"2 Plinius berichtet nach dem Xenophon, die Camel... \n",
"3 nen. Ich ritt auf fie los, aber Hufrein lies m... \n",
"4 lungen und Bewegungen. Mit einer Physiognomie,... \n",
".. ... \n",
"478 Pl. 5. Fig.2. Fig.3. Fig. 1 Riedel fee. Fig. 1... \n",
"479 Seite 109 Zeile I v. u. Abansoon lies as anson... \n",
"480 Th. PL 3 THUNDE Ironis Cine kanclerte tatue be... \n",
"481 iC; 1: 0 Wis . bici\" ..) .\"., 6,7, ART 9 * noi... \n",
"482 Osterreichische Nationalbibliothek + Z166069305 \n",
"\n",
" text_prep \\\n",
"0 befand mich in einiger Entfernung davon, und d... \n",
"1 große Flinten, die ihm seine Diener zur Seite ... \n",
"2 Plinius berichtet nach dem Xenophon, die Camel... \n",
"3 Ich ritt auf sie los, aber Hufrein lies mich r... \n",
"4 Lungen und Bewegungen. Mit einer Physiognomie,... \n",
".. ... \n",
"478 Pl. 5. Fig. 2. Fig. 3. Fig. 1 Riedel fee. Fig.... \n",
"479 Seite 109 Zeile I v. u. Abends sonnig, ansonst... \n",
"480 Theodor Pl. 3 Thundersturm Ironische cine kan... \n",
"481 Ich kam also um die neunte Stunde zu Wismar. \n",
"482 Please provide the faulty OCR texts generated ... \n",
"\n",
" _id \\\n",
"0 7e2b21a1-4cdb-4b6a-b1c0-75c30bfe13bb \n",
"1 ab0905d4-6ca7-4e94-8fb2-3d2081632d6d \n",
"2 feb1b47b-effe-4e9d-be0f-7a749fed5ec0 \n",
"3 02bb750e-0993-4341-ab74-934d28fc523e \n",
"4 7949fe4e-5534-4522-b151-35b57a733650 \n",
".. ... \n",
"478 131e6f12-db13-422f-96e4-0924fe19026b \n",
"479 da3ced63-91a8-4710-81b6-40da8a1cf407 \n",
"480 228e98a9-2d29-4f34-a0c2-adcdd2eeff09 \n",
"481 9e9b9254-c675-4427-a20e-88b8d723aec3 \n",
"482 93723fd2-f359-4251-8621-513c4f6e1128 \n",
"\n",
" _highlights _score rank corpus \\\n",
"0 [{'text_prep': 'befand mich in einiger Entfern... 0.892140 1/2 D19 \n",
"1 [{'text_prep': 'Sein letztes Hulfritt trifft z... 0.872264 4/9 D19 \n",
"2 [{'text_prep': 'Ich muss hier auch bemerken, d... 0.865306 13/4 D19 \n",
"3 [{'text_prep': 'Ehe wir noch an dieser Ore anl... 0.864078 17/3 D19 \n",
"4 [{'text_prep': 'Die Haustiere finden nirgends ... 0.869331 6/15 D19 \n",
".. ... ... ... ... \n",
"478 [{'text_prep': 'Fig. 1 Riedel fee.'}] 0.819874 466 D19 \n",
"479 [{'text_prep': 'Seite 109 Zeile I v. u. Abends... 0.818248 467 D19 \n",
"480 [{'text_prep': 'Theodor Pl. 3 Thundersturm Ir... 0.818178 468 D19 \n",
"481 [{'text_prep': 'Ich kam also um die neunte Stu... 0.812620 469 D19 \n",
"482 [{'text_prep': '(Please paste the text)'}] 0.805837 470 D19 \n",
"\n",
" rerank \n",
"0 1 \n",
"1 2 \n",
"2 3 \n",
"3 4 \n",
"4 5 \n",
".. ... \n",
"478 479 \n",
"479 480 \n",
"480 481 \n",
"481 482 \n",
"482 483 \n",
"\n",
"[483 rows x 14 columns]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Reciprocal Rank Fusion (RRF)\n",
"\n",
"# Set the RRF parameter\n",
"k = 60\n",
"\n",
"# Concatenate the DataFrames to combine all rankings into one DataFrame\n",
"combined_df = pd.concat([output1, output2], ignore_index=True)\n",
"\n",
"# Initialize the RRF score column to 0\n",
"combined_df['rrf_score'] = 0\n",
"\n",
"# Function to update RRF scores\n",
"def update_rrf_scores(df, k):\n",
" # Calculate RRF score using the formula and add to the rrf_score column\n",
" df['rrf_score'] += 1 / (k + df['rank'])\n",
"\n",
"# Update RRF scores for the combined DataFrame\n",
"update_rrf_scores(combined_df, k)\n",
"\n",
"# Custom aggregation function to combine values with a '/'\n",
"def combine_values(values):\n",
" # Convert to string and join unique values with a '/' separator\n",
" return '/'.join(values.astype(str).unique())\n",
"\n",
"# Group by 'item' and sum the RRF scores for each item\n",
"final_scores_df = final_scores_df = combined_df.groupby('document', as_index=False).agg({\n",
" 'rrf_score': 'sum',\n",
" 'barcode': 'first',\n",
" 'page': 'first',\n",
" 'iiif_link': 'first',\n",
" 'text_orig': 'first',\n",
" 'text_clean': 'first',\n",
" 'text_prep': 'first',\n",
" '_id': 'first',\n",
" '_highlights': 'first',\n",
" '_score': 'first',\n",
" 'rank': combine_values,\n",
" 'corpus': 'first'\n",
"})\n",
"\n",
"# Sort by total RRF score in descending order and reset index\n",
"final_scores_df = final_scores_df.sort_values(by='rrf_score', ascending=False).reset_index(drop=True)\n",
"\n",
"# Add rerank based on the sorted order\n",
"final_scores_df['rerank'] = final_scores_df.index + 1\n",
"\n",
"# Display the final DataFrame\n",
"#print(f'Total results: {len(final_scores_df)}')\n",
"#final_scores_df\n",
"\n",
"final_scores_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47aad8d3",
"metadata": {
"code_folding": [
0
]
},
"outputs": [],
"source": [
"## Multi-Term Queries\n",
"\n",
"query = 'Nur Pflanzen, Vegetation und Flora'\n",
"limit = 1000 # max limit = 1000\n",
"threshold = 0.80 # set threshold\n",
"filter_string = \"barcode:(Z166069305) OR barcode:(Z166069408)\" # Sonnini\n",
"\n",
"results = marqoClient.index(indexName).search(\n",
" {\n",
" query: 2.0,\n",
" \"Tiere oder jegliche Fauna\": -0.8,\n",
" \"wichtige Ernährung für die Bevölkerung\": +0.8,\n",
" #\"Meteorologische Beobachtungen\": -0.8,\n",
" },\n",
" limit=limit,\n",
" #filter_string=filter_string\n",
")\n",
"\n",
"output = pd.DataFrame(results[\"hits\"])\n",
"output = output[output['_score'] > threshold] # filter results above threshold\n",
"\n",
"print(f'Total results: {len(output)}')\n",
"#output"
]
},
{
"cell_type": "markdown",
"id": "7cd84368",
"metadata": {},
"source": [
"# Generate Preview"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a045bdfd",
"metadata": {
"code_folding": [
0
],
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "52fc4bb25414483dad663802648a3559",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Box(children=(HBox(children=(HTML(value='<b>1</b>'), HTML(value='<i>Z166069305_430: </i><mark>b…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "334de0628ea440049482be20848ffcf7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output(layout=Layout(border='1px solid black', height='auto', padding='10px', width='100%'))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"## Preview R matches\n",
"\n",
"R=20 # set number of results to be previewed\n",
"\n",
"# Function to unpack and concatenate all texts from the lists of dictionaries\n",
"def unpack_texts(series):\n",
" return series.apply(lambda x: ' '.join([d.get('text_clean', d.get('text_prep', '')) for d in x if isinstance(d, dict)]))\n",
"\n",
"# Apply the function to the '_highlights' column and create a new column 'unpacked_highlights'\n",
"final_scores_df['unpacked_highlights'] = unpack_texts(final_scores_df['_highlights'])\n",
"#print(output['_highlights'])\n",
"\n",
"text_col = 'text_prep' if 'text_prep' in final_scores_df.columns else 'text_clean'\n",
"\n",
"# Check if 'unpacked_highlights' is empty and replace it if necessary\n",
"final_scores_df['unpacked_highlights'] = final_scores_df.apply(\n",
" lambda row: row[text_col] if not row['unpacked_highlights'] else row['unpacked_highlights'],\n",
" axis=1\n",
")\n",
"\n",
"# Define a function to highlight parts of the text\n",
"def highlight_text(text, highlights):\n",
" # Ensure highlights is a list of strings\n",
" if isinstance(highlights, str):\n",
" highlights = [highlights]\n",
" # Wrap each highlight in <mark> tags\n",
" for highlight in highlights:\n",
" # Replace highlight text with a highlighted version\n",
" text = text.replace(highlight, f'<mark>{highlight}</mark>')\n",
" return text\n",
"\n",
"# Define a function to display full details of a row\n",
"def show_details(row):\n",
" details = widgets.Output()\n",
" with details:\n",
" # Use inline styles to ensure no scrolling\n",
" content = f\"\"\"\n",
" <div style=\"border: 1px solid black; padding: 10px; max-width: 100%; box-sizing: border-box;\">\n",
" <h3>Details for {row['barcode']}, {row['page']}</h3>\n",
" <p><b>Retrieved text chunk: </b><i>{row[\"unpacked_highlights\"]}</i></p>\n",
" <p>{highlight_text(row[text_col], row[\"unpacked_highlights\"])}</p>\n",
" <img src=\"{row[\"iiif_link\"]}\" alt=\"IIIF Image Preview\" style=\"max-width: 100%; height: auto;\">\n",
" <a href=\"https://digital.onb.ac.at/OnbViewer/viewer.faces?doc=ABO_%2B{row[\"barcode\"]}\" target=\"_blank\">Open ÖNB Viewer</a>\n",
" </div>\n",
" \"\"\"\n",
" display(HTML(content))\n",
" return details\n",
"\n",
"# Define a function to create a row with more information and a clickable button\n",
"def create_row(row, idx):\n",
" button = widgets.Button(description=\"Inspect\", layout=widgets.Layout(width=\"auto\"))\n",
"\n",
" # When the button is clicked, show more details\n",
" def on_button_clicked(b):\n",
" details_box.clear_output()\n",
" with details_box:\n",
" display(show_details(row))\n",
" \n",
" button.on_click(on_button_clicked)\n",
" \n",
" # Highlight the text in the 'unpacked_highlights' column\n",
" unpacked = highlight_text(row['unpacked_highlights'], row['unpacked_highlights'])\n",
" \n",
" # Create a display row with button, highlights, and iiif links\n",
" row_display = widgets.HBox([\n",
" widgets.HTML(value=f\"<b>{idx + 1}</b>\"), # Display the index + 1\n",
" widgets.HTML(\n",
" value=f\"<i>{row['document']}: </i>{unpacked}\",\n",
" layout=widgets.Layout(width='500px')\n",
" ), # Highlights as text\n",
" widgets.HTML(\n",
" value=f\"Score: {row['_score']:.3f}\",\n",
" layout=widgets.Layout(width='100px')\n",
" ), # Score as text\n",
" widgets.HTML(\n",
" value=f\"Prev. ranks: {row['rank']}\",\n",
" layout=widgets.Layout(width='100px')\n",
" ), # Ranks as text\n",
" widgets.HTML(\n",
" value=f\"Corpus: {row['corpus']}\",\n",
" layout=widgets.Layout(width='100px')\n",
" ), # Corpus as text\n",
" button # Button to view more details\n",
" ])\n",
" \n",
" # Wrap the row display in a Box with a border\n",
" boxed_row_display = widgets.Box(\n",
" [row_display],\n",
" layout=widgets.Layout(\n",
" border='1px solid black', # Set the border style\n",
" padding='10px', # Add padding inside the box\n",
" margin='5px 0px', # Add margin above and below the box\n",
" width='100%' # Ensure the box spans the full width\n",
" )\n",
" )\n",
" \n",
" return boxed_row_display\n",
"\n",
"# Create an interactive list of display rows (one for each DataFrame row)\n",
"rows = [create_row(row, idx) for idx, row in final_scores_df.head(R).iterrows()]\n",
"\n",
"# Display area for detailed view\n",
"details_box = widgets.Output()\n",
"details_box.layout = widgets.Layout(\n",
" border='1px solid black',\n",
" padding='10px',\n",
" width='100%', # Ensure full width\n",
" height='auto', # Adjust height automatically\n",
")\n",
"\n",
"# Display the rows and details box\n",
"rows_box = widgets.VBox(rows)\n",
"display(rows_box, details_box)"
]
},
{
"cell_type": "markdown",
"id": "88e0078b",
"metadata": {},
"source": [
"# Similarity Metrics with Sentence Transformer"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "c783b576",
"metadata": {},
"outputs": [],
"source": [
"from sentence_transformers import SentenceTransformer, util"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "b5db773f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\vignolim\\Anaconda3\\lib\\site-packages\\transformers\\tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
" warnings.warn(\n"
]
}
],
"source": [
"# Load a pre-trained Sentence Transformer model\n",
"model = SentenceTransformer('paraphrase-MiniLM-L6-v2')\n",
"\n",
"# List of sentences\n",
"list_of_sentences = final_scores_df['unpacked_highlights'].tolist()\n",
"\n",
"# Step 1: Encode the sentences and the query into embeddings\n",
"sentence_embeddings = model.encode(list_of_sentences, convert_to_tensor=True)\n",
"query_embedding = model.encode(query, convert_to_tensor=True)\n",
"\n",
"# Step 2: Compute the cosine similarities between the query and the list of sentences\n",
"cosine_scores = util.pytorch_cos_sim(query_embedding, sentence_embeddings)\n",
"\n",
"# Step 3: Create a pandas DataFrame with sentences and their corresponding cosine similarity scores\n",
"df_cosine_sim = pd.DataFrame({\n",
" 'Sentence': list_of_sentences,\n",
" 'Cosine Similarity': cosine_scores.squeeze().tolist()\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "33ef4dfa",
"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>Sentence</th>\n",
" <th>Cosine Similarity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>S. sono. Gmelin hat diesen Vogel in der 13.</td>\n",
" <td>0.587100</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Ich todtete eine gelbe Bachstelze ***) und ein...</td>\n",
" <td>0.293896</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Auf diesem Damme stehen von Zeit zu Zeit klein...</td>\n",
" <td>0.347629</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Indessen schienen mir diese Vögel sich mehr de...</td>\n",
" <td>0.471382</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Die Wanderung dieser Vögel nach Ägypten hat je...</td>\n",
" <td>0.476594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Ich traf auch eine Wachtel an, die ich sogleic...</td>\n",
" <td>0.200288</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Diese Vögel sind sehr fett, ihr Fleisch ist za...</td>\n",
" <td>0.407871</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Reichhaltig war er. Kein Vogel ist in Ägypten ...</td>\n",
" <td>0.515706</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Linn. Den ersten Morgens bei einem sehr schöne...</td>\n",
" <td>0.310032</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Bearbeitung der Gegenden um den Canal Salza. F...</td>\n",
" <td>0.271253</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>Diese Vögel hielten sich nicht in der Nähe von</td>\n",
" <td>0.515144</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>nennt. Diese letztgenannten Vögel sprangen paa...</td>\n",
" <td>0.336441</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>Binther- Nilaat- Schildfröste- te- Vogel- Raub...</td>\n",
" <td>0.163589</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>Durch diese habe ich auch den Unterschied des ...</td>\n",
" <td>0.268166</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>Sie sagten uns, diese Vögel kommen oft nach Ab...</td>\n",
" <td>0.320704</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>Es hielt daher sehr schwer, sich einen von die...</td>\n",
" <td>0.352532</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>Senesbaum Vogel Beschreibung einer Art von Fal...</td>\n",
" <td>0.317160</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>Diese Vögel gehören nicht zu einerlei Art. Die...</td>\n",
" <td>0.433839</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>Ich erkannte Beccafien, Feldlerchen und Sperli...</td>\n",
" <td>0.303594</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>Kapitel. Natron -- Bleichen der Leinwand und d...</td>\n",
" <td>0.265617</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Sentence Cosine Similarity\n",
"0 S. sono. Gmelin hat diesen Vogel in der 13. 0.587100\n",
"1 Ich todtete eine gelbe Bachstelze ***) und ein... 0.293896\n",
"2 Auf diesem Damme stehen von Zeit zu Zeit klein... 0.347629\n",
"3 Indessen schienen mir diese Vögel sich mehr de... 0.471382\n",
"4 Die Wanderung dieser Vögel nach Ägypten hat je... 0.476594\n",
"5 Ich traf auch eine Wachtel an, die ich sogleic... 0.200288\n",
"6 Diese Vögel sind sehr fett, ihr Fleisch ist za... 0.407871\n",
"7 Reichhaltig war er. Kein Vogel ist in Ägypten ... 0.515706\n",
"8 Linn. Den ersten Morgens bei einem sehr schöne... 0.310032\n",
"9 Bearbeitung der Gegenden um den Canal Salza. F... 0.271253\n",
"10 Diese Vögel hielten sich nicht in der Nähe von 0.515144\n",
"11 nennt. Diese letztgenannten Vögel sprangen paa... 0.336441\n",
"12 Binther- Nilaat- Schildfröste- te- Vogel- Raub... 0.163589\n",
"13 Durch diese habe ich auch den Unterschied des ... 0.268166\n",
"14 Sie sagten uns, diese Vögel kommen oft nach Ab... 0.320704\n",
"15 Es hielt daher sehr schwer, sich einen von die... 0.352532\n",
"16 Senesbaum Vogel Beschreibung einer Art von Fal... 0.317160\n",
"17 Diese Vögel gehören nicht zu einerlei Art. Die... 0.433839\n",
"18 Ich erkannte Beccafien, Feldlerchen und Sperli... 0.303594\n",
"19 Kapitel. Natron -- Bleichen der Leinwand und d... 0.265617"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_cosine_sim.head(R)"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "c26885b7",
"metadata": {},
"outputs": [],
"source": [
"# Add SentenceTransformer Cosine Similarity score to output dataframe\n",
"final_scores_df['ST_cosine_similarity'] = df_cosine_sim['Cosine Similarity']"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "17d5abeb",
"metadata": {},
"outputs": [],
"source": [
"# Add direct link to ÖNB Viewer to output dataframe\n",
"base_url = \"https://digital.onb.ac.at/OnbViewer/viewer.faces?doc=ABO_%2B\"\n",
"\n",
"# Create the 'onb_viewer_link' column by appending each 'barcode' to the base URL\n",
"final_scores_df['onb_viewer_link'] = base_url + final_scores_df['barcode'].astype(str)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "b6a2d7e0",
"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>document</th>\n",
" <th>rrf_score</th>\n",
" <th>barcode</th>\n",
" <th>page</th>\n",
" <th>iiif_link</th>\n",
" <th>text_orig</th>\n",
" <th>text_clean</th>\n",
" <th>text_prep</th>\n",
" <th>_id</th>\n",
" <th>_highlights</th>\n",
" <th>_score</th>\n",
" <th>rank</th>\n",
" <th>corpus</th>\n",
" <th>rerank</th>\n",
" <th>unpacked_highlights</th>\n",
" <th>ST_cosine_similarity</th>\n",
" <th>onb_viewer_link</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Z166069305_252</td>\n",
" <td>0.032258</td>\n",
" <td>Z166069305</td>\n",
" <td>252</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>224 7 Dieſer Falke war ein Weibchen. Der Ein...</td>\n",
" <td>Dieser Falke war ein Weibchen. Der Eingeweide ...</td>\n",
" <td>Dieser Falke war ein Weibchen. Der Eingeweidek...</td>\n",
" <td>8a5893c6-fde6-42f3-94e3-16db95629fe5</td>\n",
" <td>[{'text_prep': 'S. sono. Gmelin hat diesen Vog...</td>\n",
" <td>0.856740</td>\n",
" <td>2</td>\n",
" <td>D19</td>\n",
" <td>1</td>\n",
" <td>S. sono. Gmelin hat diesen Vogel in der 13.</td>\n",
" <td>0.587100</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Z166069305_43</td>\n",
" <td>0.031025</td>\n",
" <td>Z166069305</td>\n",
" <td>43</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>16 5 1 entgegen, indem er mit großer Heftig...</td>\n",
" <td>entgegen, indem er mit groser Heftigkeit nach ...</td>\n",
" <td>entgegen, indem er mit großer Heftigkeit nach ...</td>\n",
" <td>4205ec0a-9cf3-448d-afba-3023e4c92052</td>\n",
" <td>[{'text_prep': 'Ich todtete eine gelbe Bachste...</td>\n",
" <td>0.852868</td>\n",
" <td>3/6</td>\n",
" <td>D19</td>\n",
" <td>2</td>\n",
" <td>Ich todtete eine gelbe Bachstelze ***) und ein...</td>\n",
" <td>0.293896</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Z166069305_265</td>\n",
" <td>0.029911</td>\n",
" <td>Z166069305</td>\n",
" <td>265</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>237 Seeufer, an welchem wir bis zum See Maadi...</td>\n",
" <td>Seeufer, an welchem wir bis zum See Maadie hin...</td>\n",
" <td>Seeufer, an welchem wir bis zum See Maadie hin...</td>\n",
" <td>5fc895c1-b587-4442-a34c-dd446c3054f2</td>\n",
" <td>[{'text_prep': 'Auf diesem Damme stehen von Ze...</td>\n",
" <td>0.848943</td>\n",
" <td>4/10</td>\n",
" <td>D19</td>\n",
" <td>3</td>\n",
" <td>Auf diesem Damme stehen von Zeit zu Zeit klein...</td>\n",
" <td>0.347629</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Z166069305_344</td>\n",
" <td>0.029412</td>\n",
" <td>Z166069305</td>\n",
" <td>344</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>316 niederſchoß, wünſchte ich mir Glük, dieſe...</td>\n",
" <td>niederschos, wunschte ich mir Gluk, diesen uns...</td>\n",
" <td>Niederschoss, wünschte ich mir Glück, diesen u...</td>\n",
" <td>a7debf83-ac70-445a-afee-5f35515b3e1d</td>\n",
" <td>[{'text_prep': 'Indessen schienen mir diese Vö...</td>\n",
" <td>0.843653</td>\n",
" <td>8</td>\n",
" <td>D19</td>\n",
" <td>4</td>\n",
" <td>Indessen schienen mir diese Vögel sich mehr de...</td>\n",
" <td>0.471382</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Z166069305_243</td>\n",
" <td>0.027778</td>\n",
" <td>Z166069305</td>\n",
" <td>243</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>215 don -einſam liegenden Seen beleben, bleib...</td>\n",
" <td>don -einsam liegenden Seen beleben, bleiben di...</td>\n",
" <td>don-einsam liegenden Seen beleben, bleiben die...</td>\n",
" <td>58487609-e48c-47a1-8a0a-06569fef7f53</td>\n",
" <td>[{'text_prep': 'Die Wanderung dieser Vögel nac...</td>\n",
" <td>0.835293</td>\n",
" <td>24/3</td>\n",
" <td>D19</td>\n",
" <td>5</td>\n",
" <td>Die Wanderung dieser Vögel nach Ägypten hat je...</td>\n",
" <td>0.476594</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</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",
" <td>...</td>\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>413</th>\n",
" <td>Z166069305_70</td>\n",
" <td>0.002119</td>\n",
" <td>Z166069305</td>\n",
" <td>70</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>42 ohne Thellung und ohne Abgaben die Früchte...</td>\n",
" <td>ohne Thellung und ohne Abgaben die Fruchte ihr...</td>\n",
" <td>ohne Thellung und ohne Abgaben die Früchte ihr...</td>\n",
" <td>06af6d12-1ddf-492d-8848-2537dc8b4a9c</td>\n",
" <td>[{'text_prep': 'Vorzüglich zog Frankreich groß...</td>\n",
" <td>0.794844</td>\n",
" <td>412</td>\n",
" <td>D19</td>\n",
" <td>414</td>\n",
" <td>Vorzüglich zog Frankreich große Vorteile darau...</td>\n",
" <td>0.237914</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>414</th>\n",
" <td>Z166069305_299</td>\n",
" <td>0.002114</td>\n",
" <td>Z166069305</td>\n",
" <td>299</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>271 i Auf meiner ganzen Reife habe ich und m...</td>\n",
" <td>Auf meiner ganzen Reife habe ich und meine Rei...</td>\n",
" <td>Auf meiner ganzen Reise habe ich und meine Gef...</td>\n",
" <td>01d37f8c-4a15-42a2-9ef5-a73fc7739766</td>\n",
" <td>[{'text_prep': 'Der Fluss sieht rotlich und en...</td>\n",
" <td>0.794709</td>\n",
" <td>413</td>\n",
" <td>D19</td>\n",
" <td>415</td>\n",
" <td>Der Fluss sieht rotlich und endlich grünlich a...</td>\n",
" <td>0.106832</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>415</th>\n",
" <td>Z166069305_410</td>\n",
" <td>0.002110</td>\n",
" <td>Z166069305</td>\n",
" <td>410</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>382 ilmy 1 \" mann, der feſt von dieſem Ged...</td>\n",
" <td>ilmy mann, der fest von diesem Gedanken uberze...</td>\n",
" <td>Ilmymann, der fest von diesem Gedanken überzeu...</td>\n",
" <td>dde560fc-9473-4faa-9e3c-1280ba131213</td>\n",
" <td>[{'text_prep': 'Sie hielten mich in ihrer Einb...</td>\n",
" <td>0.791636</td>\n",
" <td>414</td>\n",
" <td>D19</td>\n",
" <td>416</td>\n",
" <td>Sie hielten mich in ihrer Einbildung für einen...</td>\n",
" <td>0.140597</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>416</th>\n",
" <td>Z166069305_75</td>\n",
" <td>0.002105</td>\n",
" <td>Z166069305</td>\n",
" <td>75</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>47 3 dle ich hier gemacht habe, werden iðre ...</td>\n",
" <td>dle ich hier gemacht habe, werden ire Stelle i...</td>\n",
" <td>Die ich hier gemacht habe, werden ihre Stelle ...</td>\n",
" <td>32dd47f1-c7f4-4d4a-bb50-f428f79fce3b</td>\n",
" <td>[{'text_prep': 'Die ich hier gemacht habe, wer...</td>\n",
" <td>0.791505</td>\n",
" <td>415</td>\n",
" <td>D19</td>\n",
" <td>417</td>\n",
" <td>Die ich hier gemacht habe, werden ihre Stelle ...</td>\n",
" <td>0.136045</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>417</th>\n",
" <td>Z166069305_6</td>\n",
" <td>0.002101</td>\n",
" <td>Z166069305</td>\n",
" <td>6</td>\n",
" <td>https://iiif.onb.ac.at/images/ABO/Z166069305/0...</td>\n",
" <td>2125 murid gobiothers conale ܪܝ ، فرد. برد...</td>\n",
" <td>2125 murid gobiothers conale KOENISE KAISERLIC...</td>\n",
" <td>2255 Murdoch Gouverneurs Comte KOENIGSE KAISE...</td>\n",
" <td>21bb54fb-3e4e-456c-9605-f89970db24b7</td>\n",
" <td>[{'text_prep': '2255 Murdoch Gouverneurs Comt...</td>\n",
" <td>0.791373</td>\n",
" <td>416</td>\n",
" <td>D19</td>\n",
" <td>418</td>\n",
" <td>2255 Murdoch Gouverneurs Comte KOENIGSE KAISE...</td>\n",
" <td>0.109442</td>\n",
" <td>https://digital.onb.ac.at/OnbViewer/viewer.fac...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>418 rows × 17 columns</p>\n",
"</div>"
],
"text/plain": [
" document rrf_score barcode page \\\n",
"0 Z166069305_252 0.032258 Z166069305 252 \n",
"1 Z166069305_43 0.031025 Z166069305 43 \n",
"2 Z166069305_265 0.029911 Z166069305 265 \n",
"3 Z166069305_344 0.029412 Z166069305 344 \n",
"4 Z166069305_243 0.027778 Z166069305 243 \n",
".. ... ... ... ... \n",
"413 Z166069305_70 0.002119 Z166069305 70 \n",
"414 Z166069305_299 0.002114 Z166069305 299 \n",
"415 Z166069305_410 0.002110 Z166069305 410 \n",
"416 Z166069305_75 0.002105 Z166069305 75 \n",
"417 Z166069305_6 0.002101 Z166069305 6 \n",
"\n",
" iiif_link \\\n",
"0 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"1 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"2 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"3 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"4 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
".. ... \n",
"413 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"414 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"415 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"416 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"417 https://iiif.onb.ac.at/images/ABO/Z166069305/0... \n",
"\n",
" text_orig \\\n",
"0 224 7 Dieſer Falke war ein Weibchen. Der Ein... \n",
"1 16 5 1 entgegen, indem er mit großer Heftig... \n",
"2 237 Seeufer, an welchem wir bis zum See Maadi... \n",
"3 316 niederſchoß, wünſchte ich mir Glük, dieſe... \n",
"4 215 don -einſam liegenden Seen beleben, bleib... \n",
".. ... \n",
"413 42 ohne Thellung und ohne Abgaben die Früchte... \n",
"414 271 i Auf meiner ganzen Reife habe ich und m... \n",
"415 382 ilmy 1 \" mann, der feſt von dieſem Ged... \n",
"416 47 3 dle ich hier gemacht habe, werden iðre ... \n",
"417 2125 murid gobiothers conale ܪܝ ، فرد. برد... \n",
"\n",
" text_clean \\\n",
"0 Dieser Falke war ein Weibchen. Der Eingeweide ... \n",
"1 entgegen, indem er mit groser Heftigkeit nach ... \n",
"2 Seeufer, an welchem wir bis zum See Maadie hin... \n",
"3 niederschos, wunschte ich mir Gluk, diesen uns... \n",
"4 don -einsam liegenden Seen beleben, bleiben di... \n",
".. ... \n",
"413 ohne Thellung und ohne Abgaben die Fruchte ihr... \n",
"414 Auf meiner ganzen Reife habe ich und meine Rei... \n",
"415 ilmy mann, der fest von diesem Gedanken uberze... \n",
"416 dle ich hier gemacht habe, werden ire Stelle i... \n",
"417 2125 murid gobiothers conale KOENISE KAISERLIC... \n",
"\n",
" text_prep \\\n",
"0 Dieser Falke war ein Weibchen. Der Eingeweidek... \n",
"1 entgegen, indem er mit großer Heftigkeit nach ... \n",
"2 Seeufer, an welchem wir bis zum See Maadie hin... \n",
"3 Niederschoss, wünschte ich mir Glück, diesen u... \n",
"4 don-einsam liegenden Seen beleben, bleiben die... \n",
".. ... \n",
"413 ohne Thellung und ohne Abgaben die Früchte ihr... \n",
"414 Auf meiner ganzen Reise habe ich und meine Gef... \n",
"415 Ilmymann, der fest von diesem Gedanken überzeu... \n",
"416 Die ich hier gemacht habe, werden ihre Stelle ... \n",
"417 2255 Murdoch Gouverneurs Comte KOENIGSE KAISE... \n",
"\n",
" _id \\\n",
"0 8a5893c6-fde6-42f3-94e3-16db95629fe5 \n",
"1 4205ec0a-9cf3-448d-afba-3023e4c92052 \n",
"2 5fc895c1-b587-4442-a34c-dd446c3054f2 \n",
"3 a7debf83-ac70-445a-afee-5f35515b3e1d \n",
"4 58487609-e48c-47a1-8a0a-06569fef7f53 \n",
".. ... \n",
"413 06af6d12-1ddf-492d-8848-2537dc8b4a9c \n",
"414 01d37f8c-4a15-42a2-9ef5-a73fc7739766 \n",
"415 dde560fc-9473-4faa-9e3c-1280ba131213 \n",
"416 32dd47f1-c7f4-4d4a-bb50-f428f79fce3b \n",
"417 21bb54fb-3e4e-456c-9605-f89970db24b7 \n",
"\n",
" _highlights _score rank corpus \\\n",
"0 [{'text_prep': 'S. sono. Gmelin hat diesen Vog... 0.856740 2 D19 \n",
"1 [{'text_prep': 'Ich todtete eine gelbe Bachste... 0.852868 3/6 D19 \n",
"2 [{'text_prep': 'Auf diesem Damme stehen von Ze... 0.848943 4/10 D19 \n",
"3 [{'text_prep': 'Indessen schienen mir diese Vö... 0.843653 8 D19 \n",
"4 [{'text_prep': 'Die Wanderung dieser Vögel nac... 0.835293 24/3 D19 \n",
".. ... ... ... ... \n",
"413 [{'text_prep': 'Vorzüglich zog Frankreich groß... 0.794844 412 D19 \n",
"414 [{'text_prep': 'Der Fluss sieht rotlich und en... 0.794709 413 D19 \n",
"415 [{'text_prep': 'Sie hielten mich in ihrer Einb... 0.791636 414 D19 \n",
"416 [{'text_prep': 'Die ich hier gemacht habe, wer... 0.791505 415 D19 \n",
"417 [{'text_prep': '2255 Murdoch Gouverneurs Comt... 0.791373 416 D19 \n",
"\n",
" rerank unpacked_highlights \\\n",
"0 1 S. sono. Gmelin hat diesen Vogel in der 13. \n",
"1 2 Ich todtete eine gelbe Bachstelze ***) und ein... \n",
"2 3 Auf diesem Damme stehen von Zeit zu Zeit klein... \n",
"3 4 Indessen schienen mir diese Vögel sich mehr de... \n",
"4 5 Die Wanderung dieser Vögel nach Ägypten hat je... \n",
".. ... ... \n",
"413 414 Vorzüglich zog Frankreich große Vorteile darau... \n",
"414 415 Der Fluss sieht rotlich und endlich grünlich a... \n",
"415 416 Sie hielten mich in ihrer Einbildung für einen... \n",
"416 417 Die ich hier gemacht habe, werden ihre Stelle ... \n",
"417 418 2255 Murdoch Gouverneurs Comte KOENIGSE KAISE... \n",
"\n",
" ST_cosine_similarity onb_viewer_link \n",
"0 0.587100 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"1 0.293896 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"2 0.347629 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"3 0.471382 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"4 0.476594 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
".. ... ... \n",
"413 0.237914 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"414 0.106832 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"415 0.140597 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"416 0.136045 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"417 0.109442 https://digital.onb.ac.at/OnbViewer/viewer.fac... \n",
"\n",
"[418 rows x 17 columns]"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"final_scores_df"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "931d1a7d",
"metadata": {},
"outputs": [],
"source": [
"# Save DataFrame to CSV\n",
"final_scores_df.to_csv('data/sonnini_llm_corrected/i_onit-sonnini-DHd2025-prep-q_Pferd, Pferde.csv', index=False)\n",
"\n",
"# Save DataFrame to Excel\n",
"final_scores_df.to_excel('data/sonnini_llm_corrected/i_onit-sonnini-DHd2025-prep-q_Pferd, Pferde.xlsx', index=False, engine='openpyxl')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f4a0175",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|