# Path: keras_image_captioning/common_utils.py
# def parse_timedelta(timedelta_str):
#     if isinstance(timedelta_str, timedelta):
#         return timedelta_str
#     if not timedelta_str or timedelta_str == 'null':
#         return None
# 
#     tokens = re.split(r' days?,? ', timedelta_str)
#     if len(tokens) == 1:
#         if tokens[0].find('day') == -1:
#             days = '0'
#             rest = tokens[0]
#         else:
#             days = tokens[0].split(' ')[0]
#             rest = '00:00:00'
#     else:
#         days, rest = tokens
# 
#     days = int(days)
#     hours, minutes, seconds = map(int, rest.split(':'))
#     return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
# 
# Path: keras_image_captioning/io_utils.py
# def write_yaml_file(obj, path):
#     with open(path, 'w') as f:
#         yaml.dump(obj, f, default_flow_style=False)


import itertools
import yaml
import sys
from collections import OrderedDict, namedtuple
from datetime import timedelta
from random import choice, randint, uniform
from .common_utils import parse_timedelta
from .io_utils import write_yaml_file


class Embed300FineRandomConfigBuilder(Embed300RandomConfigBuilder):
    def __init__(self, fixed_config_keys):
        super(Embed300FineRandomConfigBuilder, self).__init__(
                                                            fixed_config_keys)

        self._reduce_lr_factor = lambda: uniform(0.1, 0.9)
        self._reduce_lr_patience = lambda: 4
        self._lemmatize_caption = lambda: False

        # Values below are from an analysis of hpsearch/16
        self._word_vector_init = lambda: 'glove'
        self._rnn_layers = lambda: randint(2, 3)
        self._learning_rate = lambda: 10 ** uniform(-4, -2.7)
        self._dropout_rate = lambda: uniform(0.2, 0.5)


class FileConfigBuilder(ConfigBuilderBase):
    def __init__(self, yaml_path):
        self._yaml_path = yaml_path

    def build_config(self):
        with open(self._yaml_path) as yaml_file:
            config_dict = yaml.load(yaml_file)

        # For backward compatibility
        for field in Config._fields:
            config_dict.setdefault(field, None)


# Path: keras_image_captioning/common_utils.py
# def parse_timedelta(timedelta_str):
#     if isinstance(timedelta_str, timedelta):
#         return timedelta_str
#     if not timedelta_str or timedelta_str == 'null':
#         return None
# 
#     tokens = re.split(r' days?,? ', timedelta_str)
#     if len(tokens) == 1:
#         if tokens[0].find('day') == -1:
#             days = '0'
#             rest = tokens[0]
#         else:
#             days = tokens[0].split(' ')[0]
#             rest = '00:00:00'
#     else:
#         days, rest = tokens
# 
#     days = int(days)
#     hours, minutes, seconds = map(int, rest.split(':'))
#     return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
# 
# Path: keras_image_captioning/io_utils.py
# def write_yaml_file(obj, path):
#     with open(path, 'w') as f:
#         yaml.dump(obj, f, default_flow_style=False)


# Path: keras_image_captioning/config.py
import itertools
import yaml
import sys
from collections import OrderedDict, namedtuple
from datetime import timedelta
from random import choice, randint, uniform
from .common_utils import parse_timedelta
from .io_utils import write_yaml_file



class Embed300FineRandomConfigBuilder(Embed300RandomConfigBuilder):
    def __init__(self, fixed_config_keys):
        super(Embed300FineRandomConfigBuilder, self).__init__(
                                                            fixed_config_keys)

        self._reduce_lr_factor = lambda: uniform(0.1, 0.9)
        self._reduce_lr_patience = lambda: 4
        self._lemmatize_caption = lambda: False

        # Values below are from an analysis of hpsearch/16
        self._word_vector_init = lambda: 'glove'
        self._rnn_layers = lambda: randint(2, 3)
        self._learning_rate = lambda: 10 ** uniform(-4, -2.7)
        self._dropout_rate = lambda: uniform(0.2, 0.5)


class FileConfigBuilder(ConfigBuilderBase):
    def __init__(self, yaml_path):
        self._yaml_path = yaml_path

    def build_config(self):
        with open(self._yaml_path) as yaml_file:
            config_dict = yaml.load(yaml_file)

        # For backward compatibility
        for field in Config._fields:
            config_dict.setdefault(field, None)


        config_dict['time_limit'] = parse_timedelta(config_dict['time_limit'])