{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a19216bf-7568-46b1-b55c-8c6cc970ee88",
   "metadata": {},
   "source": [
    "# Part of Speech Tagging"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cae9ee33-f0a0-43a4-9373-c3c45b56cf7e",
   "metadata": {},
   "source": [
    " ## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6708d97c-f4fb-459d-bea0-8a1ddadab088",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "297f6b9c-e16c-44a2-9c4e-cc2f4c801053",
   "metadata": {},
   "source": [
    "## Helix Problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "id": "a93acc9b-c125-4a37-93c6-48c7fd5a0ec4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23],\n",
       "       [24, 25, 26, 27, 28, 29]])"
      ]
     },
     "execution_count": 292,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start_pos = (2, 5)  \n",
    "i, j = start_pos\n",
    "i_min = 0\n",
    "j_min = 0\n",
    "i_max = 4\n",
    "j_max = 5\n",
    "assert (i == i_min) |(i == i_max)| (j == j_min)| (j == j_max)\n",
    "\n",
    "mat = np.arange(0, (i_max+1)*(j_max+1),1).reshape((i_max+1),(j_max+1)); mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "id": "c02d2ce1-1522-4dd9-b687-ec36244f5d7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def right(i,j): return (i,j+1)\n",
    "def down(i,j): return (i+1,j)\n",
    "def left(i,j): return (i,j-1)\n",
    "def up(i,j): return (i-1,j)\n",
    "\n",
    "directions = ['r', 'd', 'l', 'u']\n",
    "\n",
    "def get_initial_dir(start_pos, i_min, j_min, i_max, j_max):\n",
    "    i, j = start_pos\n",
    "    # assert i_min < i_max\n",
    "    # assert j_min < j_max\n",
    "    assert (i == i_min) |(i == i_max)| (j == j_min)| (j == j_max)\n",
    "    if (i == i_min) & (j<j_max): return 'r'\n",
    "    elif (i < i_max) & (j == j_max): return 'd'\n",
    "    elif (i == i_max) & (j > j_min): return 'l'\n",
    "    elif (i > i_min) & (j == j_min): return 'u'\n",
    "    elif (j_min >= j_max): return 'u'\n",
    "    elif (i_min >= i_max): return 'l'\n",
    "    else: raise Exception ('Not Implemented')\n",
    "\n",
    "\n",
    "\n",
    "def move(curr_pos, curr_dir, i_min, j_min, i_max, j_max):\n",
    "    new_pos = curr_pos\n",
    "    new_dir= curr_dir\n",
    "    i, j = curr_pos\n",
    "    if curr_dir == 'r':\n",
    "        if j < j_max : new_pos = right(i,j)\n",
    "        else: new_pos = down(i,j); new_dir = 'd'\n",
    "    elif curr_dir == 'd':\n",
    "        if i < i_max : new_pos = down(i,j)\n",
    "        else: new_pos = left(i,j); new_dir  = 'l'\n",
    "    elif curr_dir == 'l':\n",
    "        if j > j_min : new_pos = left(i,j)\n",
    "        else: new_pos = up(i,j); new_dir = 'u'\n",
    "    elif curr_dir == 'u':\n",
    "        if i > i_min : new_pos = up(i,j)\n",
    "        else: new_pos = right(i,j); new_dir = 'r'\n",
    "        \n",
    "    return new_pos, new_dir\n",
    "\n",
    "\n",
    "def move_inner(curr_pos, curr_dir):\n",
    "    new_pos = curr_pos\n",
    "    i, j = curr_pos\n",
    "    if curr_dir == 'r': new_pos = down(i,j)\n",
    "    elif curr_dir == 'd': new_pos = left(i,j)\n",
    "    elif curr_dir == 'l': new_pos = up(i,j)\n",
    "    elif curr_dir == 'u': new_pos = right(i,j)\n",
    "    return new_pos\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "id": "4c9ac123-085b-4a07-a8b0-67729a673ba3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 5) d 0 0 4 5 29\n",
      "(1, 3) r 1 1 3 4 11\n",
      "(2, 2) l 2 2 2 3 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 19, 20, 21, 22, 23],\n",
       "       [24, 25, 26, 27, 28, 29]])"
      ]
     },
     "execution_count": 328,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def make_loop(start_pos, i_min, j_min, i_max, j_max, idx_list=[]):\n",
    "    curr_pos = start_pos\n",
    "    curr_dir = get_initial_dir(start_pos, i_min, j_min, i_max, j_max)\n",
    "    step = 0\n",
    "    max_steps = 2*((i_max-i_min+1)+(j_max -j_min+1)-1) -1; max_steps\n",
    "    while step < (max_steps-2):\n",
    "        new_pos, new_dir = move(curr_pos,curr_dir, i_min, j_min, i_max, j_max)\n",
    "        curr_pos, curr_dir = new_pos, new_dir\n",
    "        idx_list.append(curr_pos)\n",
    "        step +=1\n",
    "    # print(f\"IDX_LIST : {len(idx_list), mat[idx_list[0]], mat[idx_list[-1]]}\")\n",
    "    return curr_pos, curr_dir, step\n",
    "\n",
    "def do_helix(start_pos, i_min, j_min, i_max, j_max, idx_list=[]):\n",
    "    total_steps_max = (i_max-i_min+1)*(j_max-j_min+1) -1\n",
    "    curr_pos = start_pos\n",
    "    # print(mat[curr_pos[0], curr_pos[1]])\n",
    "    if (total_steps_max<=0): return idx_list\n",
    "    # elif (i_min >= i_max)|(j_min>=j_max): return idx_list.append(curr_pos)\n",
    "    else:\n",
    "        idx_list.append(curr_pos)\n",
    "        curr_pos, curr_dir, step = make_loop(curr_pos, i_min, j_min, i_max, j_max, idx_list)\n",
    "        print(curr_pos,curr_dir, i_min, j_min, i_max, j_max, total_steps_max)\n",
    "        \n",
    "        total_steps_max = total_steps_max - step +1\n",
    "        i_min +=1\n",
    "        i_max -=1\n",
    "        j_min +=1\n",
    "        j_max -=1\n",
    "        new_pos = move_inner(curr_pos, curr_dir)\n",
    "        \n",
    "        return do_helix(new_pos, i_min, j_min, i_max, j_max, idx_list)\n",
    "        \n",
    "idx= np.array(do_helix(start_pos, i_min, j_min, i_max, j_max))\n",
    "mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "id": "cea73394-6a26-48ef-a291-95fdabba76d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30, 2)"
      ]
     },
     "execution_count": 322,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "id": "c418e4dd-c104-49fc-afa8-5ee78abc34d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 5 17\n",
      "3 5 23\n",
      "4 5 29\n",
      "4 4 28\n",
      "4 3 27\n",
      "4 2 26\n",
      "4 1 25\n",
      "4 0 24\n",
      "3 0 18\n",
      "2 0 12\n",
      "1 0 6\n",
      "0 0 0\n",
      "0 1 1\n",
      "0 2 2\n",
      "0 3 3\n",
      "0 4 4\n",
      "0 5 5\n",
      "1 5 11\n",
      "1 4 10\n",
      "2 4 16\n",
      "3 4 22\n",
      "3 3 21\n",
      "3 2 20\n",
      "3 1 19\n",
      "2 1 13\n",
      "1 1 7\n",
      "1 2 8\n",
      "1 3 9\n",
      "2 3 15\n",
      "2 2 14\n"
     ]
    }
   ],
   "source": [
    "for i,j in idx:\n",
    "    print(i, j, mat[i,j])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3509e6-c609-4675-bb28-38ef287539df",
   "metadata": {},
   "source": [
    "# Part of Speech Tagging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05cc4480-8039-4388-88d3-e23809743133",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "40ead112-59d1-4283-9576-68c901745a04",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6869d068-3796-44d3-957c-cf4b4441915e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}