{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"/home/juliafalcao\")\n",
    "sys.path.append(\"/home/juliafalcao/experiments\")\n",
    "\n",
    "from experiments.constants import * \n",
    "from experiments.utils import *\n",
    "\n",
    "import pandas as pd\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SRC, TGT = SPANISH, BASQUE\n",
    "SRC, TGT = ENGLISH, MALTESE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# batches_filepath = f\"/netscratch/falcao/data/{SRC}-{TGT}/eval-set/batches.{SRC}-{TGT}.json\"\n",
    "batches_filepath = f\"/home/juliafalcao/thesis_data/{SRC}-{TGT}/batches.{SRC}-{TGT}.json\"\n",
    "with open(batches_filepath, mode=\"r\") as f:\n",
    "    j = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_filepath = f\"/home/juliafalcao/thesis_data/{SRC}-{TGT}/df_eval.csv\"\n",
    "df_eval = pd.read_csv(df_filepath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfs = []\n",
    "for batch_json in j:\n",
    "    _df = pd.DataFrame(batch_json[\"items\"])\n",
    "    _df[\"_batchNo\"] = batch_json[\"task\"][\"batchNo\"]\n",
    "    dfs.append(_df)\n",
    "\n",
    "df = pd.concat(dfs)\n",
    "df = df[[\"_batchNo\", *df.columns[:-1]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total items: 1500\n"
     ]
    }
   ],
   "source": [
    "print(\"total items:\", len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "ids = df.groupby(\"itemID\").itemType.unique().apply(len).sort_values(ascending=False).where(lambda x: x == 3).dropna().index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Regional and seasonal severe weather phenomena include blizzards, snowstorms, ice storms, and dust storms.',\n",
       "       'The grandparents enjoyed the social aspects of learning in informal clubs with the other grandparents, and they participated very enthusiastically in the various language competitions, pairing up in a team with their own grandchild.',\n",
       "       'Supervisors should be adequately trained about risks and control measures connected with the work of young people, and made aware of any restrictions on the tasks young people can do.',\n",
       "       \"He would go on talking to anyone who listened. He just never stopped. Even if you didn't ask for it, he'd let you know exactly how he felt.\",\n",
       "       'While immigration check is usually absent or a formality when you arrive in your homeland, customs control can be a hassle.',\n",
       "       'Nowadays air travel is only rarely booked directly through the airline without first searching and comparing prices.',\n",
       "       'There were also toilets in ancient Egypt, Persia and China. In Roman civilization, toilets were sometimes part of public bath houses where men and women were together in mixed company.',\n",
       "       'If using a drone, check well in advance as to what you are allowed to film and what permits or additional licensing are required.',\n",
       "       'Starting a new job brings with it that curious mixture of trepidation at the new challenges to tackle and excitement at the opportunities that lie ahead.',\n",
       "       'Unattended bags are a target for theft and can also attract attention from authorities wary of bomb threats.',\n",
       "       'It is quite possible that the person who is in charge of the interview will ask you some tricky questions, especially about the reasons why you left other jobs or former employers.',\n",
       "       'Each show would also provide recommendations for books that children should look for when they went to their library.'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.itemID.isin(ids) & (df.itemType == \"BAD\") & (df.targetID == \"gtrans.mt\")].sourceText.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'itemType': 'REF',\n",
       "  'targetID': 'ref.mt',\n",
       "  'sourceText': 'Nowadays air travel is only rarely booked directly through the airline without first searching and comparing prices.',\n",
       "  'targetText': 'Illum il-ġurnata l-ivvjaġġar bl-ajru rari jiġi bbukkjat direttament permezz tal-linja tal-ajru mingħajr ma l-ewwel isir tiftix u paragunar tal-prezzijiet.'},\n",
       " {'itemType': 'TGT',\n",
       "  'targetID': 'gtrans.mt',\n",
       "  'sourceText': 'Nowadays air travel is only rarely booked directly through the airline without first searching and comparing prices.',\n",
       "  'targetText': 'Illum il-ġurnata l-ivvjaġġar bl-ajru huwa rari biss ibbukkjat direttament permezz tal-linja tal-ajru mingħajr ma l-ewwel wieħed ifittex u jqabbel il-prezzijiet.'},\n",
       " {'itemType': 'BAD',\n",
       "  'targetID': 'gtrans.mt',\n",
       "  'sourceText': 'Nowadays air travel is only rarely booked directly through the airline without first searching and comparing prices.',\n",
       "  'targetText': 'Illum il-ġurnata l-ivvjaġġar bl-ajru huwa rari biss ibbukkjat direttament permezz tal-linja tal-ajru mingħajr jkollok aċċess għall-aħbarijiet riċenti, fatti jqabbel il-prezzijiet.'}]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"Nowadays air travel is only rarely booked directly through the airline without first searching and comparing prices.\"\n",
    "df[(df.sourceText == s) & (df.targetID.isin([\"gtrans.mt\", \"ref.mt\"]))][[\"itemType\", \"targetID\", \"sourceText\", \"targetText\"]].to_dict(orient=\"records\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Dan is-servizz huwa b’xejn, u b’mod faċli, jista’ jkollok aċċess għall-aħbarijiet riċenti, fatti u figuri, dokumenti legali u għadd kbir ta’ informazzjoni prattika.']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df.targetText.apply(lambda x: \"kollok aċċess għall-aħbarijiet riċenti, fatti\" in x)) & (df.itemType == \"REF\")].targetText.to_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "identical hypotheses: 0\n"
     ]
    },
    {
     "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>_batchNo</th>\n",
       "      <th>_block</th>\n",
       "      <th>_item</th>\n",
       "      <th>itemID</th>\n",
       "      <th>itemType</th>\n",
       "      <th>sourceID</th>\n",
       "      <th>sourceText</th>\n",
       "      <th>targetID</th>\n",
       "      <th>targetText</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [_batchNo, _block, _item, itemID, itemType, sourceID, sourceText, targetID, targetText]\n",
       "Index: []"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "identical_hyps = df[df.targetID.apply(lambda id: \"+\" in id)]\n",
    "print(\"identical hypotheses:\", len(identical_hyps))\n",
    "identical_hyps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sourceID values: {'src.es'}\n"
     ]
    }
   ],
   "source": [
    "print(\"sourceID values:\", set(df.sourceID))\n",
    "assert len(set(df.sourceID)) == 1 # ? not sure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "targetID values: ['cmbacktrans.eu', 'euskadi.eu', 'nllb.eu', 'ref.eu', 'cmbacktrans.eu+euskadi.eu']\n"
     ]
    }
   ],
   "source": [
    "print(\"targetID values:\", df.targetID.unique().tolist())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "itemType values: {'TGT', 'REF', 'BAD'}\n",
      "value counts:\n",
      "TGT    1200\n",
      "BAD     150\n",
      "REF     150\n",
      "\n",
      "per batch:\n",
      "TGT    80\n",
      "BAD    10\n",
      "REF    10\n"
     ]
    }
   ],
   "source": [
    "print(\"itemType values:\", set(df.itemType))\n",
    "assert set(df.itemType) <= set([\"BAD\", \"CHK\", \"REF\", \"TGT\"])\n",
    "\n",
    "print(\"value counts:\")\n",
    "print(df.itemType.value_counts().to_string())\n",
    "print()\n",
    "\n",
    "print(\"per batch:\")\n",
    "_df = df[df._batchNo == 1]\n",
    "print(_df.itemType.value_counts().to_string())"
   ]
  }
 ],
 "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.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
