# Path: search/utils.py
# def _dec(fl):
# def normalize(iterable):
# def scale_to_one(iterable):
# def weighted_average(values, weights):
# def tokenize(string):
# def max_distance(phrase, word_index):
# def position_similarity(token1, token2, phrase1, phrase2):
# 
# Path: search/config.py
# MATCH_WEIGHT = 0.2
# DIST_WEIGHT = 0.8
# THRESHOLD = 0.75
# MIN_WORD_LENGTH = 3
# STR_SPLIT_REGEX = r'\W+'


import jellyfish as jf
from search import utils, config
    matrix = [(s1, s2) for s1 in longest for s2 in shortest]

    matches = {}
    for string1, string2 in matrix:
        # get the jaro winkler equality between the two strings
        match = jf.jaro_winkler(string1, string2)
        # calculate the distance factor for the position of the segments
        # on their respective lists
        positional = utils.position_similarity(
            string1, string2, longest, shortest)

        # get them together and append to the matches dictionary
        match = (match, positional)
        matches.setdefault(string1, []).append(match)

    # get the highest value for each list, the apply the word-distance factor
    # the key takes the jaro winkler distance value to get the max value
    matches = [max(m, key=lambda x: x[0]) for m in matches.values()]
    _weights = (config.MATCH_WEIGHT, config.DIST_WEIGHT)
    matches = [utils.weighted_average((m, d), _weights) for m, d in matches]

    # get the weighted mean for all the highest matches and apply the highest
    # match value found as coefficient as multiplier, to add weights to more
    # coherent matches.
    mean_match = (sum(matches) / len(matches)) * max(matches)
    return mean_match


def search(
        query, attributes, dataset, limit=-1,

# Path: search/utils.py
# def _dec(fl):
# def normalize(iterable):
# def scale_to_one(iterable):
# def weighted_average(values, weights):
# def tokenize(string):
# def max_distance(phrase, word_index):
# def position_similarity(token1, token2, phrase1, phrase2):
# 
# Path: search/config.py
# MATCH_WEIGHT = 0.2
# DIST_WEIGHT = 0.8
# THRESHOLD = 0.75
# MIN_WORD_LENGTH = 3
# STR_SPLIT_REGEX = r'\W+'


# Path: search/core.py
import jellyfish as jf
from search import utils, config

    matrix = [(s1, s2) for s1 in longest for s2 in shortest]

    matches = {}
    for string1, string2 in matrix:
        # get the jaro winkler equality between the two strings
        match = jf.jaro_winkler(string1, string2)
        # calculate the distance factor for the position of the segments
        # on their respective lists
        positional = utils.position_similarity(
            string1, string2, longest, shortest)

        # get them together and append to the matches dictionary
        match = (match, positional)
        matches.setdefault(string1, []).append(match)

    # get the highest value for each list, the apply the word-distance factor
    # the key takes the jaro winkler distance value to get the max value
    matches = [max(m, key=lambda x: x[0]) for m in matches.values()]
    _weights = (config.MATCH_WEIGHT, config.DIST_WEIGHT)
    matches = [utils.weighted_average((m, d), _weights) for m, d in matches]

    # get the weighted mean for all the highest matches and apply the highest
    # match value found as coefficient as multiplier, to add weights to more
    # coherent matches.
    mean_match = (sum(matches) / len(matches)) * max(matches)
    return mean_match


def search(
        query, attributes, dataset, limit=-1,

        threshold=config.THRESHOLD, weights=None):