<commit_before>/*
 * Copyright 2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.nlpub.watset.wsi;

import org.nlpub.watset.util.ContextSimilarity;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static org.nlpub.watset.util.Maximizer.argmax;

/**
 * A sense is a simple wrapper over an object that has a different address in the memory.
 *
 * @param <V> wrapped class.
 */
public interface Sense<V> extends Supplier<V> {
    /**
     * Disambiguate a context as according to its similarity to the senses in the inventory.
     * The method skips context keys which are marked as ignored.
     *
     * @param inventory  sense inventory.
     * @param similarity similarity measure.
     * @param context    context to disambiguate.
     * @param ignored    blacklisted keys.
     * @param <V>        context element class.
     * @return disambiguated context.
     */
    static <V> Map<Sense<V>, Number> disambiguate(Map<V, Map<Sense<V>, Map<V, Number>>> inventory, ContextSimilarity<V> similarity, Map<V, Number> context, Set<V> ignored) {
        final Map<Sense<V>, Number> dcontext = new HashMap<>(context.size());

        for (final Map.Entry<V, Number> entry : context.entrySet()) {
            final V target = entry.getKey();

            if (ignored.contains(target)) continue;

            final Sense<V> sense = argmax(inventory.getOrDefault(target, Collections.emptyMap()).keySet().iterator(), candidate -> {
                final Map<V, Number> candidateContext = inventory.get(target).get(candidate);
                return similarity.apply(context, candidateContext).doubleValue();
            }).orElseThrow(() -> new IllegalArgumentException("Cannot find the sense for the word in context."));
<commit_msg>Generalize to Collection in disambiguate [ci skip]<commit_after>/*
 * Copyright 2018 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.nlpub.watset.wsi;

import org.nlpub.watset.util.ContextSimilarity;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

import static org.nlpub.watset.util.Maximizer.argmax;

/**
 * A sense is a simple wrapper over an object that has a different address in the memory.
 *
 * @param <V> wrapped class.
 */
public interface Sense<V> extends Supplier<V> {
    /**
     * Disambiguate a context as according to its similarity to the senses in the inventory.
     * The method skips context keys which are marked as ignored.
     *
     * @param inventory  sense inventory.
     * @param similarity similarity measure.
     * @param context    context to disambiguate.
     * @param ignored    blacklisted keys.
     * @param <V>        context element class.
     * @return disambiguated context.
     */
    static <V> Map<Sense<V>, Number> disambiguate(Map<V, Map<Sense<V>, Map<V, Number>>> inventory, ContextSimilarity<V> similarity, Map<V, Number> context, Collection<V> ignored) {
        final Map<Sense<V>, Number> dcontext = new HashMap<>(context.size());

        for (final Map.Entry<V, Number> entry : context.entrySet()) {
            final V target = entry.getKey();

            if (ignored.contains(target)) continue;

            final Sense<V> sense = argmax(inventory.getOrDefault(target, Collections.emptyMap()).keySet().iterator(), candidate -> {
                final Map<V, Number> candidateContext = inventory.get(target).get(candidate);
                return similarity.apply(context, candidateContext).doubleValue();
            }).orElseThrow(() -> new IllegalArgumentException("Cannot find the sense for the word in context."));

{'fraction_non_alphanumeric': 0.1042931937172775, 'fraction_numerical': 0.003350785340314136, 'mean_word_length': 3.3856749311294765, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3511863', 'n_tokens_mistral': 1319, 'n_tokens_neox': 1278, 'n_words': 474}