edu.ucla.sspace.tools.SelectTopKWords Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sspace-wordsi Show documentation
Show all versions of sspace-wordsi Show documentation
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!
package edu.ucla.sspace.tools;
import edu.ucla.sspace.basis.BasisMapping;
import edu.ucla.sspace.matrix.Matrix;
import edu.ucla.sspace.matrix.MatrixIO;
import edu.ucla.sspace.matrix.MatrixIO.Format;
import edu.ucla.sspace.util.BoundedSortedMultiMap;
import edu.ucla.sspace.util.MultiMap;
import edu.ucla.sspace.util.SerializableUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Keith Stevens
*/
public class SelectTopKWords {
public static void main(String[] args) throws Exception {
// Load the basis mapping.
BasisMapping basis =
SerializableUtil.load(new File(args[0]));
// Create the top 10 lists for each topic in the word space.
List> topTerms = new ArrayList>();
Matrix m = MatrixIO.readMatrix(new File(args[1]), Format.DENSE_TEXT);
for (int c = 0; c < m.columns(); ++c)
topTerms.add(new BoundedSortedMultiMap(10));
for (int r = 0; r < m.rows(); ++r) {
String term = basis.getDimensionDescription(r);
for (int c = 0; c < m.columns(); ++c)
topTerms.get(c).put(m.get(r, c), term);
}
for (MultiMap topicTerms : topTerms) {
for (String term : topicTerms.values())
System.out.printf("%s ", term);
System.out.println();
}
}
}