Package edu.ucla.sspace.vector

Examples of edu.ucla.sspace.vector.Vector


        // For each word, write out the word itself, followed by the '|'
        // character and then a list of space-separated values.
        for (String word : words) {
            StringBuilder sb = new StringBuilder(word);
            sb.append('|');
            Vector vector = sspace.getVector(word);           
            int length = vector.length();
            // Special case for the types just to make writing go a bit faster
            if (vector instanceof DoubleVector) {
                DoubleVector dv = (DoubleVector)vector;
                for (int i = 0; i < length - 1; ++i)
                    sb.append(dv.get(i)).append(" ");
                sb.append(dv.get(length - 1));
            }
            else if (vector instanceof IntegerVector) {
                IntegerVector iv = (IntegerVector)vector;
                for (int i = 0; i < length - 1; ++i)
                    sb.append(iv.get(i)).append(" ");
                sb.append(iv.get(length - 1));
            }
            else {
                for (int i = 0; i < length - 1; ++i)
                    sb.append(vector.getValue(i).doubleValue()).append(" ");
                sb.append(vector.getValue(length - 1).doubleValue());
            }
            pw.println(sb);
        }
        pw.close();
    }
View Full Code Here


        LOGGER.fine("saving binary S-Space with " + words.size() +
                    " words with " + dimensions + "-dimensional vectors");

        for (String word : words) {
            dos.writeUTF(word);
        Vector v = sspace.getVector(word);
        for (int i = 0; i < v.length(); ++i) {
            dos.writeDouble(v.getValue(i).doubleValue());
            }
        }
        dos.close();
    }
View Full Code Here

        for (String word : words) {
            pw.print(word + "|");
            // for each vector, write all the non-zero elements and their
            // indices
            Vector vector = sspace.getVector(word);
            StringBuilder sb = null;
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(nz[0]).append(",").append(sdv.get(nz[0]));
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(nz[i]).append(",").
                            append(sdv.getValue(nz[i]).doubleValue());
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();                   
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(nz[0]).append(",")
                        .append(sv.getValue(nz[0]).doubleValue());
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(nz[i]).append(",").
                            append(sv.getValue(nz[i]).doubleValue());
                }
            }
           
            else {
                boolean first = true;
                sb = new StringBuilder(dimensions / 2);
                for (int i = 0; i < vector.length(); ++i) {
                    double d = vector.getValue(i).doubleValue();
                    if (d != 0d) {
                        if (first) {
                            sb.append(i).append(",").append(d);
                            first = false;
                        }
View Full Code Here

        LOGGER.fine("saving sparse-binary S-Space with " + words.size() +
                    " words with " + dimensions + "-dimensional vectors");

        for (String word : words) {
            dos.writeUTF(word);
            Vector vector = sspace.getVector(word);
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    dos.writeInt(nz.length);
                    for (int i : nz) {
                        dos.writeInt(i);
                        dos.writeDouble(sdv.get(i));
                    }
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();
                    dos.writeInt(nz.length);
                    for (int i : nz) {
                        dos.writeInt(i);
                        dos.writeDouble(sv.getValue(i).doubleValue());
                    }
                }
            }
            else {
                // count how many are non-zero
                int nonZero = 0;
                for (int i = 0; i < vector.length(); ++i) {
                    if (vector.getValue(i).doubleValue() != 0d)
                        nonZero++;
                }
                dos.writeInt(nonZero);
                for (int i = 0; i < vector.length(); ++i) {
                    double d = vector.getValue(i).doubleValue();
                    if (d != 0d) {
                        dos.writeInt(i);
                        dos.writeDouble(d);
                    }
                }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    public SortedMultiMap<Double,String> getMostSimilar(
            final String word, int numberOfSimilarWords) {
        Vector v = sspace.getVector(word);
        // if the semantic space did not have the word, then return null
        if (v == null)
            return null;
        // Find the most similar words vectors to this word's vector, which will
        // end up including the word itself.  Therefore, increase the count by
View Full Code Here

            return null;
        // Compute the mean vector for all the terms
        DoubleVector mean = new DenseVector(sspace.getVectorLength());
        int found = 0;
        for (String term : terms) {
            Vector v = sspace.getVector(term);
            if (v == null)
                info(LOGGER, "No vector for term " + term);
            else {
                VectorMath.add(mean, v);
                found++;
View Full Code Here

                        // Record the similarity in a local data structure so we
                        // avoid locking the global mostSimilar term map
                        SortedMultiMap<Double,String> localMostSimTerms =
                            new BoundedSortedMultiMap<Double,String>(k, false);
                        for (String term : terms) {
                            Vector tVec = sspace.getVector(term);
                            double sim = Similarity.cosineSimilarity(v, tVec);
                            localMostSimTerms.put(sim, term);
                        }
                        // Lock the global map and then add the local results
                        synchronized (kMostSimTerms) {
View Full Code Here

        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)));
        }
View Full Code Here

                        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;
            }
View Full Code Here

        }

        // Iterate through each term in the document and sum the term Vectors
        // found in the provided SemanticSpace.
        for (Map.Entry<String, Integer> entry : termCounts.entrySet()) {
            Vector termVector = sspace.getVector(entry.getKey());
            if (termVector == null) {
        continue;
      }
            add(documentVector, termVector, entry.getValue());
        }
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.vector.Vector

Copyright © 2018 www.massapicom. 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.