<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.util;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.graph.builder.GraphBuilder;

import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

public interface Neighbors {
    static <V, E> Iterator<V> neighborIterator(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                iterator();
    }

    static <V, E> Set<V> neighborSetOf(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                collect(Collectors.toSet());
    }

    static <V, E> Graph<V, E> neighborhoodGraph(Graph<V, E> graph, V node) {
        final GraphBuilder<V, E, ? extends SimpleWeightedGraph<V, E>> builder = SimpleWeightedGraph.createBuilder(graph.getEdgeSupplier());

        final Set<V> neighborhood = neighborSetOf(graph, node);

        for (final V neighbor : neighborhood) {
            builder.addVertex(neighbor);

            for (final E edge : graph.edgesOf(neighbor)) {
                final V opposite = Graphs.getOppositeVertex(graph, edge, neighbor);

                if (neighborhood.contains(opposite)) {
                    builder.addVertex(opposite);
                    builder.addEdge(neighbor, opposite, edge, graph.getEdgeWeight(edge));
                }
            }
        }

        return builder.buildAsUnmodifiable();
    }
}<commit_msg>Simplify neighborhoodGraph<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.util;

import org.jgrapht.Graph;
import org.jgrapht.Graphs;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.graph.builder.GraphBuilder;

import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

public interface Neighbors {
    static <V, E> Iterator<V> neighborIterator(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                iterator();
    }

    static <V, E> Set<V> neighborSetOf(Graph<V, E> graph, V node) {
        return graph.edgesOf(node).stream().
                map(e -> Graphs.getOppositeVertex(graph, e, node)).
                collect(Collectors.toSet());
    }

    /**
     * Extract a neighborhood graph for the given node from a graph.
     *
     * @param graph graph
     * @param node  target node
     * @param <V>   node class
     * @param <E>   edge class
     * @return a neighborhood graph
     */
    static <V, E> Graph<V, E> neighborhoodGraph(Graph<V, E> graph, V node) {
        final GraphBuilder<V, E, ? extends SimpleWeightedGraph<V, E>> builder = SimpleWeightedGraph.createBuilder(graph.getEdgeSupplier());

        final Set<V> neighborhood = neighborSetOf(graph, node);
        neighborhood.forEach(builder::addVertex);

        for (final V neighbor : neighborhood) {
            for (final E edge : graph.edgesOf(neighbor)) {
                final V source = graph.getEdgeSource(edge);
                final V target = graph.getEdgeTarget(edge);

                if (neighborhood.contains(source) && neighborhood.contains(target)) {
                    final double weight = graph.getEdgeWeight(edge);
                    builder.addEdge(source, target, edge, weight);
                }
            }
        }

        return builder.buildAsUnmodifiable();
    }
}
{'fraction_non_alphanumeric': 0.10582462129293792, 'fraction_numerical': 0.0034136974610625135, 'mean_word_length': 2.726550079491256, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 35, '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': '6031810', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1300, 'n_words': 401}