Examples of SemanticSpace


Examples of edu.ucla.sspace.common.SemanticSpace

        IteratorFactory.setProperties(props);

        // use the System properties in case the user specified them as
        // -Dprop=<val> to the JVM directly.

        SemanticSpace space = getSpace();
       
        // all the documents are listed in one file, with one document per line
        Iterator<Document> docIter = getDocumentIterator();
       
        processDocumentsAndSpace(space, docIter, numThreads, props);

        File outputPath = new File(argOptions.getPositionalArg(0));
        File outputFile = null;
        // If the path is a directory, generate the .sspace file name based on
        // the space's name, taking into account any duplicates
        if (outputPath.isDirectory()) {
            outputFile = (overwrite)
                ? new File(outputPath, space.getSpaceName() + EXT)
                : File.createTempFile(space.getSpaceName(), EXT, outputPath);

        }
        // Otherwise the user has specified a file name directly, which should
        // be used.
        else {
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

public class DeeseEvaluator {
    public static void main(String[] args) throws Exception {
        DeeseAntonymEvaluation  evaluator = new DeeseAntonymEvaluation();
        for (String file : args) {
            SemanticSpace sspace = SemanticSpaceIO.load(file);
            WordAssociationReport report = evaluator.evaluate(sspace);
            System.out.printf("%s: %.3f\n", file, report.correlation());
        }
    }
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

        // Load the semantic spaces one by one, evaluating each one as it's
        // loaded.
        Set<String> loadedSSpaces = new HashSet<String>();
        int spaces = argOptions.numPositionalArgs();
        for (int i = 0; i < spaces; ++i) {
            SemanticSpace sspace = null;
            String[] sspaceConfig = argOptions.getPositionalArg(i).split(",");
            String sspaceFileName = sspaceConfig[0];
            SimType comparisonFunction = SimType.COSINE;
            if (sspaceConfig.length > 1) {
                for (int j = 1; j < sspaceConfig.length; ++j) {
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

            }

            // Load the semantic space that has the predefined word senses from
            // disk and return an EvaluationWordsi instance.
            try {
                SemanticSpace sspace = SemanticSpaceIO.load(
                        options.getStringOption('e'));
                if (options.hasOption('E'))
                    reporter = new SemEvalReporter(System.out);
                return new EvaluationWordsi(
                        getAcceptedWords(), getExtractor(), sspace, reporter);
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

                    "of the data", sspace.getSpaceName());
            Map<String,Vector> termToVector =
                new HashMap<String,Vector>(sspace.getWords().size());
            for (String term : sspace.getWords())
                termToVector.put(term, sspace.getVector(term));
            SemanticSpace copy = new VectorMapSemanticSpace<Vector>(
                termToVector, "copy of " + sspace.getSpaceName(),
                sspace.getVectorLength());
            out.writeObject(copy);
        }
    }
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

            System.exit(1);
        }

        Clustering clustering = ReflectionUtil.getObjectInstance(
                options.getStringOption('a'));
        SemanticSpace sspace = new StaticSemanticSpace(
                options.getStringOption('s'));
        int numClusters = options.getIntOption('c', 0);

        Set<String> words = sspace.getWords();
        List<DoubleVector> vectors = new ArrayList<DoubleVector>();
        List<SparseDoubleVector> sparseVectors =
            new ArrayList<SparseDoubleVector>();
        for (String word : words) {
            Vector v = sspace.getVector(word);
            if (v instanceof SparseDoubleVector)
                sparseVectors.add((SparseDoubleVector) v);
            else
                vectors.add(Vectors.asDouble(sspace.getVector(word)));
        }

        Properties props = System.getProperties();
        Assignments assignments = null;
        if (sparseVectors.size() > 0) {
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

        }
       
        NearestNeighborFinder nnf = null;
        if (options.hasOption('C')) {
            try {
                SemanticSpace sspace =
                    SemanticSpaceIO.load(options.getStringOption('C'));
                int numWords = sspace.getWords().size();
                // See how many principle vectors to create
                int numPrincipleVectors = -1;
                if  (options.hasOption('p')) {
                    numPrincipleVectors = options.getIntOption('p');
                    if (numPrincipleVectors > numWords) {
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

           
            // Don't re-open .sspace files that are already loaded
            if (fileNameToSSpace.containsKey(sspaceFileName))
                break;

            SemanticSpace sspace = null;
            try {
                sspace = SemanticSpaceIO.load(sspaceFileName);
            } catch (Throwable t) {
                // Catch Throwable since this method may throw an IOError
                out.println("an error occurred while loading the semantic " +
                            "space from " + sspaceFileName + ":\n" + t);
                t.printStackTrace();
            }
            fileNameToSSpace.put(sspaceFileName, sspace);
            current = sspace;
            currentNnf = null;
            break;
        }

        // Removes all references to the space, which free the associated
        // memory.
        case UNLOAD: {
            if (!commandTokens.hasNext()) {
                out.println("missing .sspace file argument");
                return false;
            }
            String sspaceName = commandTokens.next();
            String aliased = aliasToFileName.get(sspaceName);
            SemanticSpace removed = null;
            if (aliased != null) {
                aliasToFileName.remove(sspaceName);
                removed = fileNameToSSpace.remove(aliased);
            }
            else {
                removed = fileNameToSSpace.remove(sspaceName);
                // Remove the alias for the file if it existed
                Iterator<Map.Entry<String,String>> it =
                    aliasToFileName.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String,String> e = it.next();
                    if (e.getValue().equals(sspaceName)) {
                        it.remove();
                        break;
                    }
                }
            }
           
            // If we are removing the current semantic space, reassign it to be
            // the oldest semantic space, or if none are available, null.
            if (removed == current) {
                Iterator<SemanticSpace> it =
                    fileNameToSSpace.values().iterator();
                current = (it.hasNext()) ? it.next() : null;
            }
            break;
        }

        // Creates an alias for a semantic space file.  This is useful for long
        // file names.
        case ALIAS: {
            if (!commandTokens.hasNext()) {
                out.println("missing .sspace file argument");
                return false;
            }
            String fileName = commandTokens.next();
            if (!fileNameToSSpace.containsKey(fileName)) {
                out.println(fileName + "is not currently loaded");
                return false;
            }
            if (!commandTokens.hasNext()) {
                out.println("missing alias name");
                return false;
            }
            String alias = commandTokens.next();
            aliasToFileName.put(alias, fileName);
            break;
        }

        // Finds the nearest neighbors to a word in the current semantic space
        case GET_NEIGHBORS: {
            if (!commandTokens.hasNext()) {
                out.println("missing word argument");
                return false;
            }
            String focusWord = commandTokens.next();

            int neighbors = 10;
            if (commandTokens.hasNext()) {
                String countStr = commandTokens.next();
                try {
                    neighbors = Integer.parseInt(countStr);
                } catch (NumberFormatException nfe) {
                    out.println("invalid number of neighbors: " + countStr);
                    return false;
                }
            }
           
            // If this is the first time the nearest neighbors have been
            // searched for, construct a new NNF
            if (currentNnf == null)
                currentNnf = new PartitioningNearestNeighborFinder(current);

            // Using the provided or default arguments find the closest
            // neighbors to the target word in the current semantic space
            SortedMultiMap<Double,String> mostSimilar =
                currentNnf.getMostSimilar(focusWord, neighbors);

            if (mostSimilar == null) {
                out.println(focusWord +
                            " is not in the current semantic space");
            }
            else {
                // Print each of the neighbors and their similarity score
                for (Map.Entry<Double,String> e : mostSimilar.entrySet()) {
                    out.println(e.getValue() + "\t" + e.getKey());
                }
            }
            break;
        }

        // Get the similarity for two words
        case GET_SIMILARITY: {
            if (current == null) {
                out.println("no current semantic space");
                return false;
            }

            if (!commandTokens.hasNext()) {
                out.println("missing word argument");
                return false;
            }
            String word1 = commandTokens.next();

            if (!commandTokens.hasNext()) {
                out.println("missing word argument");
                return false;
            }
            String word2 = commandTokens.next();                     

            Similarity.SimType simType = Similarity.SimType.COSINE;
            if (commandTokens.hasNext()) {
                // Upper case since it's an enum
                String simTypeStr = commandTokens.next().toUpperCase();
                try {
                    simType = Similarity.SimType.valueOf(simTypeStr);
                } catch (IllegalArgumentException iae) {
                    // See if the user provided a prefix of the similarity
                    // measure's name
                    for (Similarity.SimType t : Similarity.SimType.values())
                        if (t.name().startsWith(simTypeStr))
                            simType = t;
                    // If no prefix was found, report an error
                    if (simType == null) {
                        out.println("invalid similarity measure: " +simTypeStr);
                        return false;
                    }
                }
            }

            Vector word1vec = current.getVector(word1);
            if (word1vec == null) {
                out.println(word1 + " is not in semantic space "
                            + getCurrentSSpaceFileName());
                break;
            }
            Vector word2vec = current.getVector(word2);
            if (word2vec == null) {
                out.println(word2 + " is not in semantic space "
                            + getCurrentSSpaceFileName());
                break;
            }
           
            double similarity =
                Similarity.getSimilarity(simType, word1vec, word2vec);
            out.println(similarity);
            break;
        }

        // Compare the vectors for the same word from two different semantic
        // spaces
        case COMPARE_SSPACE_VECTORS: {

            if (!commandTokens.hasNext()) {
                out.println("missing word argument");
                return false;
            }
            String word = commandTokens.next();

            if (!commandTokens.hasNext()) {
                out.println("missing sspace argument");
                return false;
            }
            String name1 = commandTokens.next();
            SemanticSpace sspace1 = getSSpace(name1);
            if (sspace1 == null) {
                out.println("no such semantic space: " + name1);
                return false;
            }

            if (!commandTokens.hasNext()) {
                out.println("missing sspace argument");
                return false;
            }
            String name2 = commandTokens.next();
            SemanticSpace sspace2 = getSSpace(name2);
            if (sspace2 == null) {
                out.println("no such semantic space: " + name2);
                return false;
            }
           
            Similarity.SimType simType = Similarity.SimType.COSINE;
            if (commandTokens.hasNext()) {
                String simTypeStr = commandTokens.next();
                try {
                    simType = Similarity.SimType.valueOf(simTypeStr);
                } catch (IllegalArgumentException iae) {
                    out.println("invalid similarity measure: " + simTypeStr);
                    return false;
                }
            }

            // Get the vectors from each dimension
            Vector sspace1vec = sspace1.getVector(word);
            if (sspace1vec == null) {
                out.println(word + " is not in semantic space "
                            + name1);
                break;
            }
            Vector sspace2vec = sspace2.getVector(word);
            if (sspace2vec == null) {
                out.println(word + " is not in semantic space "
                            + name2);
                break;
            }

            // Ensure that the two have the same number of dimensions
            if (sspace1vec.length() != sspace2vec.length()) {
                out.println(name1 + " and " + name2 + " have different numbers "
                            + "of dimensions and are not comparable.");
                break;
            }

            double similarity =
                Similarity.getSimilarity(simType, sspace1vec, sspace2vec);
            out.println(similarity);
            break;
        }

        case HELP: {
            out.println("available commands:\n" + getCommands());
            break;
        }

        // Write the results of a command to a file
        case WRITE_COMMAND_RESULTS: {
            if (!commandTokens.hasNext()) {
                out.println("missing file destination argument");
                return false;
            }
            String fileName = commandTokens.next();
            try {
                // Open up a new output stream where the command's results will
                // be sent
                PrintStream ps = new PrintStream(fileName);
                // Recursively call execute using the file as the new output
                // stream
                execute(commandTokens, ps);
                ps.close();
            } catch (IOException ioe) {
                out.println("An error occurred while writing to " + fileName +
                            ":\n"  + ioe);
            }
            break;
        }

        // Print the vector for a word
        case PRINT_VECTOR: {
            if (current == null) {
                out.println("no current semantic space");
                return false;
            }

            if (!commandTokens.hasNext()) {
                out.println("missing word argument");
                return false;
            }
            String word = commandTokens.next();

            Vector vec = current.getVector(word);
            if (vec == null) {
                out.println(word + " is not in semantic space " +
                            getCurrentSSpaceFileName());
                break;
            }
           
            out.println(VectorIO.toString(vec));
            break;
        }

        // Update the current semantic space
        case SET_CURRENT_SSPACE: {
            if (!commandTokens.hasNext()) {
                out.println("missing .sspace file argument");
                return false;
            }
            String spaceName = commandTokens.next();
            // Check whether the name was an alias
            String fileName = aliasToFileName.get(spaceName);
            // If the argument wasn't an alias, the arg was the file name
            if (fileName == null)
                fileName = spaceName;
           
            SemanticSpace s = fileNameToSSpace.get(fileName);
            if (s == null) {
                out.println("no such .sspace (file is not currently loaded)");
                return false;
            }
            current = s;
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

        final int numSimilar = (argOptions.hasOption('n'))
            ? argOptions.getIntOption('n') : 10;

        LOGGER.fine("loading .sspace file: " + sspaceFile.getName());
       
        final SemanticSpace sspace = SemanticSpaceIO.load(sspaceFile);

        File output = (overwrite)
            ? new File(outputDir, sspaceFile.getName() + ".similarityList")
            : File.createTempFile(sspaceFile.getName(), "similarityList",
                                  outputDir);

        final PrintWriter outputWriter = new PrintWriter(output);
           
        final Set<String> words = sspace.getWords();
        NearestNeighborFinder nnf =
            new PartitioningNearestNeighborFinder(sspace);


        for (String word : words) {           
View Full Code Here

Examples of edu.ucla.sspace.common.SemanticSpace

        SimplePipeline.runPipeline(reader, segmenter, indexTermGenerator);
       

        File tmpFile = new File(folder.getRoot(), "test.sspace");
        SemanticSpace sspace = SemanticSpaceIO.load(tmpFile);
        assertEquals(11, sspace.getVectorLength());
        assertEquals(513, sspace.getWords().size());   
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.