All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.ucla.sspace.evaluation.DeeseAntonymEvaluation Maven / Gradle / Ivy

Go to download

The S-Space Package is a collection of algorithms for building Semantic Spaces as well as a highly-scalable library for designing new distributional semantics algorithms. Distributional algorithms process text corpora and represent the semantic for words as high dimensional feature vectors. This package also includes matrices, vectors, and numerous clustering algorithms. These approaches are known by many names, such as word spaces, semantic spaces, or distributed semantics and rest upon the Distributional Hypothesis: words that appear in similar contexts have similar meanings.

The newest version!
/*
 * Copyright 2010 
 *
 * This file is part of the S-Space package and is covered under the terms and
 * conditions therein.
 *
 * The S-Space package is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation and distributed hereunder to you.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND NO REPRESENTATIONS OR WARRANTIES,
 * EXPRESS OR IMPLIED ARE MADE.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, WE MAKE
 * NO REPRESENTATIONS OR WARRANTIES OF MERCHANT- ABILITY OR FITNESS FOR ANY
 * PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE OR DOCUMENTATION
 * WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
 * RIGHTS.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see .
 */

package edu.ucla.sspace.evaluation;

import edu.ucla.sspace.common.SemanticSpace;
import edu.ucla.sspace.common.Similarity;

import edu.ucla.sspace.util.Pair;

import edu.ucla.sspace.vector.Vector;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;


/**
 *
 * @see WordAssociationTest
 */
public class DeeseAntonymEvaluation extends AbstractWordAssociationTest {

    /**
     * The set of Deese antonyms
     */
    private static final Set> DEESE_ANTONYMS =
        new LinkedHashSet>();
    
    static {
        DEESE_ANTONYMS.add(new Pair("active", "passive"));
        DEESE_ANTONYMS.add(new Pair("bad", "good"));
        DEESE_ANTONYMS.add(new Pair("high", "low"));
        DEESE_ANTONYMS.add(new Pair("right", "wrong"));
        DEESE_ANTONYMS.add(new Pair("big", "little"));
        DEESE_ANTONYMS.add(new Pair("empty", "full"));
        DEESE_ANTONYMS.add(new Pair("narrow", "wide"));
        DEESE_ANTONYMS.add(new Pair("strong", "weak"));
        DEESE_ANTONYMS.add(new Pair("cold", "hot"));
        DEESE_ANTONYMS.add(new Pair("heavy", "light"));
        DEESE_ANTONYMS.add(new Pair("pretty", "ugly"));
        DEESE_ANTONYMS.add(new Pair("alone", "together"));
        DEESE_ANTONYMS.add(new Pair("few", "many"));
        DEESE_ANTONYMS.add(new Pair("dark", "light"));
        DEESE_ANTONYMS.add(new Pair("bottom", "top"));
        DEESE_ANTONYMS.add(new Pair("long", "short"));
        DEESE_ANTONYMS.add(new Pair("sour", "sweet"));
        DEESE_ANTONYMS.add(new Pair("clean", "dirty"));
        DEESE_ANTONYMS.add(new Pair("hard", "soft"));
        DEESE_ANTONYMS.add(new Pair("rich", "poor"));
        DEESE_ANTONYMS.add(new Pair("back", "front"));
        DEESE_ANTONYMS.add(new Pair("dry", "wet"));
        DEESE_ANTONYMS.add(new Pair("left", "right"));
        DEESE_ANTONYMS.add(new Pair("short", "tall"));
        DEESE_ANTONYMS.add(new Pair("far", "near"));
        DEESE_ANTONYMS.add(new Pair("easy", "hard"));
        DEESE_ANTONYMS.add(new Pair("happy", "sad"));
        DEESE_ANTONYMS.add(new Pair("old", "young"));
        DEESE_ANTONYMS.add(new Pair("alive", "dead"));
        DEESE_ANTONYMS.add(new Pair("deep", "shallow"));
        DEESE_ANTONYMS.add(new Pair("large", "small"));
        DEESE_ANTONYMS.add(new Pair("rough", "smooth"));
        DEESE_ANTONYMS.add(new Pair("black", "white"));
        DEESE_ANTONYMS.add(new Pair("fast", "slow"));
        DEESE_ANTONYMS.add(new Pair("new", "old"));
        DEESE_ANTONYMS.add(new Pair("thin", "thick"));
        DEESE_ANTONYMS.add(new Pair("first", "last"));
        DEESE_ANTONYMS.add(new Pair("single", "married"));
        DEESE_ANTONYMS.add(new Pair("inside", "outside"));
    }

    public DeeseAntonymEvaluation() {
        super(createMap());
    }
    
    private static Map,Double> createMap() {
        Map,Double> antonymToSimilarity = 
            new HashMap,Double>();
        for (Pair p : DEESE_ANTONYMS)
            antonymToSimilarity.put(p, 1d);
        return antonymToSimilarity;
    }

    /**
     * {@inheritDoc}
     */
    protected double getLowestScore() {
        return 0;
    }

    /**
     * {@inheritDoc}
     */
    protected double getHighestScore() {
        return 1;
    }

    /**
     * Returns the association of the two words on a scale of 0 to 1.
     *
     * @return the assocation or {@code null} if either {@code word1} or {@code
     *         word2} are not in the semantic space
     */
    protected Double computeAssociation(SemanticSpace sspace, 
                                        String word1, String word2) {
        Vector v1 = sspace.getVector(word1);
        Vector v2 = sspace.getVector(word2);
        if (v1 == null || v2 == null)
            return null;
        
        // Find the ranks of each of the two words to each other
        double rank1 = findRank(sspace, word1, word2);
        double rank2 = findRank(sspace, word2, word1);
        return 2d / (rank1 + rank2);
    }

    /**
     * Returns the average computer generated score on the Deese Antonymy test.
     */
    protected double computeScore(double[] humanScores, double[] compScores) {
        double average = 0;
        for (double score : compScores)
            average += score;
        return average / compScores.length;
    }

    /**
     * Returns the ranking k such that {@code other} is the kth
     * most similar word to {@code target} in the semantic space.
     */
    private int findRank(SemanticSpace sspace, 
                         String target, String other) {
        Vector v1 = sspace.getVector(target);
        Vector v2 = sspace.getVector(other);
        // Compute the base similarity between the two words
        double baseSim = Similarity.cosineSimilarity(v1, v2);
        int rank = 0;
        // Next, count how many words are more similar to the target than the
        // other word is
        for (String word : sspace.getWords()) {
            Vector v = sspace.getVector(word);
            double sim = Similarity.cosineSimilarity(v1, v);
            if (sim > baseSim)
                rank++;
        }
        return rank;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy