{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":121150,"databundleVersionId":15833391}],"dockerImageVersionId":31329,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Imports & Setup\n# Basic libraries\nimport numpy as np\nimport pandas as pd\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Display settings\npd.set_option('display.max_columns', None)\nsns.set_style(\"whitegrid\")\n\nprint(\"Libraries loaded successfully\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:42:52.35834Z","iopub.execute_input":"2026-05-05T10:42:52.35863Z","iopub.status.idle":"2026-05-05T10:42:56.553943Z","shell.execute_reply.started":"2026-05-05T10:42:52.358594Z","shell.execute_reply":"2026-05-05T10:42:56.552987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load dataset\ntrain = pd.read_csv('/kaggle/input/competitions/deep-past-initiative-machine-translation/train.csv')\ntest = pd.read_csv('/kaggle/input/competitions/deep-past-initiative-machine-translation/test.csv')\n\nprint(\"Train shape:\", train.shape)\nprint(\"Test shape:\", test.shape)\n\ntrain.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:42:58.808428Z","iopub.execute_input":"2026-05-05T10:42:58.809154Z","iopub.status.idle":"2026-05-05T10:42:58.917921Z","shell.execute_reply.started":"2026-05-05T10:42:58.809123Z","shell.execute_reply":"2026-05-05T10:42:58.917246Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 1. Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"# Dataset Overview\n# Basic info\ntrain.info()\n\n# Describe dataset\ntrain.describe(include='all')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:43:09.029532Z","iopub.execute_input":"2026-05-05T10:43:09.029832Z","iopub.status.idle":"2026-05-05T10:43:09.064961Z","shell.execute_reply.started":"2026-05-05T10:43:09.029807Z","shell.execute_reply":"2026-05-05T10:43:09.06434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Column Understanding\nprint(\"Columns:\\n\", train.columns)\n\n# Sample rows\ntrain.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:43:20.016165Z","iopub.execute_input":"2026-05-05T10:43:20.016589Z","iopub.status.idle":"2026-05-05T10:43:20.025825Z","shell.execute_reply.started":"2026-05-05T10:43:20.016559Z","shell.execute_reply":"2026-05-05T10:43:20.025069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Missing Values Analysis\n# Missing values count\nmissing = train.isnull().sum().sort_values(ascending=False)\nmissing = missing[missing > 0]\n\nprint(missing)\n\n# Visualize missing data\nplt.figure(figsize=(10,5))\nsns.barplot(x=missing.index, y=missing.values)\nplt.xticks(rotation=90)\nplt.title(\"Missing Values per Column\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:43:29.180907Z","iopub.execute_input":"2026-05-05T10:43:29.181678Z","iopub.status.idle":"2026-05-05T10:43:29.362649Z","shell.execute_reply.started":"2026-05-05T10:43:29.181645Z","shell.execute_reply":"2026-05-05T10:43:29.362048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Duplicate Data Check\nduplicates = train.duplicated().sum()\nprint(\"Duplicate rows:\", duplicates)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:43:40.849874Z","iopub.execute_input":"2026-05-05T10:43:40.850703Z","iopub.status.idle":"2026-05-05T10:43:40.863327Z","shell.execute_reply.started":"2026-05-05T10:43:40.850665Z","shell.execute_reply":"2026-05-05T10:43:40.862533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:45:29.877685Z","iopub.execute_input":"2026-05-05T10:45:29.877951Z","iopub.status.idle":"2026-05-05T10:45:29.882233Z","shell.execute_reply.started":"2026-05-05T10:45:29.877928Z","shell.execute_reply":"2026-05-05T10:45:29.881656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Text Length Analysis\n# Length of Akkadian (transliteration) text\ntrain['akkadian_len'] = train['transliteration'].astype(str).apply(len)\n\n# Length of English text\ntrain['english_len'] = train['translation'].astype(str).apply(len)\n\ntrain[['akkadian_len', 'english_len']].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:46:55.149201Z","iopub.execute_input":"2026-05-05T10:46:55.149947Z","iopub.status.idle":"2026-05-05T10:46:55.16994Z","shell.execute_reply.started":"2026-05-05T10:46:55.149917Z","shell.execute_reply":"2026-05-05T10:46:55.16943Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Text Length Visualization\nplt.figure(figsize=(12,5))\n\nplt.subplot(1,2,1)\nsns.histplot(train['akkadian_len'], bins=50, kde=True)\nplt.title(\"Akkadian (Transliteration) Text Length Distribution\")\n\nplt.subplot(1,2,2)\nsns.histplot(train['english_len'], bins=50, kde=True)\nplt.title(\"English Translation Length Distribution\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:47:04.638152Z","iopub.execute_input":"2026-05-05T10:47:04.63882Z","iopub.status.idle":"2026-05-05T10:47:05.071877Z","shell.execute_reply.started":"2026-05-05T10:47:04.638789Z","shell.execute_reply":"2026-05-05T10:47:05.071264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Word Count Analysis\n# Word counts\ntrain['akkadian_words'] = train['transliteration'].astype(str).apply(lambda x: len(x.split()))\ntrain['english_words'] = train['translation'].astype(str).apply(lambda x: len(x.split()))\n\ntrain[['akkadian_words', 'english_words']].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:47:14.389087Z","iopub.execute_input":"2026-05-05T10:47:14.389814Z","iopub.status.idle":"2026-05-05T10:47:14.427022Z","shell.execute_reply.started":"2026-05-05T10:47:14.389783Z","shell.execute_reply":"2026-05-05T10:47:14.426318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Word Count Visualization\nplt.figure(figsize=(12,5))\n\nplt.subplot(1,2,1)\nsns.histplot(train['akkadian_words'], bins=50, kde=True)\nplt.title(\"Akkadian Word Count\")\n\nplt.subplot(1,2,2)\nsns.histplot(train['english_words'], bins=50, kde=True)\nplt.title(\"English Word Count\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:47:21.428829Z","iopub.execute_input":"2026-05-05T10:47:21.429428Z","iopub.status.idle":"2026-05-05T10:47:21.955458Z","shell.execute_reply.started":"2026-05-05T10:47:21.429399Z","shell.execute_reply":"2026-05-05T10:47:21.954837Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Most Common Words (English)\nfrom collections import Counter\n\nall_words = \" \".join(train['translation'].astype(str)).split()\nword_freq = Counter(all_words)\n\ncommon_words = pd.DataFrame(word_freq.most_common(20), columns=['word', 'count'])\n\nplt.figure(figsize=(10,5))\nsns.barplot(x='count', y='word', data=common_words)\nplt.title(\"Top 20 Most Common English Words\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:47:30.245838Z","iopub.execute_input":"2026-05-05T10:47:30.246543Z","iopub.status.idle":"2026-05-05T10:47:30.485652Z","shell.execute_reply.started":"2026-05-05T10:47:30.246512Z","shell.execute_reply":"2026-05-05T10:47:30.48471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Akkadian Token Pattern Analysis\n# Inspect transliteration samples\ntrain['transliteration'].head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:47:41.602861Z","iopub.execute_input":"2026-05-05T10:47:41.603151Z","iopub.status.idle":"2026-05-05T10:47:41.609625Z","shell.execute_reply.started":"2026-05-05T10:47:41.603126Z","shell.execute_reply":"2026-05-05T10:47:41.608953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Special Character Analysis\nimport re\n\ndef count_special(text):\n    return len(re.findall(r'[^a-zA-Z0-9\\s]', str(text)))\n\ntrain['akkadian_special'] = train['transliteration'].apply(count_special)\n\ntrain['akkadian_special'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:48:04.24868Z","iopub.execute_input":"2026-05-05T10:48:04.249111Z","iopub.status.idle":"2026-05-05T10:48:04.305023Z","shell.execute_reply.started":"2026-05-05T10:48:04.249081Z","shell.execute_reply":"2026-05-05T10:48:04.304467Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Correlation Analysis\ncorr = train[['akkadian_len', 'english_len', 'akkadian_words', 'english_words']].corr()\n\nplt.figure(figsize=(8,5))\nsns.heatmap(corr, annot=True, cmap='coolwarm')\nplt.title(\"Correlation Matrix\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:49:01.53384Z","iopub.execute_input":"2026-05-05T10:49:01.534721Z","iopub.status.idle":"2026-05-05T10:49:01.716503Z","shell.execute_reply.started":"2026-05-05T10:49:01.534677Z","shell.execute_reply":"2026-05-05T10:49:01.715729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Display random samples\nfor i in range(5):\n    print(\"Akkadian:\", train['transliteration'].iloc[i])\n    print(\"English :\", train['translation'].iloc[i])\n    print(\"-\"*80)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:55:27.294838Z","iopub.execute_input":"2026-05-05T10:55:27.295126Z","iopub.status.idle":"2026-05-05T10:55:27.300386Z","shell.execute_reply.started":"2026-05-05T10:55:27.295103Z","shell.execute_reply":"2026-05-05T10:55:27.29976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get all unique characters in transliteration\nall_chars = set(\"\".join(train['transliteration'].astype(str)))\n\nprint(\"Total unique characters:\", len(all_chars))\nprint(sorted(all_chars))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:55:53.739539Z","iopub.execute_input":"2026-05-05T10:55:53.740243Z","iopub.status.idle":"2026-05-05T10:55:53.754913Z","shell.execute_reply.started":"2026-05-05T10:55:53.740214Z","shell.execute_reply":"2026-05-05T10:55:53.7541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef pattern_counts(text):\n    text = str(text)\n    return {\n        \"has_numbers\": bool(re.search(r'\\d', text)),\n        \"has_hyphen\": \"-\" in text,\n        \"has_uppercase\": bool(re.search(r'[A-Z]', text)),\n        \"has_dot\": \".\" in text\n    }\n\npattern_df = train['transliteration'].apply(pattern_counts).apply(pd.Series)\n\npattern_df.mean()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:55:59.992275Z","iopub.execute_input":"2026-05-05T10:55:59.993152Z","iopub.status.idle":"2026-05-05T10:56:00.220926Z","shell.execute_reply.started":"2026-05-05T10:55:59.993119Z","shell.execute_reply":"2026-05-05T10:56:00.220318Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Token examples\ntokens = train['transliteration'].astype(str).apply(lambda x: x.split())\n\n# Flatten tokens\nall_tokens = [token for sublist in tokens for token in sublist]\n\nprint(\"Total tokens:\", len(all_tokens))\nprint(\"Unique tokens:\", len(set(all_tokens)))\n\n# Sample tokens\nlist(set(all_tokens))[:50]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:56:09.907319Z","iopub.execute_input":"2026-05-05T10:56:09.907896Z","iopub.status.idle":"2026-05-05T10:56:09.934259Z","shell.execute_reply.started":"2026-05-05T10:56:09.907859Z","shell.execute_reply":"2026-05-05T10:56:09.933525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import Counter\n\ntoken_freq = Counter(all_tokens)\ncommon_tokens = pd.DataFrame(token_freq.most_common(20), columns=['token', 'count'])\n\nplt.figure(figsize=(10,5))\nsns.barplot(x='count', y='token', data=common_tokens)\nplt.title(\"Top 20 Most Common Akkadian Tokens\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:56:19.92272Z","iopub.execute_input":"2026-05-05T10:56:19.923435Z","iopub.status.idle":"2026-05-05T10:56:20.154086Z","shell.execute_reply.started":"2026-05-05T10:56:19.923407Z","shell.execute_reply":"2026-05-05T10:56:20.153288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['length_ratio'] = train['english_len'] / (train['akkadian_len'] + 1)\n\ntrain['length_ratio'].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:56:27.414218Z","iopub.execute_input":"2026-05-05T10:56:27.414841Z","iopub.status.idle":"2026-05-05T10:56:27.423592Z","shell.execute_reply.started":"2026-05-05T10:56:27.41481Z","shell.execute_reply":"2026-05-05T10:56:27.422797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Extremely long sequences\nlong_texts = train[train['akkadian_len'] > train['akkadian_len'].quantile(0.99)]\n\nprint(\"Number of extreme long samples:\", len(long_texts))\n\nlong_texts[['transliteration', 'translation']].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:56:37.917716Z","iopub.execute_input":"2026-05-05T10:56:37.917967Z","iopub.status.idle":"2026-05-05T10:56:37.937049Z","shell.execute_reply.started":"2026-05-05T10:56:37.917945Z","shell.execute_reply":"2026-05-05T10:56:37.936218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"empty_akkadian = (train['transliteration'].astype(str).str.strip() == \"\").sum()\nempty_english = (train['translation'].astype(str).str.strip() == \"\").sum()\n\nprint(\"Empty Akkadian:\", empty_akkadian)\nprint(\"Empty English :\", empty_english)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:56:45.228471Z","iopub.execute_input":"2026-05-05T10:56:45.229036Z","iopub.status.idle":"2026-05-05T10:56:45.236821Z","shell.execute_reply.started":"2026-05-05T10:56:45.229006Z","shell.execute_reply":"2026-05-05T10:56:45.235952Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\"\"\"\n\nKey Observations\n\n1. Transliteration and translation lengths vary significantly → padding required\n2. Positive correlation between input and output lengths → suitable for seq2seq models\n3. Presence of special characters and symbols in transliteration → cleaning needed\n4. Word distribution is skewed → normalization may help\n5. Dataset appears clean with minimal missing values\n\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2026-05-05T10:49:27.675847Z","iopub.execute_input":"2026-05-05T10:49:27.676177Z","iopub.status.idle":"2026-05-05T10:49:27.681534Z","shell.execute_reply.started":"2026-05-05T10:49:27.676152Z","shell.execute_reply":"2026-05-05T10:49:27.680771Z"}}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2. Preprocessing Pipeline","metadata":{}},{"cell_type":"code","source":"def print_tree(startpath):\n    for root, dirs, files in os.walk(startpath):\n        level = root.replace(startpath, '').count(os.sep)\n        indent = ' ' * 4 * level\n        print(f\"{indent} {os.path.basename(root)}\")\n        subindent = ' ' * 4 * (level + 1)\n        for f in files:\n            print(f\"{subindent} {f}\")\n\nprint_tree(\"/kaggle/input\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T10:58:38.276874Z","iopub.execute_input":"2026-05-05T10:58:38.27755Z","iopub.status.idle":"2026-05-05T10:58:38.284928Z","shell.execute_reply.started":"2026-05-05T10:58:38.27751Z","shell.execute_reply":"2026-05-05T10:58:38.284292Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Required Files\n\nimport pandas as pd\nimport numpy as np\nimport re\n\nDATA_PATH = \"/kaggle/input/competitions/deep-past-initiative-machine-translation\"\n\ntrain = pd.read_csv(f\"{DATA_PATH}/train.csv\")\ntest = pd.read_csv(f\"{DATA_PATH}/test.csv\")\n\n# Optional lexical resources\nlexicon = pd.read_csv(f\"{DATA_PATH}/OA_Lexicon_eBL.csv\")\ndictionary = pd.read_csv(f\"{DATA_PATH}/eBL_Dictionary.csv\")\n\nprint(\"Train shape:\", train.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:03:59.922285Z","iopub.execute_input":"2026-05-05T11:03:59.922969Z","iopub.status.idle":"2026-05-05T11:04:00.135296Z","shell.execute_reply.started":"2026-05-05T11:03:59.922938Z","shell.execute_reply":"2026-05-05T11:04:00.134713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Rename Columns (Standardization)\n\ntrain = train.rename(columns={\n    'transliteration': 'akkadian',\n    'translation': 'english'\n})\n\ntest = test.rename(columns={\n    'transliteration': 'akkadian'\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:04:15.120391Z","iopub.execute_input":"2026-05-05T11:04:15.121262Z","iopub.status.idle":"2026-05-05T11:04:15.126334Z","shell.execute_reply.started":"2026-05-05T11:04:15.121226Z","shell.execute_reply":"2026-05-05T11:04:15.125606Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Basic Cleaning\n\ndef clean_akkadian(text):\n    text = str(text)\n\n    # normalize spaces\n    text = re.sub(r'\\s+', ' ', text)\n\n    # keep hyphens (important!)\n    # remove weird unwanted chars but KEEP structure\n    text = re.sub(r'[^\\w\\s\\-\\.\\[\\]]', '', text)\n\n    return text.strip()\n\n\ndef clean_english(text):\n    text = str(text).lower()\n\n    # remove unwanted chars\n    text = re.sub(r'[^a-z0-9\\s]', '', text)\n\n    text = re.sub(r'\\s+', ' ', text)\n\n    return text.strip()\n\n\ntrain['akkadian'] = train['akkadian'].apply(clean_akkadian)\ntrain['english'] = train['english'].apply(clean_english)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:04:40.218603Z","iopub.execute_input":"2026-05-05T11:04:40.218882Z","iopub.status.idle":"2026-05-05T11:04:40.358463Z","shell.execute_reply.started":"2026-05-05T11:04:40.21886Z","shell.execute_reply":"2026-05-05T11:04:40.357918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Remove Bad Rows\n\n# Remove empty rows\ntrain = train[\n    (train['akkadian'].str.strip() != \"\") &\n    (train['english'].str.strip() != \"\")\n]\n\n# Remove duplicates\ntrain = train.drop_duplicates()\n\nprint(\"Cleaned shape:\", train.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:04:54.508662Z","iopub.execute_input":"2026-05-05T11:04:54.509054Z","iopub.status.idle":"2026-05-05T11:04:54.525421Z","shell.execute_reply.started":"2026-05-05T11:04:54.509025Z","shell.execute_reply":"2026-05-05T11:04:54.524713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Length Filtering\n\ntrain['akk_len'] = train['akkadian'].apply(len)\ntrain['eng_len'] = train['english'].apply(len)\n\n# Keep reasonable range\ntrain = train[\n    (train['akk_len'] > 3) &\n    (train['akk_len'] < 500) &\n    (train['eng_len'] > 3) &\n    (train['eng_len'] < 500)\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:05:12.602252Z","iopub.execute_input":"2026-05-05T11:05:12.602777Z","iopub.status.idle":"2026-05-05T11:05:12.611375Z","shell.execute_reply.started":"2026-05-05T11:05:12.602745Z","shell.execute_reply":"2026-05-05T11:05:12.610789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Token-Level Normalization\n\ndef normalize_tokens(text):\n    tokens = text.split()\n\n    # lowercase only normal words, keep symbolic tokens intact\n    tokens = [t if t.isupper() else t.lower() for t in tokens]\n\n    return \" \".join(tokens)\n\ntrain['akkadian'] = train['akkadian'].apply(normalize_tokens)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:05:49.146229Z","iopub.execute_input":"2026-05-05T11:05:49.147092Z","iopub.status.idle":"2026-05-05T11:05:49.162725Z","shell.execute_reply.started":"2026-05-05T11:05:49.14706Z","shell.execute_reply":"2026-05-05T11:05:49.161881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Lexicon Enhancement \n# Try mapping rare tokens using dictionary (if columns exist)\n\nprint(dictionary.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:06:09.719853Z","iopub.execute_input":"2026-05-05T11:06:09.72013Z","iopub.status.idle":"2026-05-05T11:06:09.725047Z","shell.execute_reply.started":"2026-05-05T11:06:09.720108Z","shell.execute_reply":"2026-05-05T11:06:09.723941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train / Validation Split\nfrom sklearn.model_selection import train_test_split\n\ntrain_df, val_df = train_test_split(train, test_size=0.1, random_state=42)\n\nprint(\"Train:\", train_df.shape)\nprint(\"Val:\", val_df.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:06:55.478691Z","iopub.execute_input":"2026-05-05T11:06:55.479314Z","iopub.status.idle":"2026-05-05T11:06:55.870354Z","shell.execute_reply.started":"2026-05-05T11:06:55.479284Z","shell.execute_reply":"2026-05-05T11:06:55.86953Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare Final Data Format\ntrain_df = train_df[['akkadian', 'english']]\nval_df = val_df[['akkadian', 'english']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:07:23.70136Z","iopub.execute_input":"2026-05-05T11:07:23.702384Z","iopub.status.idle":"2026-05-05T11:07:23.708255Z","shell.execute_reply.started":"2026-05-05T11:07:23.702354Z","shell.execute_reply":"2026-05-05T11:07:23.707414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Save Processed Data\ntrain_df.to_csv(\"train_processed.csv\", index=False)\nval_df.to_csv(\"val_processed.csv\", index=False)\n\nprint(\"Saved processed datasets\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:07:36.732742Z","iopub.execute_input":"2026-05-05T11:07:36.733371Z","iopub.status.idle":"2026-05-05T11:07:36.761873Z","shell.execute_reply.started":"2026-05-05T11:07:36.733344Z","shell.execute_reply":"2026-05-05T11:07:36.761286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3. Model Training","metadata":{}},{"cell_type":"code","source":"# Install & Imports\n\n!pip install -q transformers datasets sentencepiece sacrebleu","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:10:17.512065Z","iopub.execute_input":"2026-05-05T11:10:17.512888Z","iopub.status.idle":"2026-05-05T11:10:24.382326Z","shell.execute_reply.started":"2026-05-05T11:10:17.512856Z","shell.execute_reply":"2026-05-05T11:10:24.381615Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom datasets import Dataset\nfrom transformers import T5Tokenizer, T5ForConditionalGeneration, Trainer, TrainingArguments\n\nimport torch","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:11:14.674192Z","iopub.execute_input":"2026-05-05T11:11:14.675224Z","iopub.status.idle":"2026-05-05T11:11:14.679138Z","shell.execute_reply.started":"2026-05-05T11:11:14.675189Z","shell.execute_reply":"2026-05-05T11:11:14.678286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Processed Data\ntrain_df = pd.read_csv(\"train_processed.csv\")\nval_df = pd.read_csv(\"val_processed.csv\")\n\ntrain_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:11:16.1147Z","iopub.execute_input":"2026-05-05T11:11:16.115386Z","iopub.status.idle":"2026-05-05T11:11:16.135789Z","shell.execute_reply.started":"2026-05-05T11:11:16.115355Z","shell.execute_reply":"2026-05-05T11:11:16.135191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert to HuggingFace Dataset\n\ntrain_dataset = Dataset.from_pandas(train_df)\nval_dataset = Dataset.from_pandas(val_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:03:51.109908Z","iopub.execute_input":"2026-05-05T12:03:51.110813Z","iopub.status.idle":"2026-05-05T12:03:51.127172Z","shell.execute_reply.started":"2026-05-05T12:03:51.110766Z","shell.execute_reply":"2026-05-05T12:03:51.126397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Tokenizer\n\ntokenizer = T5Tokenizer.from_pretrained(\"t5-small\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:03:56.272114Z","iopub.execute_input":"2026-05-05T12:03:56.272946Z","iopub.status.idle":"2026-05-05T12:03:57.008038Z","shell.execute_reply.started":"2026-05-05T12:03:56.272902Z","shell.execute_reply":"2026-05-05T12:03:57.007207Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Add Task Prefix\n\ndef add_prefix(example):\n    example['input_text'] = \"translate Akkadian to English: \" + example['akkadian']\n    example['target_text'] = example['english']\n    return example\n\ntrain_dataset = train_dataset.map(add_prefix)\nval_dataset = val_dataset.map(add_prefix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:03:57.74498Z","iopub.execute_input":"2026-05-05T12:03:57.745701Z","iopub.status.idle":"2026-05-05T12:03:57.832033Z","shell.execute_reply.started":"2026-05-05T12:03:57.745659Z","shell.execute_reply":"2026-05-05T12:03:57.831152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tokenization\n\nmax_input_length = 128\nmax_target_length = 128\n\ndef tokenize_function(example):\n    inputs = tokenizer(\n        example['input_text'],\n        max_length=max_input_length,\n        padding=\"max_length\",\n        truncation=True\n    )\n    \n    targets = tokenizer(\n        example['target_text'],\n        max_length=max_target_length,\n        padding=\"max_length\",\n        truncation=True\n    )\n    \n    inputs[\"labels\"] = targets[\"input_ids\"]\n    return inputs\n\ntrain_dataset = train_dataset.map(tokenize_function, batched=True)\nval_dataset = val_dataset.map(tokenize_function, batched=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:00.156521Z","iopub.execute_input":"2026-05-05T12:04:00.1573Z","iopub.status.idle":"2026-05-05T12:04:00.873325Z","shell.execute_reply.started":"2026-05-05T12:04:00.157271Z","shell.execute_reply":"2026-05-05T12:04:00.872535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Model\n\nmodel = T5ForConditionalGeneration.from_pretrained(\"t5-small\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:02.372839Z","iopub.execute_input":"2026-05-05T12:04:02.373657Z","iopub.status.idle":"2026-05-05T12:04:02.854355Z","shell.execute_reply.started":"2026-05-05T12:04:02.373624Z","shell.execute_reply":"2026-05-05T12:04:02.85343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training Arguments\n\ntraining_args = TrainingArguments(\n    output_dir=\"./results\",\n    eval_strategy=\"epoch\",\n\n    learning_rate=1e-4,\n    per_device_train_batch_size=8,\n    per_device_eval_batch_size=8,\n\n    num_train_epochs=10,\n    weight_decay=0.01,\n\n    logging_steps=20,\n    save_strategy=\"epoch\",\n\n    load_best_model_at_end=True,\n    metric_for_best_model=\"eval_loss\",\n\n    fp16=torch.cuda.is_available(),\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:04.759971Z","iopub.execute_input":"2026-05-05T12:04:04.760672Z","iopub.status.idle":"2026-05-05T12:04:04.79989Z","shell.execute_reply.started":"2026-05-05T12:04:04.760628Z","shell.execute_reply":"2026-05-05T12:04:04.79911Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Metrics tracking\n\n# def compute_metrics(eval_pred):\n#     logits, labels = eval_pred\n\n#     preds = np.argmax(logits, axis=-1)\n\n#     decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)\n#     decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)\n\n#     bleu = evaluate.load(\"sacrebleu\")\n#     score = bleu.compute(\n#         predictions=decoded_preds,\n#         references=[[l] for l in decoded_labels]\n#     )\n\n#     return {\"bleu\": score[\"score\"]}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:08.893356Z","iopub.execute_input":"2026-05-05T12:04:08.893749Z","iopub.status.idle":"2026-05-05T12:04:08.897959Z","shell.execute_reply.started":"2026-05-05T12:04:08.893708Z","shell.execute_reply":"2026-05-05T12:04:08.897068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Trainer Setup\n\nfrom transformers import Trainer, DataCollatorForSeq2Seq\n\ndata_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=model)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=train_dataset,\n    eval_dataset=val_dataset,\n    data_collator=data_collator,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:09.375447Z","iopub.execute_input":"2026-05-05T12:04:09.376333Z","iopub.status.idle":"2026-05-05T12:04:09.553142Z","shell.execute_reply.started":"2026-05-05T12:04:09.376291Z","shell.execute_reply":"2026-05-05T12:04:09.55224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train Model\n\ntrainer.train()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:04:11.206964Z","iopub.execute_input":"2026-05-05T12:04:11.207617Z","iopub.status.idle":"2026-05-05T12:06:39.858036Z","shell.execute_reply.started":"2026-05-05T12:04:11.207584Z","shell.execute_reply":"2026-05-05T12:06:39.857401Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t5_model = model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:07:55.56856Z","iopub.execute_input":"2026-05-05T12:07:55.568971Z","iopub.status.idle":"2026-05-05T12:07:55.572426Z","shell.execute_reply.started":"2026-05-05T12:07:55.568941Z","shell.execute_reply":"2026-05-05T12:07:55.571824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nSAVE_DIR_T5 = \"/kaggle/working/t5_akkadian_model\"\n\nos.makedirs(SAVE_DIR_T5, exist_ok=True)\n\nt5_model.save_pretrained(SAVE_DIR_T5)\ntokenizer.save_pretrained(SAVE_DIR_T5)\n\nprint(\"T5 model saved at:\", SAVE_DIR_T5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:08:09.441659Z","iopub.execute_input":"2026-05-05T12:08:09.441934Z","iopub.status.idle":"2026-05-05T12:08:09.955929Z","shell.execute_reply.started":"2026-05-05T12:08:09.441911Z","shell.execute_reply":"2026-05-05T12:08:09.955308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n\n# logs = trainer.state.log_history\n\n# train_loss = []\n# eval_loss = []\n# epochs = []\n\n# for log in logs:\n#     if \"loss\" in log:\n#         train_loss.append(log[\"loss\"])\n#     if \"eval_loss\" in log:\n#         eval_loss.append(log[\"eval_loss\"])\n#         epochs.append(log[\"epoch\"])\n\n# plt.figure(figsize=(10,5))\n# plt.plot(range(len(train_loss)), train_loss, label=\"Train Loss\")\n# plt.plot(range(len(eval_loss)), eval_loss, label=\"Val Loss\")\n# plt.legend()\n# plt.title(\"Training vs Validation Loss\")\n# plt.xlabel(\"Steps\")\n# plt.ylabel(\"Loss\")\n# plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:37:33.894955Z","iopub.execute_input":"2026-05-05T11:37:33.895404Z","iopub.status.idle":"2026-05-05T11:37:34.039611Z","shell.execute_reply.started":"2026-05-05T11:37:33.895371Z","shell.execute_reply":"2026-05-05T11:37:34.03894Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Evaluation of Model 1","metadata":{"execution":{"iopub.status.busy":"2026-05-05T11:19:18.428775Z","iopub.execute_input":"2026-05-05T11:19:18.429354Z","iopub.status.idle":"2026-05-05T11:19:18.43251Z","shell.execute_reply.started":"2026-05-05T11:19:18.429324Z","shell.execute_reply":"2026-05-05T11:19:18.431858Z"}}},{"cell_type":"code","source":"!pip install -q evaluate","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:19:23.973401Z","iopub.execute_input":"2026-05-05T11:19:23.97443Z","iopub.status.idle":"2026-05-05T11:19:27.821906Z","shell.execute_reply.started":"2026-05-05T11:19:23.974397Z","shell.execute_reply":"2026-05-05T11:19:27.820752Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import evaluate\nbleu = evaluate.load(\"sacrebleu\")\n\ndef evaluate_full():\n    predictions = []\n    references = []\n\n    for i in range(len(val_df)):\n        input_text = val_df.iloc[i]['akkadian']\n        target_text = val_df.iloc[i]['english']\n\n        inputs = tokenizer(\n            \"translate Akkadian to English: \" + input_text,\n            return_tensors=\"pt\",\n            truncation=True,\n            padding=True\n        ).to(model.device)\n\n        outputs = model.generate(\n            **inputs,\n            max_length=128,\n            num_beams=4, \n            early_stopping=True\n        )\n\n        pred = tokenizer.decode(outputs[0], skip_special_tokens=True)\n\n        predictions.append(pred)\n        references.append([target_text])\n\n    score = bleu.compute(predictions=predictions, references=references)\n\n    print(\"Final BLEU Score:\", score)\n\nevaluate_full()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:38:02.389303Z","iopub.execute_input":"2026-05-05T11:38:02.390339Z","iopub.status.idle":"2026-05-05T11:39:44.06461Z","shell.execute_reply.started":"2026-05-05T11:38:02.390283Z","shell.execute_reply":"2026-05-05T11:39:44.063705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate(text):\n    inputs = tokenizer(\n        \"translate Akkadian to English: \" + text,\n        return_tensors=\"pt\",\n        truncation=True,\n        padding=True\n    ).to(model.device)\n\n    outputs = model.generate(\n        **inputs,\n        max_length=128,\n        num_beams=4,\n        early_stopping=True\n    )\n\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:40:57.102112Z","iopub.execute_input":"2026-05-05T11:40:57.102962Z","iopub.status.idle":"2026-05-05T11:40:57.107641Z","shell.execute_reply.started":"2026-05-05T11:40:57.102912Z","shell.execute_reply":"2026-05-05T11:40:57.106839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\n\ndef test_random_samples(n=5):\n    indices = random.sample(range(len(val_df)), n)\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n        pred = translate(inp)\n\n        print(\"INPUT :\", inp)\n        print(\"ACTUAL:\", actual)\n        print(\"PRED  :\", pred)\n        print(\"-\"*80)\n\ntest_random_samples(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:41:02.318291Z","iopub.execute_input":"2026-05-05T11:41:02.318994Z","iopub.status.idle":"2026-05-05T11:41:06.951182Z","shell.execute_reply.started":"2026-05-05T11:41:02.31896Z","shell.execute_reply":"2026-05-05T11:41:06.950536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def compare_multiple(n=5):\n    import random\n    indices = random.sample(range(len(val_df)), n)\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n        pred = translate(inp)\n\n        print(f\"\\n🔹 Sample {i}\")\n        print(\"INPUT     :\", inp)\n        print(\"ACTUAL    :\", actual)\n        print(\"PREDICTED :\", pred)\n        print(\"=\"*100)\n\ncompare_multiple(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:41:52.636445Z","iopub.execute_input":"2026-05-05T11:41:52.637055Z","iopub.status.idle":"2026-05-05T11:41:57.725731Z","shell.execute_reply.started":"2026-05-05T11:41:52.637022Z","shell.execute_reply":"2026-05-05T11:41:57.725005Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# logs = trainer.state.log_history\n\n# train_loss = []\n# eval_loss = []\n\n# for log in logs:\n#     if \"loss\" in log:\n#         train_loss.append(log[\"loss\"])\n#     if \"eval_loss\" in log:\n#         eval_loss.append(log[\"eval_loss\"])\n\n# plt.plot(train_loss, label=\"Train Loss\")\n# plt.plot(eval_loss, label=\"Val Loss\")\n# plt.legend()\n# plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:42:30.211725Z","iopub.execute_input":"2026-05-05T11:42:30.212154Z","iopub.status.idle":"2026-05-05T11:42:30.323756Z","shell.execute_reply.started":"2026-05-05T11:42:30.212124Z","shell.execute_reply":"2026-05-05T11:42:30.3229Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# generate predictions for T5 model\n\nt5_predictions = []\n\nfor i in range(len(test_df)):\n    text = test_df.iloc[i]['akkadian']\n    pred = translate_t5(text)\n    t5_predictions.append(pred)\n\ntest_df['t5_prediction'] = t5_predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:17:38.600217Z","iopub.execute_input":"2026-05-05T12:17:38.60089Z","iopub.status.idle":"2026-05-05T12:17:41.850523Z","shell.execute_reply.started":"2026-05-05T12:17:38.600851Z","shell.execute_reply":"2026-05-05T12:17:41.849545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"t5_submission = pd.DataFrame({\n    \"id\": test_df[\"id\"],\n    \"translation\": test_df[\"t5_prediction\"]\n})\n\nt5_submission.to_csv(\"t5_predictions.csv\", index=False)\n\nprint(\" T5 submission saved\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:17:52.390088Z","iopub.execute_input":"2026-05-05T12:17:52.390845Z","iopub.status.idle":"2026-05-05T12:17:52.396326Z","shell.execute_reply.started":"2026-05-05T12:17:52.390812Z","shell.execute_reply":"2026-05-05T12:17:52.395638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate_t5(text):\n    device = next(t5_model.parameters()).device\n\n    inputs = tokenizer(\n        \"translate Akkadian to English: \" + text,\n        return_tensors=\"pt\",\n        truncation=True,\n        padding=True\n    ).to(device)\n\n    outputs = t5_model.generate(\n        **inputs,\n        max_length=128,\n        num_beams=4,\n        early_stopping=True\n    )\n\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:10:20.393661Z","iopub.execute_input":"2026-05-05T12:10:20.394348Z","iopub.status.idle":"2026-05-05T12:10:20.398889Z","shell.execute_reply.started":"2026-05-05T12:10:20.394316Z","shell.execute_reply":"2026-05-05T12:10:20.398116Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model 2 (Encoder (LSTM) - Decoder (LSTM) - Seq2Seq)","metadata":{}},{"cell_type":"code","source":"# imports\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\n\nimport pandas as pd\nimport numpy as np\nfrom collections import Counter\nimport random","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:36.668866Z","iopub.execute_input":"2026-05-05T12:13:36.669274Z","iopub.status.idle":"2026-05-05T12:13:36.673379Z","shell.execute_reply.started":"2026-05-05T12:13:36.669243Z","shell.execute_reply":"2026-05-05T12:13:36.672495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load Data\n\ntrain_df = pd.read_csv(\"train_processed.csv\")\nval_df = pd.read_csv(\"val_processed.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:36.997792Z","iopub.execute_input":"2026-05-05T12:13:36.998186Z","iopub.status.idle":"2026-05-05T12:13:37.015648Z","shell.execute_reply.started":"2026-05-05T12:13:36.998156Z","shell.execute_reply":"2026-05-05T12:13:37.014804Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tokenization\n\ndef tokenize(text):\n    return text.lower().split()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:37.399128Z","iopub.execute_input":"2026-05-05T12:13:37.399841Z","iopub.status.idle":"2026-05-05T12:13:37.403196Z","shell.execute_reply.started":"2026-05-05T12:13:37.399809Z","shell.execute_reply":"2026-05-05T12:13:37.402634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Build Vocabulary\n\ndef build_vocab(sentences, min_freq=2):\n    counter = Counter()\n    for sent in sentences:\n        counter.update(tokenize(sent))\n\n    vocab = {\n        \"<pad>\":0,\n        \"<sos>\":1,\n        \"<eos>\":2,\n        \"<unk>\":3\n    }\n\n    for word, freq in counter.items():\n        if freq >= min_freq:\n            vocab[word] = len(vocab)\n\n    return vocab\n\nsrc_vocab = build_vocab(train_df['akkadian'])\ntgt_vocab = build_vocab(train_df['english'])\n\ninv_tgt_vocab = {i:w for w,i in tgt_vocab.items()}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:39.120691Z","iopub.execute_input":"2026-05-05T12:13:39.121212Z","iopub.status.idle":"2026-05-05T12:13:39.14413Z","shell.execute_reply.started":"2026-05-05T12:13:39.121183Z","shell.execute_reply":"2026-05-05T12:13:39.143201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Numerical Encoding\n\ndef encode(sentence, vocab, max_len=50):\n    tokens = tokenize(sentence)\n    ids = [vocab.get(t, vocab[\"<unk>\"]) for t in tokens]\n\n    ids = [vocab[\"<sos>\"]] + ids[:max_len-2] + [vocab[\"<eos>\"]]\n\n    while len(ids) < max_len:\n        ids.append(vocab[\"<pad>\"])\n\n    return ids","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:40.609702Z","iopub.execute_input":"2026-05-05T12:13:40.61015Z","iopub.status.idle":"2026-05-05T12:13:40.615104Z","shell.execute_reply.started":"2026-05-05T12:13:40.61012Z","shell.execute_reply":"2026-05-05T12:13:40.614518Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dataset Class\n\nclass TranslationDataset(torch.utils.data.Dataset):\n    def __init__(self, df):\n        self.src = df['akkadian'].values\n        self.tgt = df['english'].values\n\n    def __len__(self):\n        return len(self.src)\n\n    def __getitem__(self, idx):\n        src = torch.tensor(encode(self.src[idx], src_vocab))\n        tgt = torch.tensor(encode(self.tgt[idx], tgt_vocab))\n        return src, tgt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:42.199581Z","iopub.execute_input":"2026-05-05T12:13:42.200221Z","iopub.status.idle":"2026-05-05T12:13:42.205175Z","shell.execute_reply.started":"2026-05-05T12:13:42.200189Z","shell.execute_reply":"2026-05-05T12:13:42.204333Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# DataLoaders\n\ntrain_dataset = TranslationDataset(train_df)\nval_dataset = TranslationDataset(val_df)\n\ntrain_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)\nval_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:43.272766Z","iopub.execute_input":"2026-05-05T12:13:43.273582Z","iopub.status.idle":"2026-05-05T12:13:43.278118Z","shell.execute_reply.started":"2026-05-05T12:13:43.273544Z","shell.execute_reply":"2026-05-05T12:13:43.277557Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Model (Encoder + Decoder)\n\nclass Encoder(nn.Module):\n    def __init__(self, input_dim, emb_dim, hid_dim):\n        super().__init__()\n        self.embedding = nn.Embedding(input_dim, emb_dim)\n        self.lstm = nn.LSTM(emb_dim, hid_dim, batch_first=True)\n\n    def forward(self, x):\n        embedded = self.embedding(x)\n        outputs, (hidden, cell) = self.lstm(embedded)\n        return hidden, cell","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:44.419108Z","iopub.execute_input":"2026-05-05T12:13:44.419744Z","iopub.status.idle":"2026-05-05T12:13:44.424502Z","shell.execute_reply.started":"2026-05-05T12:13:44.419712Z","shell.execute_reply":"2026-05-05T12:13:44.423789Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Decoder(nn.Module):\n    def __init__(self, output_dim, emb_dim, hid_dim):\n        super().__init__()\n        self.embedding = nn.Embedding(output_dim, emb_dim)\n        self.lstm = nn.LSTM(emb_dim, hid_dim, batch_first=True)\n        self.fc = nn.Linear(hid_dim, output_dim)\n\n    def forward(self, x, hidden, cell):\n        x = x.unsqueeze(1)\n        embedded = self.embedding(x)\n\n        output, (hidden, cell) = self.lstm(embedded, (hidden, cell))\n        prediction = self.fc(output.squeeze(1))\n\n        return prediction, hidden, cell","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:45.44073Z","iopub.execute_input":"2026-05-05T12:13:45.441339Z","iopub.status.idle":"2026-05-05T12:13:45.446621Z","shell.execute_reply.started":"2026-05-05T12:13:45.441308Z","shell.execute_reply":"2026-05-05T12:13:45.445764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Seq2Seq(nn.Module):\n    def __init__(self, encoder, decoder):\n        super().__init__()\n        self.encoder = encoder\n        self.decoder = decoder\n\n    def forward(self, src, tgt, teacher_forcing_ratio=0.5):\n        batch_size, tgt_len = tgt.shape\n        vocab_size = len(tgt_vocab)\n\n        outputs = torch.zeros(batch_size, tgt_len, vocab_size).to(src.device)\n\n        hidden, cell = self.encoder(src)\n        input = tgt[:,0]\n\n        for t in range(1, tgt_len):\n            output, hidden, cell = self.decoder(input, hidden, cell)\n            outputs[:,t] = output\n\n            top1 = output.argmax(1)\n            input = tgt[:,t] if random.random() < teacher_forcing_ratio else top1\n\n        return outputs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:46.984944Z","iopub.execute_input":"2026-05-05T12:13:46.985243Z","iopub.status.idle":"2026-05-05T12:13:46.992139Z","shell.execute_reply.started":"2026-05-05T12:13:46.985217Z","shell.execute_reply":"2026-05-05T12:13:46.991415Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Initialize Model\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nenc = Encoder(len(src_vocab), 128, 256)\ndec = Decoder(len(tgt_vocab), 128, 256)\n\nmodel = Seq2Seq(enc, dec).to(device)\n\noptimizer = optim.Adam(model.parameters(), lr=0.001)\ncriterion = nn.CrossEntropyLoss(ignore_index=tgt_vocab[\"<pad>\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:48.41863Z","iopub.execute_input":"2026-05-05T12:13:48.419239Z","iopub.status.idle":"2026-05-05T12:13:48.441113Z","shell.execute_reply.started":"2026-05-05T12:13:48.419207Z","shell.execute_reply":"2026-05-05T12:13:48.440498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training Loop\n\ndef train_epoch():\n    model.train()\n    total_loss = 0\n\n    for src, tgt in train_loader:\n        src, tgt = src.to(device), tgt.to(device)\n\n        optimizer.zero_grad()\n        output = model(src, tgt)\n\n        output = output[:,1:].reshape(-1, len(tgt_vocab))\n        tgt = tgt[:,1:].reshape(-1)\n\n        loss = criterion(output, tgt)\n        loss.backward()\n        optimizer.step()\n\n        total_loss += loss.item()\n\n    return total_loss / len(train_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:49.770664Z","iopub.execute_input":"2026-05-05T12:13:49.77116Z","iopub.status.idle":"2026-05-05T12:13:49.781509Z","shell.execute_reply.started":"2026-05-05T12:13:49.771115Z","shell.execute_reply":"2026-05-05T12:13:49.780399Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train Model\n\nEPOCHS = 10\n\ntrain_losses = []\n\nfor epoch in range(EPOCHS):\n    loss = train_epoch()\n    train_losses.append(loss)\n    print(f\"Epoch {epoch+1}, Loss: {loss:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:13:51.190182Z","iopub.execute_input":"2026-05-05T12:13:51.190452Z","iopub.status.idle":"2026-05-05T12:14:09.988629Z","shell.execute_reply.started":"2026-05-05T12:13:51.190427Z","shell.execute_reply":"2026-05-05T12:14:09.987715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lstm_model = model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:14:09.99009Z","iopub.execute_input":"2026-05-05T12:14:09.990415Z","iopub.status.idle":"2026-05-05T12:14:09.994594Z","shell.execute_reply.started":"2026-05-05T12:14:09.99039Z","shell.execute_reply":"2026-05-05T12:14:09.993898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SAVE_DIR_LSTM = \"/kaggle/working/lstm_model.pth\"\n\ntorch.save({\n    \"model_state_dict\": lstm_model.state_dict(),\n    \"src_vocab\": src_vocab,\n    \"tgt_vocab\": tgt_vocab\n}, SAVE_DIR_LSTM)\n\nprint(\"LSTM model saved at:\", SAVE_DIR_LSTM)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:14:09.99538Z","iopub.execute_input":"2026-05-05T12:14:09.99573Z","iopub.status.idle":"2026-05-05T12:14:10.059334Z","shell.execute_reply.started":"2026-05-05T12:14:09.99569Z","shell.execute_reply":"2026-05-05T12:14:10.058428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot Loss\n\nimport matplotlib.pyplot as plt\n\nplt.plot(train_losses)\nplt.title(\"LSTM Training Loss\")\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:14:10.061126Z","iopub.execute_input":"2026-05-05T12:14:10.061446Z","iopub.status.idle":"2026-05-05T12:14:10.180216Z","shell.execute_reply.started":"2026-05-05T12:14:10.061421Z","shell.execute_reply":"2026-05-05T12:14:10.179551Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Inference Function\n\ndef translate_lstm(sentence):\n    model.eval()\n\n    src = torch.tensor(encode(sentence, src_vocab)).unsqueeze(0).to(device)\n    hidden, cell = model.encoder(src)\n\n    input = torch.tensor([tgt_vocab[\"<sos>\"]]).to(device)\n\n    result = []\n\n    for _ in range(50):\n        output, hidden, cell = model.decoder(input, hidden, cell)\n        top1 = output.argmax(1).item()\n\n        if top1 == tgt_vocab[\"<eos>\"]:\n            break\n\n        result.append(inv_tgt_vocab.get(top1, \"<unk>\"))\n        input = torch.tensor([top1]).to(device)\n\n    return \" \".join(result)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:14:10.181132Z","iopub.execute_input":"2026-05-05T12:14:10.181436Z","iopub.status.idle":"2026-05-05T12:14:10.18679Z","shell.execute_reply.started":"2026-05-05T12:14:10.181412Z","shell.execute_reply":"2026-05-05T12:14:10.186048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Testing\n\ndef test_lstm(n=5):\n    indices = random.sample(range(len(val_df)), n)\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n        pred = translate_lstm(inp)\n\n        print(\"INPUT :\", inp)\n        print(\"ACTUAL:\", actual)\n        print(\"PRED  :\", pred)\n        print(\"-\"*80)\n\ntest_lstm(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:14:10.187635Z","iopub.execute_input":"2026-05-05T12:14:10.187864Z","iopub.status.idle":"2026-05-05T12:14:10.288415Z","shell.execute_reply.started":"2026-05-05T12:14:10.18784Z","shell.execute_reply":"2026-05-05T12:14:10.28769Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# LSTM testing + Comparison with Model 1","metadata":{}},{"cell_type":"code","source":"# Generate predictions for test set\n\ntest_df = pd.read_csv(\"/kaggle/input/competitions/deep-past-initiative-machine-translation/test.csv\")\n\ntest_df = test_df.rename(columns={'transliteration': 'akkadian'})\n\npredictions = []\n\nfor i in range(len(test_df)):\n    pred = translate_lstm(test_df.iloc[i]['akkadian'])\n    predictions.append(pred)\n\ntest_df['prediction'] = predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:54:08.543921Z","iopub.execute_input":"2026-05-05T11:54:08.544503Z","iopub.status.idle":"2026-05-05T11:54:08.613669Z","shell.execute_reply.started":"2026-05-05T11:54:08.544438Z","shell.execute_reply":"2026-05-05T11:54:08.613114Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.columns)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:56:02.123881Z","iopub.execute_input":"2026-05-05T11:56:02.12423Z","iopub.status.idle":"2026-05-05T11:56:02.128893Z","shell.execute_reply.started":"2026-05-05T11:56:02.124202Z","shell.execute_reply":"2026-05-05T11:56:02.128237Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Save submission-style file\n\nsubmission = pd.DataFrame({\n    \"id\": test_df[\"id\"],\n    \"translation\": test_df[\"prediction\"]\n})\n\nsubmission.to_csv(\"lstm_predictions.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:58:59.805237Z","iopub.execute_input":"2026-05-05T11:58:59.805664Z","iopub.status.idle":"2026-05-05T11:58:59.812145Z","shell.execute_reply.started":"2026-05-05T11:58:59.805632Z","shell.execute_reply":"2026-05-05T11:58:59.811338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compare Model 1 vs Model 2 on SAME inputs\n\nt5_model = model   # after T5 training\n\nlstm_model = model  # after LSTM training","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:57:23.76528Z","iopub.execute_input":"2026-05-05T11:57:23.765667Z","iopub.status.idle":"2026-05-05T11:57:23.769626Z","shell.execute_reply.started":"2026-05-05T11:57:23.765634Z","shell.execute_reply":"2026-05-05T11:57:23.769046Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate_t5(text):\n    inputs = tokenizer(\n        \"translate Akkadian to English: \" + text,\n        return_tensors=\"pt\",\n        truncation=True,\n        padding=True\n    ).to(t5_model.device)\n\n    outputs = t5_model.generate(\n        **inputs,\n        max_length=128,\n        num_beams=4,\n        early_stopping=True\n    )\n\n    return tokenizer.decode(outputs[0], skip_special_tokens=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:57:31.862622Z","iopub.execute_input":"2026-05-05T11:57:31.862885Z","iopub.status.idle":"2026-05-05T11:57:31.867835Z","shell.execute_reply.started":"2026-05-05T11:57:31.862854Z","shell.execute_reply":"2026-05-05T11:57:31.867036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def compare_models(n=5):\n    import random\n    indices = random.sample(range(len(val_df)), n)\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n\n        pred1 = translate_t5(inp)\n        pred2 = translate_lstm(inp)\n\n        print(\"INPUT :\", inp)\n        print(\"ACTUAL:\", actual)\n        print(\"T5    :\", pred1)\n        print(\"LSTM  :\", pred2)\n        print(\"=\"*100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T11:58:25.635592Z","iopub.execute_input":"2026-05-05T11:58:25.636336Z","iopub.status.idle":"2026-05-05T11:58:25.641101Z","shell.execute_reply.started":"2026-05-05T11:58:25.636306Z","shell.execute_reply":"2026-05-05T11:58:25.640352Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load LSTM \n\nimport torch\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\ncheckpoint = torch.load(\"/kaggle/working/lstm_model.pth\", map_location=device)\n\n# Restore vocab\nsrc_vocab = checkpoint[\"src_vocab\"]\ntgt_vocab = checkpoint[\"tgt_vocab\"]\ninv_tgt_vocab = {i: w for w, i in tgt_vocab.items()}\n\n# Rebuild model\nenc = Encoder(len(src_vocab), 128, 256)\ndec = Decoder(len(tgt_vocab), 128, 256)\n\nlstm_model = Seq2Seq(enc, dec).to(device)\nlstm_model.load_state_dict(checkpoint[\"model_state_dict\"])\nlstm_model.eval()\n\nprint(\" LSTM loaded successfully\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:15:01.43135Z","iopub.execute_input":"2026-05-05T12:15:01.432068Z","iopub.status.idle":"2026-05-05T12:15:01.484555Z","shell.execute_reply.started":"2026-05-05T12:15:01.432027Z","shell.execute_reply":"2026-05-05T12:15:01.483952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def translate_lstm(sentence):\n    lstm_model.eval()\n\n    src = torch.tensor(encode(sentence, src_vocab)).unsqueeze(0).to(device)\n    hidden, cell = lstm_model.encoder(src)\n\n    input = torch.tensor([tgt_vocab[\"<sos>\"]]).to(device)\n\n    result = []\n\n    for _ in range(50):\n        output, hidden, cell = lstm_model.decoder(input, hidden, cell)\n        top1 = output.argmax(1).item()\n\n        if top1 == tgt_vocab[\"<eos>\"]:\n            break\n\n        result.append(inv_tgt_vocab.get(top1, \"<unk>\"))\n        input = torch.tensor([top1]).to(device)\n\n    return \" \".join(result)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:15:14.544354Z","iopub.execute_input":"2026-05-05T12:15:14.545067Z","iopub.status.idle":"2026-05-05T12:15:14.550331Z","shell.execute_reply.started":"2026-05-05T12:15:14.545034Z","shell.execute_reply":"2026-05-05T12:15:14.549545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\n\ndef compare_models(n=5):\n    indices = random.sample(range(len(val_df)), n)\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n\n        pred_t5 = translate_t5(inp)\n        pred_lstm = translate_lstm(inp)\n\n        print(\"INPUT :\", inp)\n        print(\"ACTUAL:\", actual)\n        print(\"T5    :\", pred_t5)\n        print(\"LSTM  :\", pred_lstm)\n        print(\"=\"*100)\n\ncompare_models(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:15:20.362706Z","iopub.execute_input":"2026-05-05T12:15:20.363459Z","iopub.status.idle":"2026-05-05T12:15:22.74378Z","shell.execute_reply.started":"2026-05-05T12:15:20.363426Z","shell.execute_reply":"2026-05-05T12:15:22.74296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndef compare_table(n=5):\n    indices = random.sample(range(len(val_df)), n)\n\n    rows = []\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n\n        rows.append({\n            \"Input\": inp,\n            \"Actual\": actual,\n            \"T5\": translate_t5(inp),\n            \"LSTM\": translate_lstm(inp)\n        })\n\n    return pd.DataFrame(rows)\n\ncompare_table(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:15:47.046628Z","iopub.execute_input":"2026-05-05T12:15:47.047345Z","iopub.status.idle":"2026-05-05T12:15:51.7827Z","shell.execute_reply.started":"2026-05-05T12:15:47.047314Z","shell.execute_reply":"2026-05-05T12:15:51.781822Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Qualitative and Quantitative Comparison More","metadata":{}},{"cell_type":"code","source":"lstm_preds = []\nlstm_refs = []\n\nfor i in range(len(val_df)):\n    inp = val_df.iloc[i]['akkadian']\n    actual = val_df.iloc[i]['english']\n\n    pred = translate_lstm(inp)\n\n    lstm_preds.append(pred)\n    lstm_refs.append([actual])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:18:19.77175Z","iopub.execute_input":"2026-05-05T12:18:19.772599Z","iopub.status.idle":"2026-05-05T12:18:21.469773Z","shell.execute_reply.started":"2026-05-05T12:18:19.772566Z","shell.execute_reply":"2026-05-05T12:18:21.469086Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import evaluate\n\nbleu = evaluate.load(\"sacrebleu\")\n\nlstm_score = bleu.compute(predictions=lstm_preds, references=lstm_refs)\n\nprint(\"LSTM BLEU Score:\", lstm_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:18:26.091826Z","iopub.execute_input":"2026-05-05T12:18:26.092519Z","iopub.status.idle":"2026-05-05T12:18:26.780165Z","shell.execute_reply.started":"2026-05-05T12:18:26.092443Z","shell.execute_reply":"2026-05-05T12:18:26.779526Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# T5 Belu: \n\nt5_preds = []\nt5_refs = []\n\nfor i in range(len(val_df)):\n    inp = val_df.iloc[i]['akkadian']\n    actual = val_df.iloc[i]['english']\n\n    pred = translate_t5(inp)\n\n    t5_preds.append(pred)\n    t5_refs.append([actual])\n\nt5_score = bleu.compute(predictions=t5_preds, references=t5_refs)\n\nprint(\"T5 BLEU Score:\", t5_score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:18:43.353864Z","iopub.execute_input":"2026-05-05T12:18:43.354456Z","iopub.status.idle":"2026-05-05T12:20:11.335622Z","shell.execute_reply.started":"2026-05-05T12:18:43.354426Z","shell.execute_reply":"2026-05-05T12:20:11.334838Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n===== MODEL COMPARISON =====\")\nprint(\"T5 BLEU   :\", t5_score['score'])\nprint(\"LSTM BLEU :\", lstm_score['score'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:20:11.337006Z","iopub.execute_input":"2026-05-05T12:20:11.337275Z","iopub.status.idle":"2026-05-05T12:20:11.341203Z","shell.execute_reply.started":"2026-05-05T12:20:11.337252Z","shell.execute_reply":"2026-05-05T12:20:11.340549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Data\nlabels = ['BLEU Score', 'Precision-1', 'Precision-2', 'Precision-3', 'Precision-4', 'BP']\n\nt5 = [\n    5.6712,\n    34.4782,\n    13.7546,\n    6.5012,\n    2.9678,\n    0.5799\n]\n\nlstm = [\n    0.2823,\n    14.7150,\n    0.9558,\n    0.0466,\n    0.0119,\n    0.9499\n]\n\nx = np.arange(len(labels))\nwidth = 0.35\n\n# Plot\nplt.figure()\nplt.bar(x - width/2, t5, width, label='T5')\nplt.bar(x + width/2, lstm, width, label='LSTM')\n\nplt.xticks(x, labels, rotation=30)\nplt.ylabel('Values')\nplt.title('T5 vs LSTM BLEU Comparison')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T16:40:21.178871Z","iopub.execute_input":"2026-05-05T16:40:21.179306Z","iopub.status.idle":"2026-05-05T16:40:21.427411Z","shell.execute_reply.started":"2026-05-05T16:40:21.17928Z","shell.execute_reply":"2026-05-05T16:40:21.426533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Qualitative Comparison","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def comparison_table(n=5):\n    indices = random.sample(range(len(val_df)), n)\n\n    rows = []\n\n    for i in indices:\n        inp = val_df.iloc[i]['akkadian']\n        actual = val_df.iloc[i]['english']\n\n        rows.append({\n            \"Input\": inp,\n            \"Actual\": actual,\n            \"T5\": translate_t5(inp),\n            \"LSTM\": translate_lstm(inp)\n        })\n\n    return pd.DataFrame(rows)\n\ncomparison_table(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:20:11.342115Z","iopub.execute_input":"2026-05-05T12:20:11.342468Z","iopub.status.idle":"2026-05-05T12:20:16.196427Z","shell.execute_reply.started":"2026-05-05T12:20:11.342443Z","shell.execute_reply":"2026-05-05T12:20:16.195549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['lstm_prediction'] = [\n    translate_lstm(x) for x in test_df['akkadian']\n]\n\ntest_df[['akkadian', 't5_prediction', 'lstm_prediction']].head(10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:20:16.197868Z","iopub.execute_input":"2026-05-05T12:20:16.198342Z","iopub.status.idle":"2026-05-05T12:20:16.257432Z","shell.execute_reply.started":"2026-05-05T12:20:16.198315Z","shell.execute_reply":"2026-05-05T12:20:16.256543Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Downlaod Models","metadata":{}},{"cell_type":"code","source":"import shutil\n\n# Zip the T5 folder\nshutil.make_archive(\n    base_name=\"t5_akkadian_model\",\n    format=\"zip\",\n    root_dir=\"/kaggle/working/t5_akkadian_model\"\n)\n\nprint(\" T5 model zipped\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:22:43.464239Z","iopub.execute_input":"2026-05-05T12:22:43.464691Z","iopub.status.idle":"2026-05-05T12:22:55.97923Z","shell.execute_reply.started":"2026-05-05T12:22:43.46466Z","shell.execute_reply":"2026-05-05T12:22:55.978455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"shutil.make_archive(\n    base_name=\"lstm_model\",\n    format=\"zip\",\n    root_dir=\"/kaggle/working\",\n    base_dir=\"lstm_model.pth\"\n)\n\nprint(\"✅ LSTM model zipped\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:23:43.31791Z","iopub.execute_input":"2026-05-05T12:23:43.318651Z","iopub.status.idle":"2026-05-05T12:23:43.651822Z","shell.execute_reply.started":"2026-05-05T12:23:43.318616Z","shell.execute_reply":"2026-05-05T12:23:43.650969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\n\nOUTPUT_ZIP = \"/kaggle/working/full_project_backup.zip\"\n\nwith zipfile.ZipFile(OUTPUT_ZIP, \"w\", compression=zipfile.ZIP_DEFLATED) as zf:\n    # Zip /kaggle/input\n    for root, _, files in os.walk(\"/kaggle/input\"):\n        for f in files:\n            full_path = os.path.join(root, f)\n            # keep path inside zip as input/...\n            arcname = os.path.relpath(full_path, \"/kaggle\")\n            zf.write(full_path, arcname)\n\n    # Zip /kaggle/working\n    for root, _, files in os.walk(\"/kaggle/working\"):\n        for f in files:\n            full_path = os.path.join(root, f)\n            # avoid adding the zip file itself recursively\n            if full_path == OUTPUT_ZIP:\n                continue\n            # keep path inside zip as working/...\n            arcname = os.path.relpath(full_path, \"/kaggle\")\n            zf.write(full_path, arcname)\n\nprint(\" Zipped everything to:\", OUTPUT_ZIP)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-05T12:25:55.514154Z","iopub.execute_input":"2026-05-05T12:25:55.514952Z","iopub.status.idle":"2026-05-05T12:32:47.50609Z","shell.execute_reply.started":"2026-05-05T12:25:55.514903Z","shell.execute_reply":"2026-05-05T12:32:47.505142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}