org.dragoneronca.nlp.wol.disambiguation.WolPathFinder.java Source code

Java tutorial

Introduction

Here is the source code for org.dragoneronca.nlp.wol.disambiguation.WolPathFinder.java

Source

/*
 * Copyright Paolo Dragone 2014.
 * Copyright Alessandro Ronca 2014.
 *
 * This file is part of Wiktionary Ontology.
 *
 * Wiktionary Ontology is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wiktionary Ontology is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wiktionary Ontology. If not, see <http://www.gnu.org/licenses/>.
 */

package org.dragoneronca.nlp.wol.disambiguation;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import org.dragoneronca.util.graphs.Edge;
import org.dragoneronca.util.graphs.Node;
import org.dragoneronca.util.graphs.PathFinder;

import java.util.*;

/**
 * This class implements a <tt>filterEdges</tt> method specific for <tt>LightSemanticEdges</tt>.
 * <p/>
 * It uses the field <tt>targetWordHash</tt> to group edges referring to the same word and taking
 * only the max one, i.e. the disambiguated sense.
 *
 * @author Paolo Dragone
 * @author Alessandro Ronca
 */
public class WolPathFinder extends PathFinder {

    private final Set<Integer> excludedTermHashes;

    /**
     * It constructs a <tt>WolPathFinder</tt> given the parameters to define the kind of paths you
     * want.
     *
     * @param source             the source of the paths.
     * @param sinks              a set of sinks of the paths.
     * @param maxDepth           the maximum depth of the graph visit.
     * @param excludedTermHashes the edge filtering condition.
     */
    public WolPathFinder(Node source, Set<? extends Node> sinks, int maxDepth, Set<Integer> excludedTermHashes) {
        super(source, sinks, maxDepth);
        this.excludedTermHashes = excludedTermHashes;
    }

    @Override
    protected void filterEdges(Set<? extends Edge> edges) {

        Set<LightSemanticEdge> semanticEdges = (Set<LightSemanticEdge>) edges;
        Multimap<Integer, LightSemanticEdge> edgesByTermMap = LinkedHashMultimap.create();
        for (LightSemanticEdge semanticEdge : semanticEdges) {
            if (!excludedTermHashes.contains(semanticEdge.getTargetWordHash())) {
                edgesByTermMap.put(semanticEdge.getTargetWordHash(), semanticEdge);
            }
        }
        Set<LightSemanticEdge> goodSemanticEdges = new HashSet<>();
        for (Integer key : edgesByTermMap.keySet()) {
            Collection<LightSemanticEdge> lightSemanticEdges = edgesByTermMap.get(key);
            LightSemanticEdge maxEdge = Collections.max(lightSemanticEdges, new Comparator<LightSemanticEdge>() {
                @Override
                public int compare(LightSemanticEdge edge1, LightSemanticEdge edge2) {
                    return Double.compare(edge1.getWeight(), edge2.getWeight());
                }
            });
            goodSemanticEdges.add(maxEdge);
        }
        edges.retainAll(goodSemanticEdges);
    }

}