Part of Speech Tagging#

Imports#

import numpy as np

Helix Problem#

start_pos = (2, 5)  
i, j = start_pos
i_min = 0
j_min = 0
i_max = 4
j_max = 5
assert (i == i_min) |(i == i_max)| (j == j_min)| (j == j_max)

mat = np.arange(0, (i_max+1)*(j_max+1),1).reshape((i_max+1),(j_max+1)); mat
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29]])
def right(i,j): return (i,j+1)
def down(i,j): return (i+1,j)
def left(i,j): return (i,j-1)
def up(i,j): return (i-1,j)

directions = ['r', 'd', 'l', 'u']

def get_initial_dir(start_pos, i_min, j_min, i_max, j_max):
    i, j = start_pos
    # assert i_min < i_max
    # assert j_min < j_max
    assert (i == i_min) |(i == i_max)| (j == j_min)| (j == j_max)
    if (i == i_min) & (j<j_max): return 'r'
    elif (i < i_max) & (j == j_max): return 'd'
    elif (i == i_max) & (j > j_min): return 'l'
    elif (i > i_min) & (j == j_min): return 'u'
    elif (j_min >= j_max): return 'u'
    elif (i_min >= i_max): return 'l'
    else: raise Exception ('Not Implemented')



def move(curr_pos, curr_dir, i_min, j_min, i_max, j_max):
    new_pos = curr_pos
    new_dir= curr_dir
    i, j = curr_pos
    if curr_dir == 'r':
        if j < j_max : new_pos = right(i,j)
        else: new_pos = down(i,j); new_dir = 'd'
    elif curr_dir == 'd':
        if i < i_max : new_pos = down(i,j)
        else: new_pos = left(i,j); new_dir  = 'l'
    elif curr_dir == 'l':
        if j > j_min : new_pos = left(i,j)
        else: new_pos = up(i,j); new_dir = 'u'
    elif curr_dir == 'u':
        if i > i_min : new_pos = up(i,j)
        else: new_pos = right(i,j); new_dir = 'r'
        
    return new_pos, new_dir


def move_inner(curr_pos, curr_dir):
    new_pos = curr_pos
    i, j = curr_pos
    if curr_dir == 'r': new_pos = down(i,j)
    elif curr_dir == 'd': new_pos = left(i,j)
    elif curr_dir == 'l': new_pos = up(i,j)
    elif curr_dir == 'u': new_pos = right(i,j)
    return new_pos
    
def make_loop(start_pos, i_min, j_min, i_max, j_max, idx_list=[]):
    curr_pos = start_pos
    curr_dir = get_initial_dir(start_pos, i_min, j_min, i_max, j_max)
    step = 0
    max_steps = 2*((i_max-i_min+1)+(j_max -j_min+1)-1) -1; max_steps
    while step < (max_steps-2):
        new_pos, new_dir = move(curr_pos,curr_dir, i_min, j_min, i_max, j_max)
        curr_pos, curr_dir = new_pos, new_dir
        idx_list.append(curr_pos)
        step +=1
    # print(f"IDX_LIST : {len(idx_list), mat[idx_list[0]], mat[idx_list[-1]]}")
    return curr_pos, curr_dir, step

def do_helix(start_pos, i_min, j_min, i_max, j_max, idx_list=[]):
    total_steps_max = (i_max-i_min+1)*(j_max-j_min+1) -1
    curr_pos = start_pos
    # print(mat[curr_pos[0], curr_pos[1]])
    if (total_steps_max<=0): return idx_list
    # elif (i_min >= i_max)|(j_min>=j_max): return idx_list.append(curr_pos)
    else:
        idx_list.append(curr_pos)
        curr_pos, curr_dir, step = make_loop(curr_pos, i_min, j_min, i_max, j_max, idx_list)
        print(curr_pos,curr_dir, i_min, j_min, i_max, j_max, total_steps_max)
        
        total_steps_max = total_steps_max - step +1
        i_min +=1
        i_max -=1
        j_min +=1
        j_max -=1
        new_pos = move_inner(curr_pos, curr_dir)
        
        return do_helix(new_pos, i_min, j_min, i_max, j_max, idx_list)
        
idx= np.array(do_helix(start_pos, i_min, j_min, i_max, j_max))
mat
(1, 5) d 0 0 4 5 29
(1, 3) r 1 1 3 4 11
(2, 2) l 2 2 2 3 1
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29]])
idx.shape
(30, 2)
for i,j in idx:
    print(i, j, mat[i,j])
2 5 17
3 5 23
4 5 29
4 4 28
4 3 27
4 2 26
4 1 25
4 0 24
3 0 18
2 0 12
1 0 6
0 0 0
0 1 1
0 2 2
0 3 3
0 4 4
0 5 5
1 5 11
1 4 10
2 4 16
3 4 22
3 3 21
3 2 20
3 1 19
2 1 13
1 1 7
1 2 8
1 3 9
2 3 15
2 2 14

Part of Speech Tagging#