/**
* Runs the program
*/
public static void main(String[] args) {
ArgOptions options = new ArgOptions();
options.addOption('h', "help", "Generates a help message and exits",
false, null, "Program Options");
options.addOption('v', "verbose", "Enables verbose reporting",
false, null, "Program Options");
options.addOption('C', "createFinder", "Creates a nearest " +
"neighbor finder from the provided .sspace file",
true, "FILE", "Program Options");
options.addOption('L', "loadFinder", "Loads the finder from " +
"file", true, "FILE", "Program Options");
options.addOption('S', "saveFinder", "Saves the loaded or created " +
"finder to file", true, "FILE", "Program Options");
options.addOption('p', "principleVectors", "Specifies the number " +
"of principle vectors to create",
true, "INT", "Creation Options");
options.parseOptions(args);
if (options.hasOption("help") ||
(!options.hasOption('C') && !options.hasOption('L'))) {
usage(options);
return;
}
if (options.hasOption("verbose"))
LoggerUtil.setLevel(Level.FINE);
if (options.hasOption('C') && options.hasOption('L')) {
System.out.println("Cannot load and create a finder concurrently");
System.exit(1);
}
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) {
throw new IllegalArgumentException(
"Cannot have more principle vectors than " +
"word vectors: " + numPrincipleVectors);
}
else if (numPrincipleVectors < 1) {
throw new IllegalArgumentException(
"Must have at least one principle vector");
}
}
else {
numPrincipleVectors =
Math.min((int)(Math.ceil(Math.log(numWords))), 1000);
System.err.printf("Choosing a heuristically selected %d " +
"principle vectors%n",
numPrincipleVectors);
}
nnf = new PartitioningNearestNeighborFinder(
sspace, numPrincipleVectors);
} catch (IOException ioe) {
throw new IOError(ioe);
}
}
else if (options.hasOption('L')) {
nnf = SerializableUtil.<NearestNeighborFinder>load(
new File(options.getStringOption('L')));
}
else {
throw new IllegalArgumentException(
"Must either create or load a NearestNeighborFinder");
}
if (options.hasOption('S')) {
SerializableUtil.save(nnf, new File(options.getStringOption('S')));
}
int numWords = options.numPositionalArgs();
for (int i = 0; i < numWords; ++i) {
String term = options.getPositionalArg(i);
long start = System.currentTimeMillis();
MultiMap<Double,String> m = nnf.getMostSimilar(term, 10);
if (m == null) {
System.out.println(term + " is not in the semantic " +
"space; no neighbors found.");