{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pa = \"data/iwslt17/standard/tmp/valid.align\"\n",
    "pa = \"va\"\n",
    "fa = open(pa, 'r')\n",
    "lines = fa.readlines()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "couples = [a.split() for a in lines]\n",
    "aligns=[]\n",
    "for line in couples:\n",
    "    v = [list(map(int,a.split('-'))) for a in line]\n",
    "    aligns.append(v)\n",
    "# check if it ever happens that two source tokens are related to the same target token\n",
    "for a in aligns:\n",
    "    a = np.array(a)\n",
    "    s = all(a[i,1] < a[i+1,1] for i in range(len(a)-1))\n",
    "    if s == False:\n",
    "        print(a)\n",
    "        break\n",
    "        print(\"------\")\n",
    "aligns = np.array(aligns[4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[0, 0],\n       [2, 1],\n       [3, 3],\n       [6, 4],\n       [6, 5],\n       [7, 6]])"
     },
     "metadata": {},
     "execution_count": 43
    }
   ],
   "source": [
    "aligns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "3\nRECURSIVE\n(3, 3)\n"
    }
   ],
   "source": [
    "def where_to_split(aligns, m, src_length, tgt_length):\n",
    "    if m == 0:\n",
    "        return src_length // 2, tgt_length // 2\n",
    "    else:\n",
    "        cannot_split = True\n",
    "        m_init = m\n",
    "        while cannot_split:\n",
    "            max_before = max(aligns[:m+1,0])\n",
    "            last_good = np.where(~(max_before < aligns[:,0]))[0][-1]\n",
    "            if last_good - m > 0:\n",
    "                m = last_good\n",
    "            else: \n",
    "                cannot_split=False\n",
    "        src_split = max_before\n",
    "        tgt_split = aligns[last_good,1]\n",
    "        # check if there are at least two words in the second sentence\n",
    "        if (src_length-1)-src_split < 2 or (tgt_length-1)-tgt_split < 2:\n",
    "            print(\"RECURSIVE\")\n",
    "            return where_to_split(aligns, m_init -1, src_length, tgt_length)\n",
    "        else:\n",
    "            return src_split, tgt_split\n",
    "\n",
    "m = len(aligns)//2\n",
    "print(m)\n",
    "print(where_to_split(aligns, m, max(aligns[:,0]), max(aligns[:,1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "6"
     },
     "metadata": {},
     "execution_count": 50
    }
   ],
   "source": [
    "max(aligns[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.3-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}