import numpy as np
import json
import codecs
import re
import csv 
FIXED_VOCABULARY = None
SENTENCE_PAIR_DATA = False
#source: ( That ( ( 's ( ( a lot ) ( to ( pack ( into ( 18 minutes ) ) ) ) ) ) . ) )
#target: وأخرج ملتحفا فوطتي، كان الجميع يراني .
def load_data(source_path, target_path, trg_language="arabic", src_language="english", data_type="gt", is_lowercase=True):
    examples = []
    s_file=open(source_path)
    t_file=open(target_path)
    i=0
    for element in zip(s_file.readlines(), t_file.readlines()):
        line = element[0].strip()
        s_tokens, s_transitions=convert_binary_bracketing(line, data_type=data_type, lowercase=is_lowercase)
        if trg_language=="zh":
            t_tokens=list(element[1])
        else:
            t_tokens=element[1].lower().split()
        example = {}
        example["tokens"] = s_tokens
        example["target_tokens"] = t_tokens+["<s>"]#end token for mt predictions.
        example["transitions"] = s_transitions
        example["example_id"]=i
        i+=1
        examples.append(example)
    return examples


def convert_binary_bracketing(parse, data_type="gt",lowercase=False):
    transitions = []
    tokens = []
    for word in parse.split(' '):
        if word[0] != "(":
            if word.strip() == ")":
                transitions.append(1)
            else:
                # Downcase all words to match GloVe.
                if lowercase:
                    tokens.append(word.lower())
                else:
                    tokens.append(word)
                transitions.append(0)
    if(data_type=="lb"):
        transitions=lb_build(len(tokens))
    elif(data_type=="bal"):
        transitions=balanced_transitions(len(tokens))
    elif(data_type=="rb"):
        transitions=rb_build(len(tokens))
    #print(transitions)
    return tokens, transitions

def lb_build(N):
      if N==2:
          return [0,0,1]
      else:
          return [0,0,1]+(N-2)*[0,1]

def rb_build(N):
    return [0]*(N)+[1]*(N-1)

def balanced_transitions(N):
        """
        Recursively creates a balanced binary tree with N
        leaves using shift reduce transitions.
        """
        if N == 3:
            return [0, 0, 1, 0, 1]
        elif N == 2:
            return [0, 0, 1]
        elif N == 1:
            return [0]
        else:
            right_N = N // 2
            left_N = N - right_N
            return balanced_transitions(left_N) + balanced_transitions(right_N) + [1]


