import re

import pandas as pd

from utils import Constants


def custom_tokenizer(regex, string): return re.findall(regex, string)


def get_data_file_columns(file_path): return list((pd.read_csv(file_path)).columns.values)


def prepare_data(file_path, label_name):
    df = pd.read_csv(file_path)
    cols = list(df.columns.values)
    cols.pop(cols.index(label_name))

    return df[cols + [label_name]].values.tolist()


def prepare_tokens(speech, regex, stop_words, stemmer):
    speech = custom_tokenizer(regex, speech.lower())
    string_list = [stemmer.stem(string) for string in speech if string not in stop_words]

    return string_list


def get_word_features(sentence, index, columns):
    word = sentence[index]

    return {
        columns.index('word'): word,
        columns.index('is_first'): index == 0,
        columns.index('is_last'): index == len(sentence) - 1,
        columns.index('is_capitalized'): word[0].upper() == word[0],
        columns.index('is_all_caps'): word.upper() == word,
        columns.index('is_all_lower'): word.lower() == word,
        columns.index('prefix-1'): word[0],
        columns.index('prefix-2'): word[:2],
        columns.index('prefix-3'): word[:3],
        columns.index('suffix-1'): word[-1],
        columns.index('suffix-2'): word[-2:],
        columns.index('suffix-3'): word[-3:],
        columns.index('prev_word'): '' if index == 0 else sentence[index - 1],
        columns.index('next_word'): '' if index == len(sentence) - 1 else sentence[index + 1],
        columns.index('has_hyphen'): '-' in word,
        columns.index('capital_inside'): word[1:].lower() != word[1:],
        columns.index('is_numeric'): word.isdigit(),
        columns.index('label'): sentence[index]
    }

import re

import pandas as pd

from utils import Constants


def custom_tokenizer(regex, string): return re.findall(regex, string)


def get_data_file_columns(file_path): return list((pd.read_csv(file_path)).columns.values)


def prepare_data(file_path, label_name):
    df = pd.read_csv(file_path)
    cols = list(df.columns.values)
    cols.pop(cols.index(label_name))

    return df[cols + [label_name]].values.tolist()


def prepare_tokens(speech, regex, stop_words, stemmer):
    speech = custom_tokenizer(regex, speech.lower())
    string_list = [stemmer.stem(string) for string in speech if string not in stop_words]

    return string_list


def get_word_features(sentence, index, columns):
    word = sentence[index]

    return {
        columns.index('word'): word,
        columns.index('is_first'): index == 0,
        columns.index('is_last'): index == len(sentence) - 1,
        columns.index('is_capitalized'): word[0].upper() == word[0],
        columns.index('is_all_caps'): word.upper() == word,
        columns.index('is_all_lower'): word.lower() == word,
        columns.index('prefix-1'): word[0],
        columns.index('prefix-2'): word[:2],
        columns.index('prefix-3'): word[:3],
        columns.index('suffix-1'): word[-1],
        columns.index('suffix-2'): word[-2:],
        columns.index('suffix-3'): word[-3:],
        columns.index('prev_word'): '' if index == 0 else sentence[index - 1],
        columns.index('next_word'): '' if index == len(sentence) - 1 else sentence[index + 1],
        columns.index('has_hyphen'): '-' in word,
        columns.index('capital_inside'): word[1:].lower() != word[1:],
        columns.index('is_numeric'): word.isdigit(),
        columns.index('label'): sentence[index]
    }
