{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Explore data and feasibility of approach\n", "\n", "\n", "\n", "We had asked our clients and in-house experts to annotate sentences using a rigorous guideline. The aim is to decide on which sentences they would like to see in a summary for a paper.\n", "\n", "The results are in JSON format, each annotator has a separate file. Let's load them." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data/evaluation-experiment-2-stage #1-sa6a0y.json\n", "data/evaluation-experiment-2-stage #1-2m6dmb.json\n" ] } ], "source": [ "from pathlib import Path\n", "import json\n", "\n", "annotations = []\n", "for p in Path(\"data\").glob(\"*.json\"):\n", " with open(p, encoding=\"utf-8\") as f:\n", " print(p)\n", " annotations.append(json.load(f))\n", "\n", "evaluations = {\n", " sentence: [\n", " annotation[sentence] for annotation in annotations if sentence in annotation\n", " ]\n", " for sentence in {\n", " sentence for annotation in annotations for sentence in annotation.keys()\n", " }\n", "}\n", "\n", "X = [s for s in evaluations.keys()]\n", "y = [int(sum(e) > 0) for e in evaluations.values()]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the compiled and processed data for later use using LargeFileS3." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[38;5;39mCopying file for summary-train-dataset-small-0\u001b[0m\n", "\u001b[38;5;39mCompressing summary-train-dataset-small-0\u001b[0m\n", "\u001b[38;5;39mUploading summary-train-dataset-small-0 to S3 as summary-train-dataset-small/0\u001b[0m\n", "\u001b[38;5;39mUploading summary-train-dataset-small-0.tar.gz 0.04/0.04 MB (100.0%)\u001b[0m\n" ] } ], "source": [ "from great_ai.large_file import LargeFileS3\n", "import json\n", "\n", "LargeFileS3.configure_credentials_from_file(\"config.ini\")\n", "\n", "with LargeFileS3(\"summary-train-dataset-small\", \"w\", encoding=\"utf-8\") as f:\n", " json.dump((X, y), f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Filter out sentences which don't have enough annotations." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "y1 = [e[0] for e in evaluations.values() if len(e) == 2]\n", "y2 = [e[1] for e in evaluations.values() if len(e) == 2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate [Cohen's kappa](https://en.wikipedia.org/wiki/Cohen%27s_kappa).\n", "\n", "It's a bit low but the task itself is pretty subjective so it's not all that surprising." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.3546448712421808" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sklearn.metrics\n", "\n", "sklearn.metrics.cohen_kappa_score(y1, y2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can we train anything on this data?\n", "\n", "Let's try with a trivial SVM." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "X = [s for s in evaluations.keys()]\n", "y = [int(sum(e) > 0) for e in evaluations.values()]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.79, 0.75, 0.77, 0.69, 0.77])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from sklearn.model_selection import train_test_split\n", "from sklearn.svm import LinearSVC\n", "from sklearn.pipeline import Pipeline\n", "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.model_selection import cross_val_score\n", "\n", "\n", "model = Pipeline(\n", " steps=[\n", " (\"vectorizer\", TfidfVectorizer(sublinear_tf=True, min_df=3, max_df=0.3)),\n", " (\"classifier\", LinearSVC()),\n", " ]\n", ") # baseline model\n", "\n", "cross_val_score(model, X, y, cv=5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The cross-validation shows promising accuracies. But accuracy isn't everything, therefore, we should investigate the accuracy metrics." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Pipeline(steps=[('vectorizer',\n",
" TfidfVectorizer(max_df=0.3, min_df=3, sublinear_tf=True)),\n",
" ('classifier', LinearSVC())])In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. Pipeline(steps=[('vectorizer',\n",
" TfidfVectorizer(max_df=0.3, min_df=3, sublinear_tf=True)),\n",
" ('classifier', LinearSVC())])TfidfVectorizer(max_df=0.3, min_df=3, sublinear_tf=True)
LinearSVC()