use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorCmd method exportDictionaryToCSV.
public static boolean exportDictionaryToCSV(ISimilarityDictionary dictionary, Interaction interaction) {
File dest = interaction.selectCSVFileDest(DEFAULT_DICT_PREFIX + "" + nextNewDictSuffix++);
if (dest == null) {
return false;
}
if (dictionary.size() == 0) {
interaction.reportProblem("Export Dictionary", "Selected dictionary is empty");
return true;
}
FileWriter fw = null;
BufferedWriter buffer = null;
try {
fw = new FileWriter(dest);
buffer = new BufferedWriter(fw);
// first write out header, version, and column titles
CSVSupport.writeCell(ISimilarityDictionary.COGTOOL_DICT_HEADER, buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(DICT_CSV_VERSION_0, buffer);
CSVSupport.addLineEnding(buffer);
CSVSupport.writeCell("Term", buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell("Display Labels", buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell("Algorithm", buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell("Limiting URL", buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell("Similarity", buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell("Date computed", buffer);
CSVSupport.addLineEnding(buffer);
Iterator<DictEntry> values = dictionary.getEntries().iterator();
while (values.hasNext()) {
DictEntry entry = values.next();
DictValue value = dictionary.getValue(entry);
String url = "";
if (entry.algorithm instanceof ISitedTermSimilarity) {
url = ((ISitedTermSimilarity) entry.algorithm).getContextSite();
if (url == null) {
url = "";
}
}
String simil = toString(value.similarity);
CSVSupport.writeCell(entry.goalWord, buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(entry.searchWord, buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(getAlgorithmName(entry.algorithm), buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(url, buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(simil, buffer);
CSVSupport.addSeparator(buffer);
CSVSupport.writeCell(value.editedDate.toString(), buffer);
CSVSupport.addLineEnding(buffer);
}
} catch (IOException e) {
interaction.reportProblem("File I/O Error", e.getMessage());
return false;
} finally {
try {
if (buffer != null) {
buffer.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException e) {
interaction.reportProblem("File I/O Error on Close", e.getMessage());
return false;
}
}
interaction.setStatusMessage("Export successful");
return true;
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorController method createSetSimilarityAction.
protected IListenerAction createSetSimilarityAction() {
return new IListenerAction() {
public Class<?> getParameterClass() {
return DictionaryEditorUI.SetSimilarityParms.class;
}
public boolean performAction(Object actionParms) {
DictionaryEditorUI.SetSimilarityParms parms = (DictionaryEditorUI.SetSimilarityParms) actionParms;
DictEntry entry = dictionary.getEntry(parms.rowIndex);
if (entry == null) {
final double oldSimil = pendingEntry.getSimilarity();
final double newSimil = parms.similarity;
if (PrecisionUtilities.withinEpsilon(oldSimil, newSimil, 0.001)) {
interaction.setStatusMessage(SIMIL_UNCHANGED);
return true;
}
final ITermSimilarity oldAlg = pendingEntry.getDictEntry().algorithm;
pendingEntry.setSimilarity(newSimil);
pendingEntry.setAlgorithm(ITermSimilarity.MANUAL);
undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.SetSimilarity) {
@Override
public String getPresentationName() {
return SET_SIMILARITY;
}
@Override
public void redo() {
super.redo();
pendingEntry.setSimilarity(newSimil);
pendingEntry.setAlgorithm(ITermSimilarity.MANUAL);
}
@Override
public void undo() {
super.undo();
pendingEntry.setSimilarity(oldSimil);
pendingEntry.setAlgorithm(oldAlg);
}
});
return true;
}
if (PrecisionUtilities.withinEpsilon(dictionary.getValue(entry).similarity, parms.similarity, 0.001)) {
interaction.setStatusMessage(SIMIL_UNCHANGED);
return true;
}
setSimilarity(parms.rowIndex, parms.similarity, ITermSimilarity.MANUAL, undoMgr);
return true;
}
};
}
use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.
the class DictionaryEditorUI method commitEdit.
protected boolean commitEdit() {
Text actualEditor = (Text) editor.getEditor();
if (actualEditor != null) {
Table dictTable = view.getDictTable();
modifiedRow = dictTable.getSelectionIndex();
if (modifiedRow < 0) {
cleanupEditor();
return true;
}
Integer editCol = (Integer) actualEditor.getData();
int col = editCol.intValue();
String value = actualEditor.getText();
Object editParms;
if (!("".equals(value))) {
DictEntry entry = dictionary.getEntry(modifiedRow);
if (col == DictionaryEditorUIModel.SIMIL_COL) {
// check this first because we set this regardless of
// whether there's an entry for this row already
double simil = parseDouble(value);
editParms = new DictionaryEditorUI.SetSimilarityParms(simil, modifiedRow);
performAction(DictionaryEditorLID.SetSimilarity, editParms);
} else if (entry == null) {
addTerm(col, value, modifiedRow);
} else if (col == DictionaryEditorUIModel.GOAL_COL) {
editParms = new DictionaryEditorUI.SetStringParms(value, modifiedRow);
performAction(DictionaryEditorLID.SetGoalString, editParms);
} else if (col == DictionaryEditorUIModel.SEARCH_COL) {
editParms = new DictionaryEditorUI.SetStringParms(value, modifiedRow);
performAction(DictionaryEditorLID.SetSearchString, editParms);
}
} else {
// text is empty, set similarity to UNKNOWN if applicable
if (col == DictionaryEditorUIModel.SIMIL_COL) {
double simil = ITermSimilarity.UNKNOWN;
editParms = new DictionaryEditorUI.SetSimilarityParms(simil, modifiedRow);
performAction(DictionaryEditorLID.SetSimilarity, editParms);
}
}
}
cleanupEditor();
return true;
}
Aggregations