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;
}