{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "eba2ee81", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "No config specified, defaulting to: inspec/raw\n", "Reusing dataset inspec (/Users/boudin-f/.cache/huggingface/datasets/taln-ls2n___inspec/raw/1.0.0/0980ea60c840383eb282b6272baba681a578ed092f61438b008254c70d20f32b)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2ad1b39fd3294bcfabe57a9acf24986e", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from datasets import load_dataset\n", "\n", "dataset = load_dataset('taln-ls2n/wikinews-fr-100')" ] }, { "cell_type": "code", "execution_count": 2, "id": "4ba72244", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9bded16e4b0a43ad8907144bce073d0c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "statistics for test\n", "# keyphrases: 9.64\n", "% P: 95.91\n", "% R: 1.40\n", "% M: 0.85\n", "% U: 1.84\n" ] } ], "source": [ "from tqdm.notebook import tqdm\n", "\n", "for split in ['test']:\n", " \n", " P, R, M, U, nb_kps = [], [], [], [], []\n", " \n", " for sample in tqdm(dataset[split]):\n", " nb_kps.append(len(sample[\"keyphrases\"]))\n", " P.append(sample[\"prmu\"].count(\"P\") / nb_kps[-1])\n", " R.append(sample[\"prmu\"].count(\"R\") / nb_kps[-1])\n", " M.append(sample[\"prmu\"].count(\"M\") / nb_kps[-1])\n", " U.append(sample[\"prmu\"].count(\"U\") / nb_kps[-1])\n", " \n", " print(\"statistics for {}\".format(split))\n", " print(\"# keyphrases: {:.2f}\".format(sum(nb_kps)/len(nb_kps)))\n", " print(\"% P: {:.2f}\".format(sum(P)/len(P)*100))\n", " print(\"% R: {:.2f}\".format(sum(R)/len(R)*100))\n", " print(\"% M: {:.2f}\".format(sum(M)/len(M)*100))\n", " print(\"% U: {:.2f}\".format(sum(U)/len(U)*100))" ] }, { "cell_type": "code", "execution_count": 3, "id": "52dda817", "metadata": {}, "outputs": [], "source": [ "import spacy\n", "\n", "nlp = spacy.load(\"fr_core_news_sm\")\n", "\n", "# https://spacy.io/usage/linguistic-features#native-tokenizer-additions\n", "\n", "from spacy.lang.char_classes import ALPHA, ALPHA_LOWER, ALPHA_UPPER\n", "from spacy.lang.char_classes import CONCAT_QUOTES, LIST_ELLIPSES, LIST_ICONS\n", "from spacy.util import compile_infix_regex\n", "\n", "# Modify tokenizer infix patterns\n", "infixes = (\n", " LIST_ELLIPSES\n", " + LIST_ICONS\n", " + [\n", " r\"(?<=[0-9])[+\\-\\*^](?=[0-9-])\",\n", " r\"(?<=[{al}{q}])\\.(?=[{au}{q}])\".format(\n", " al=ALPHA_LOWER, au=ALPHA_UPPER, q=CONCAT_QUOTES\n", " ),\n", " r\"(?<=[{a}]),(?=[{a}])\".format(a=ALPHA),\n", " # ✅ Commented out regex that splits on hyphens between letters:\n", " # r\"(?<=[{a}])(?:{h})(?=[{a}])\".format(a=ALPHA, h=HYPHENS),\n", " r\"(?<=[{a}0-9])[:<>=/](?=[{a}])\".format(a=ALPHA),\n", " ]\n", ")\n", "\n", "infix_re = compile_infix_regex(infixes)\n", "nlp.tokenizer.infix_finditer = infix_re.finditer" ] }, { "cell_type": "code", "execution_count": 4, "id": "047ab1cc", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "135b8cd19d054319a445df200d82cc65", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/100 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "statistics for test\n", "avg doc len: 306.9\n" ] } ], "source": [ "for split in ['test']:\n", " doc_len = []\n", " for sample in tqdm(dataset[split]):\n", " doc_len.append(len(nlp(sample[\"title\"])) + len(nlp(sample[\"abstract\"])))\n", " \n", " print(\"statistics for {}\".format(split))\n", " print(\"avg doc len: {:.1f}\".format(sum(doc_len)/len(doc_len)))\n", " " ] } ], "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.10.2" } }, "nbformat": 4, "nbformat_minor": 5 }