{ "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\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titledateeurovoc_conceptsurllangformatstext__index_level_0__
096/493/EC: Council Decision of 29 March 1996 c...1996-03-29[UNO, forest conservation, international agree...http://publications.europa.eu/resource/cellar/...eng[fmx4, html, pdfa1b, print, xhtml]L_1996208EN. 01000101. xml 17. 8. 1996    EN O...29
1Proposal for a COUNCIL AND COMMISSION DECISION...1996-03-29[Ukraine, accession to the European Union, coo...http://publications.europa.eu/resource/cellar/...eng[pdf]COMMISSION OI l l ll I UKOIM AN COMMUNIMI S Br...48
2Proposal for a COUNCIL AND COMMISSION DECISION...1996-03-29[EU relations, Moldova, accession to the Europ...http://publications.europa.eu/resource/cellar/...eng[pdf], * ir ; COMMISSION OF THE EUROPEAN COMMUNITIE...54
3Amended proposal for a COUNCIL REGULATION (EC)...1996-03-28[Antarctic Ocean, authorised catch, conservati...http://publications.europa.eu/resource/cellar/...eng[html, pdf, pdfa1b, print]COMMISSION OF THE bUROPEAN COMMUNITIES Brussel...73
4Proposal for a COUNCIL DIRECTIVE amending Dire...1996-03-27[case law (EU), gender equality, job access, p...http://publications.europa.eu/resource/cellar/...eng[html, pdf, pdfa1b, print]COMMISSION OF THE EUROPEAN COMMUNITIES Brussel...108
...........................
352006Non-opposition to a notified concentration (Ca...2023-12-04[India, economic concentration, electric vehic...http://publications.europa.eu/resource/cellar/...eng[fmx4, pdfa2a, xhtml]C_202301503EN. 000101. fmx. xml Official Journ...21936
352007REPORT - on the proposal for a regulation of t...2023-12-04[EU statistics, European Statistical System, d...http://publications.europa.eu/resource/cellar/...eng[URI]None21940
352008Steps in the asylum procedure.2023-12-02[asylum seeker, external border of the EU, imm...http://publications.europa.eu/resource/cellar/...eng[pdf, pdfx4, print]Steps in the asylum procedure Arrival 1 Applyi...21945
352009Do you need support? :2023-12-02[aid to refugees, asylum seeker, external bord...http://publications.europa.eu/resource/cellar/...eng[pdf]Do you need support? lleuaa EUROPEAN UNION AGE...21946
352010Do you fear for your safety in your country? :...2023-12-02[aid to refugees, asylum seeker, external bord...http://publications.europa.eu/resource/cellar/...eng[pdf, pdfx4, print]Do you fear for your safety in your country? T...21947
\n", "

352011 rows × 8 columns

\n", "" ], "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": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titledateeurovoc_conceptsurllangformatstextembeddingscos_sim
29541310 trends shaping democracy in a volatile world.2019-11-21[democracy, digital technology, election, popu...http://publications.europa.eu/resource/cellar/...eng[pdf]31 October 2019 10 TRENDS SHAPING DEMOCRACY IN...[-0.7749167680740356, 5.183741569519043, -4.15...0.497865
351006Defence of democracy.2023-12-20[action programme, civic engagement, democracy...http://publications.europa.eu/resource/cellar/...eng[pdf]Defence of Democracy #EUDemocracy 12 December ...[-0.4795585572719574, 6.859988212585449, -5.87...0.451415
314572EU citizenship report 2020 : factsheet.2021-04-20[European citizenship, European election, acce...http://publications.europa.eu/resource/cellar/...eng[pdf]EU Citizenship Report 2020 F A C T S H E E T 1...[-0.9046934247016907, 5.682852745056152, -4.97...0.432170
286936Opinion of the European Economic and Social Co...2019-03-20[Liberalism, civil society, democracy, extremi...http://publications.europa.eu/resource/cellar/...eng[fmx4, pdfa2a, xhtml]C_2019228EN. 01002401. xml 5. 7. 2019    EN Of...[-0.5640900135040283, 3.6944658756256104, -4.5...0.420833
330997Making participatory and deliberative democrac...2022-06-16[EU research policy, green economy, innovation...http://publications.europa.eu/resource/cellar/...eng[pdf]#HorizonEU June 2022 MAKING PARTICIPATORY AND ...[-0.27760544419288635, 5.397331237792969, -5.9...0.419882
\n", "
" ], "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 }