diff --git a/l3/TM-Lab3.ipynb b/l3/TM-Lab3.ipynb index 2f85bbd6950ea2d87c962cdd753744ec8ebd8269..6b86042744fd2ec3b8ed6c8b4fd0308d48813625 100644 --- a/l3/TM-Lab3.ipynb +++ b/l3/TM-Lab3.ipynb @@ -37,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": { "deletable": false, "editable": false, @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": { "deletable": false, "editable": false, @@ -128,9 +128,103 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "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_id</th>\n", + " <th>sentence</th>\n", + " <th>beg</th>\n", + " <th>end</th>\n", + " <th>label</th>\n", + " </tr>\n", + " </thead>\n", + " <tbody>\n", + " <tr>\n", + " <th>0</th>\n", + " <td>0000-000</td>\n", + " <td>EU rejects German call to boycott British lamb .</td>\n", + " <td>0</td>\n", + " <td>1</td>\n", + " <td>--NME--</td>\n", + " </tr>\n", + " <tr>\n", + " <th>1</th>\n", + " <td>0000-000</td>\n", + " <td>EU rejects German call to boycott British lamb .</td>\n", + " <td>2</td>\n", + " <td>3</td>\n", + " <td>Germany</td>\n", + " </tr>\n", + " <tr>\n", + " <th>2</th>\n", + " <td>0000-000</td>\n", + " <td>EU rejects German call to boycott British lamb .</td>\n", + " <td>6</td>\n", + " <td>7</td>\n", + " <td>United_Kingdom</td>\n", + " </tr>\n", + " <tr>\n", + " <th>3</th>\n", + " <td>0000-001</td>\n", + " <td>Peter Blackburn</td>\n", + " <td>0</td>\n", + " <td>2</td>\n", + " <td>--NME--</td>\n", + " </tr>\n", + " <tr>\n", + " <th>4</th>\n", + " <td>0000-002</td>\n", + " <td>BRUSSELS 1996-08-22</td>\n", + " <td>0</td>\n", + " <td>1</td>\n", + " <td>Brussels</td>\n", + " </tr>\n", + " </tbody>\n", + "</table>\n", + "</div>" + ], + "text/plain": [ + " sentence_id sentence beg end \\\n", + "0 0000-000 EU rejects German call to boycott British lamb . 0 1 \n", + "1 0000-000 EU rejects German call to boycott British lamb . 2 3 \n", + "2 0000-000 EU rejects German call to boycott British lamb . 6 7 \n", + "3 0000-001 Peter Blackburn 0 2 \n", + "4 0000-002 BRUSSELS 1996-08-22 0 1 \n", + "\n", + " label \n", + "0 --NME-- \n", + "1 Germany \n", + "2 United_Kingdom \n", + "3 --NME-- \n", + "4 Brussels " + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "df_train.head()" ] @@ -168,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "deletable": false, "nbgrader": { @@ -184,8 +278,9 @@ }, "outputs": [], "source": [ + "\n", "def evaluation_scores(gold, pred):\n", - " \"\"\"Print precision, recall, and F1 score.\n", + " \"\"\"Return precision, recall, and F1 score.\n", " \n", " Arguments:\n", " gold: The set with the gold-standard values.\n", @@ -195,8 +290,15 @@ " A tuple or list containing the precision, recall, and F1 values\n", " (in that order), computed based on the specified sets.\n", " \"\"\"\n", - " # YOUR CODE HERE\n", - " raise NotImplementedError()" + " num_correct_pred = len([element for element in pred if element in gold])\n", + " num_wrong_pred = len([element for element in pred if element not in gold])\n", + " \n", + " precision = num_correct_pred / (num_correct_pred + num_wrong_pred)\n", + " recall = num_correct_pred / len(gold)\n", + " f1 = 2 * (precision * recall) / (precision + recall)\n", + "\n", + " return precision, recall, f1\n", + " " ] }, { @@ -210,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "deletable": false, "editable": false, @@ -248,7 +350,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "deletable": false, "editable": false, @@ -264,7 +366,27 @@ "task": false } }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Precision: 0.500, Recall: 0.333, F1: 0.400\n" + ] + }, + { + "data": { + "text/html": [ + "<div class=\"alert alert-success\"><strong>Checks have passed!</strong></div>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "# Some example NER spans, to illustrate how the evaluation function will be used later\n", "example_gold = {(\"0000-000\", 0, 1), (\"0000-000\", 2, 3), (\"0000-000\", 6, 7)}\n", @@ -297,7 +419,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "deletable": false, "editable": false, @@ -342,7 +464,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": { "deletable": false, "nbgrader": { @@ -370,13 +492,13 @@ " position of each span.\n", " \"\"\"\n", " # Hint: The Pandas method .itertuples() is useful for iterating over rows in a DataFrame\n", - " # YOUR CODE HERE\n", - " raise NotImplementedError()" + " for row in df.itertuples():\n", + " yield row[1], row[3], row[4]" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "deletable": false, "nbgrader": { @@ -395,6 +517,8 @@ }, "outputs": [], "source": [ + "from spacy.pipeline.ner import DEFAULT_NER_MODEL\n", + "\n", "def pred_spans(df):\n", " \"\"\"Run and evaluate spaCy's NER.\n", "\n", @@ -406,8 +530,13 @@ " triples consisting of the sentence id, start position, and end\n", " position of each span.\n", " \"\"\"\n", - " # YOUR CODE HERE\n", - " raise NotImplementedError()" + " config = {\n", + " \"moves\": None,\n", + " \"update_with_oracle_cut_size\": 100,\n", + " \"model\": DEFAULT_NER_MODEL,\n", + " \"incorrect_spans_key\": \"incorrect_spans\",\n", + " }\n", + " nlp.add_pipe(\"ner\", config=config)\n" ] }, { @@ -421,9 +550,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'spans_dev_pred' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[38], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m spans_dev_gold \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m(gold_spans(df_dev))\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# Compute and print the evaluation scores\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m scores \u001b[38;5;241m=\u001b[39m evaluation_scores(spans_dev_gold, \u001b[43mspans_dev_pred\u001b[49m)\n\u001b[1;32m 7\u001b[0m print_evaluation_scores(scores)\n", + "\u001b[0;31mNameError\u001b[0m: name 'spans_dev_pred' is not defined" + ] + } + ], "source": [ "# Produce the spans\n", "spans_dev_pred = set(pred_spans(df_dev))\n", @@ -1388,7 +1529,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.7" + "version": "3.11.0rc1" } }, "nbformat": 4,