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