use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class SNIFACTPredictionAlgo method createSimilarityScoresFile.
protected String createSimilarityScoresFile(ISimilarityDictionary dict) {
// if (dict.size() == 0) {
// return null; // TODO ask about
// }
OutputStreamWriter fw = null;
BufferedWriter buffer = null;
String scoresPath = null;
try {
File dest = File.createTempFile("scores", ".lisp");
dest.deleteOnExit();
fw = new OutputStreamWriter(new FileOutputStream(dest), "US-ASCII");
buffer = new BufferedWriter(fw);
Iterator<DictEntry> entries = dict.getEntries().iterator();
while (entries.hasNext()) {
DictEntry entry = entries.next();
if ((parameters.algorithm == ITermSimilarity.ALL) || entry.algorithm.getClass().isInstance(parameters.algorithm)) {
DictValue value = dict.getValue(entry);
double simil = (value.similarity == ITermSimilarity.UNKNOWN) ? 0.0 : value.similarity;
// escape any \'s and "'s in the input strings
String quotedGoal = entry.goalWord.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"");
String quotedSearch = entry.searchWord.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"");
buffer.write("(store-score \"" + quotedGoal + "\" \"");
buffer.write(quotedSearch + "\" " + simil + ")\n");
}
}
scoresPath = dest.getAbsolutePath();
} catch (IOException e) {
throw new ComputationException("Writing file failed", e);
} finally {
try {
if (buffer != null) {
buffer.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
throw new ComputationException("Closing writer failed", e);
}
}
return scoresPath;
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorUI method updateView.
protected void updateView() {
Table dictTable = view.getDictTable();
int selectionCount = 0;
try {
selectionCount = dictTable.getSelectionCount();
} catch (SWTException e) {
return;
}
if (selectionCount == 1) {
int index = dictTable.getSelectionIndex();
DictEntry entry = dictionary.getEntry(index);
if (entry != null) {
String url = null;
String space = null;
if (entry.algorithm instanceof ISitedTermSimilarity) {
url = ((ISitedTermSimilarity) entry.algorithm).getContextSite();
view.setURLEnabled(true);
view.setSpaceEnabled(false);
} else if (entry.algorithm instanceof LSASimilarity) {
LSASimilarity lsa = (LSASimilarity) entry.algorithm;
space = lsa.getSpace();
url = lsa.getURL();
if (url == null) {
url = LSASimilarity.DEFAULT_LSA_URL;
}
view.setURLEnabled(true);
view.setSpaceEnabled(true);
} else if (entry.algorithm instanceof GensimLSASimilarity) {
GensimLSASimilarity lsa = (GensimLSASimilarity) entry.algorithm;
space = lsa.getSpace();
url = lsa.getURL();
if (url == null) {
url = GensimLSASimilarity.DEFAULT_LSA_URL;
}
view.setURLEnabled(true);
view.setSpaceEnabled(true);
} else {
view.setURLEnabled(false);
view.setSpaceEnabled(false);
}
view.setURL(url);
view.setSpace(space);
} else {
TableItem row = dictTable.getItem(index);
Combo c = (Combo) row.getData();
int seln = c.getSelectionIndex();
boolean isLSA = (seln == DictionaryEditorUIModel.LSA_INDEX);
view.setURLEnabled((seln == DictionaryEditorUIModel.GOOGLE_WORD_INDEX) || (seln == DictionaryEditorUIModel.GOOGLE_PHRASE_INDEX) || isLSA);
view.setSpaceEnabled(isLSA);
}
} else {
// disable if 0 or more than 1 are selected
view.setURL(null);
view.setSpace(null);
view.setURLEnabled(false);
view.setSpaceEnabled(false);
}
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorController method setSimilarity.
protected void setSimilarity(final int rowIndex, final double similarity, final ITermSimilarity algorithm, IUndoableEditSequence editSeq) {
DictEntry entry = dictionary.getEntry(rowIndex);
final String goal = entry.goalWord;
final String search = entry.searchWord;
DictValue oldV = dictionary.getValue(entry);
final DictValue oldValue = new DictValue(oldV.similarity, oldV.editedDate);
final DictValue newValue = new DictValue(similarity);
if (PrecisionUtilities.withinEpsilon(oldValue.similarity, similarity, 0.001)) {
interaction.setStatusMessage(SIMIL_UNCHANGED);
return;
}
final ITermSimilarity oldAlg = entry.algorithm;
dictionary.setSimilarity(goal, search, algorithm, newValue, rowIndex);
editSeq.addEdit(new AUndoableEdit(DictionaryEditorLID.SetSimilarity) {
@Override
public String getPresentationName() {
return SET_SIMILARITY;
}
@Override
public void redo() {
super.redo();
dictionary.setSimilarity(goal, search, algorithm, newValue, rowIndex);
}
@Override
public void undo() {
super.undo();
dictionary.setSimilarity(goal, search, oldAlg, oldValue, rowIndex);
}
});
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorController method createSetAlgorithmAction.
protected IListenerAction createSetAlgorithmAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return DictionaryEditorUI.SetAlgorithmParms.class;
}
public boolean performAction(Object actionParms) {
DictionaryEditorUI.SetAlgorithmParms parms = (DictionaryEditorUI.SetAlgorithmParms) actionParms;
double similarity;
if (parms.rowIndices.length == 0) {
interaction.reportProblem(ERROR_TITLE, NO_SELECTION);
return false;
}
CompoundUndoableEdit editSeq = new CompoundUndoableEdit(SET_ALGORITHM, DictionaryEditorLID.SetAlgorithm);
for (int rowIndice : parms.rowIndices) {
DictEntry entry = dictionary.getEntry(rowIndice);
if (entry == null) {
final ITermSimilarity oldAlg = pendingEntry.getDictEntry().algorithm;
final double oldSimilarity = pendingEntry.getSimilarity();
final ITermSimilarity newAlg = parms.algorithm;
boolean equal = (newAlg == null) ? oldAlg == null : newAlg.equals(oldAlg);
if (equal) {
interaction.setStatusMessage(ALG_UNCHANGED);
continue;
}
pendingEntry.setAlgorithm(newAlg);
pendingEntry.setSimilarity(ITermSimilarity.UNKNOWN);
editSeq.addEdit(new AUndoableEdit(DictionaryEditorLID.SetAlgorithm) {
@Override
public String getPresentationName() {
return SET_ALGORITHM;
}
@Override
public void redo() {
super.redo();
pendingEntry.setAlgorithm(newAlg);
pendingEntry.setSimilarity(ITermSimilarity.UNKNOWN);
}
@Override
public void undo() {
super.undo();
pendingEntry.setAlgorithm(oldAlg);
pendingEntry.setSimilarity(oldSimilarity);
}
});
interaction.setStatusMessage(ALG_CHANGED);
continue;
}
if (parms.algorithm.equals(entry.algorithm)) {
interaction.setStatusMessage(ALG_UNCHANGED);
continue;
}
if (checkNewEntry(entry.goalWord, entry.searchWord, parms.algorithm)) {
interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
continue;
}
if (parms.algorithm != ITermSimilarity.MANUAL) {
similarity = computeSimilarity(entry.goalWord, entry.searchWord, parms.algorithm);
} else {
similarity = dictionary.getValue(entry).similarity;
}
setSimilarity(rowIndice, similarity, parms.algorithm, editSeq);
}
editSeq.end();
if (editSeq.isSignificant()) {
undoMgr.addEdit(editSeq);
interaction.setStatusMessage(ALG_CHANGED);
}
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorController method createDeleteEntryAction.
protected IListenerAction createDeleteEntryAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return int[].class;
}
public boolean performAction(Object actionParms) {
final int[] rows = (int[]) actionParms;
final DictEntry[] deletedEntries = new DictEntry[rows.length];
final DictValue[] deletedValues = new DictValue[rows.length];
for (int i = rows.length - 1; i >= 0; i--) {
deletedEntries[i] = dictionary.getEntry(rows[i]);
if (deletedEntries[i] == null) {
interaction.reportProblem(ERROR_TITLE, NO_ENTRY);
continue;
}
deletedValues[i] = dictionary.getValue(deletedEntries[i]);
dictionary.removeEntry(rows[i]);
}
undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.Delete) {
@Override
public String getPresentationName() {
return (rows.length == 1) ? DELETE_ROW : DELETE_ROWS;
}
@Override
public void redo() {
super.redo();
for (int i = rows.length - 1; i >= 0; i--) {
dictionary.removeEntry(rows[i]);
}
}
@Override
public void undo() {
super.undo();
for (int i = 0; i < rows.length; i++) {
dictionary.insertEntry(deletedEntries[i], deletedValues[i], rows[i]);
}
}
});
return true;
}
};
}
Aggregations