Returns the closest matching word in the dictionary
559532
def best_match(given_word)
      words = (@word_list.is_a? Array) ? @word_list : @word_list.keys

      word_bigrams = bigramate(given_word)
      word_hash = words.map do |key|
        [key, bigram_compare(word_bigrams, bigramate(key))]
      end
      word_hash = Hash[word_hash]

      # Weight by word usage, if logical
      word_hash = apply_usage_weights(word_hash) if @word_list.is_a? Hash

      word_hash.max_by { |_key, value| value }.first
    end
954419
function closest(el, fn) {
                    return el && (fn(el) ? el : closest(el.parentNode, fn));
                }
940611
def nearest_neighbor(x, tSet):
    
    assert isinstance(x, tuple) and isinstance(tSet, dict)
    current_key = ()
    min_d = float('inf')
    for key in tSet:
        d = distance(x, key)
        if d < min_d:
            min_d = d
            current_key = key
    return tSet[current_key]
391890
function closest(e, classname) {
    if ($(e).hasClass(classname)) {
        return e;
    }
    return e.parentNode && closest(e.parentNode, classname);
}
261613
def nearest_keys(self, key):
        """"""
        if not isinstance(key, tuple):
            _key = (key,)
        if self.__stale:
            self.generate_tree()
        d, idx = self.__tree.query(
            _key, self.k_neighbors, distance_upper_bound=self.distance_upper_bound)

        try:
            return [self.__keys[id][0] for id in idx if id < len(self.__keys)]
        except TypeError:
            # if k_neighbors = 1 query is not returnng arrays
            return self.__keys[idx]
1314249
def find_nearest_index(arr, value):
    """"""
    arr = np.array(arr)
    index = (abs(arr-value)).argmin()
    return index
625669
def nearest_subpackage(cls, package, all_packages):
    """"""
    def shared_prefix(candidate):
      zipped = zip(package.split('.'), candidate.split('.'))
      matching = itertools.takewhile(lambda pair: pair[0] == pair[1], zipped)
      return [pair[0] for pair in matching]
    shared_packages = [_f for _f in map(shared_prefix, all_packages) if _f]
    return '.'.join(max(shared_packages, key=len)) if shared_packages else package
246182
def _infer_spaces(s):
    
    s = s.lower()

    # Find the best match for the i first characters, assuming cost has
    # been built for the i-1 first characters.
    # Returns a pair (match_cost, match_length).
    def best_match(i):
        candidates = enumerate(reversed(cost[max(0, i - MAXWORD):i]))
        return min((c + WORDCOST.get(s[i-k-1: i], 9e999), k + 1)
                    for k, c in candidates)

    # Build the cost array.
    cost = [0]
    for i in range(1, len(s) + 1):
        c, k = best_match(i)
        cost.append(c)

    # Backtrack to recover the minimal-cost string.
    out = []
    i = len(s)
    while i > 0:
        c, k = best_match(i)
        assert c == cost[i]
        out.append(s[i-k:i])
        i -= k

    return u" ".join(reversed(out))
923138
def results(self):
        
        d = dict()
        words = [word.strip() for word in self.haystack]

        if not words:
            raise NoResultException('No similar word found.')

        for w in words:
            d[w] = Levenshtein.ratio(self.needle, w)

        return sorted(d.items(), key=operator.itemgetter(1), reverse=True)
1244702
def index_nearest(value, array):
    

    a = (array-value)**2
    return index(a.min(), a)
1093593
def closest(self):
        """"""

        current = self.tag
        closest = None
        while closest is None and current is not None:
            if self.match(current):
                closest = current
            else:
                current = self.get_parent(current)
        return closest
948032
def min_count(self, n=1):
    
    word_count = {w:c for w,c in iteritems(self.word_count) if c >= n}
    return CountedVocabulary(word_count=word_count)
219427
def best_match(string_a, *string_b)
      similarities(string_a, *string_b).max_by { |_k, v| v }[0]
    end
1127009
def find_closest(self, color):
        
        # Find distance between colors and find name based on closest color
        rgb = sRGBColor(*color)
        lab = convert_color(rgb, LabColor, target_illuminant='D65')
        min_diff = float("inf")
        min_name, min_color = "", ()
        for known_name, known_color in self.items():
            known_rgb = sRGBColor(*known_color)
            known_lab = convert_color(known_rgb, LabColor,
                                      target_illuminant='D65')
            diff = delta_e_cie1976(lab, known_lab)
            if min_diff > diff:
                min_diff = diff
                min_name = known_name
                min_color = known_color
        return min_name, min_color
313317
def closest_value(root, target):
    
    a = root.val
    kid = root.left if target < a else root.right
    if not kid:
        return a
    b = closest_value(kid, target)
    return min((a,b), key=lambda x: abs(target-x))
1349246
def _component_closest_to_origin(components, glyph_set):
    
    return min(components, key=lambda comp: _distance((0, 0), _bounds(comp, glyph_set)))
92122
function closest(element, selector) {
    var closest;

    while (!closest) {
        if (matches(element, selector)) {
            closest = element;
        }

        element = parent(element);

        if (!element || element === document) {
            break;
        }
    }

    return closest;
}