Search in sources :

Example 11 with DictEntry

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;
}
Also used : DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) FileWriter(java.io.FileWriter) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) IOException(java.io.IOException) ISitedTermSimilarity(edu.cmu.cs.hcii.cogtool.model.ISitedTermSimilarity) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 12 with DictEntry

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;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ITermSimilarity(edu.cmu.cs.hcii.cogtool.model.ITermSimilarity) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 13 with DictEntry

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;
}
Also used : Table(org.eclipse.swt.widgets.Table) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) Text(org.eclipse.swt.widgets.Text) EventObject(java.util.EventObject) Point(org.eclipse.swt.graphics.Point)

Aggregations

DictEntry (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry)13 DictValue (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue)7 PendingDictEntry (edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry)7 AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)6 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)4 ITermSimilarity (edu.cmu.cs.hcii.cogtool.model.ITermSimilarity)3 DictionaryEditorUI (edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI)3 File (java.io.File)3 IOException (java.io.IOException)3 TableItem (org.eclipse.swt.widgets.TableItem)3 ISitedTermSimilarity (edu.cmu.cs.hcii.cogtool.model.ISitedTermSimilarity)2 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)2 BufferedWriter (java.io.BufferedWriter)2 Point (org.eclipse.swt.graphics.Point)2 Combo (org.eclipse.swt.widgets.Combo)2 Table (org.eclipse.swt.widgets.Table)2 CogToolLID (edu.cmu.cs.hcii.cogtool.CogToolLID)1 GensimLSASimilarity (edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity)1 LSASimilarity (edu.cmu.cs.hcii.cogtool.model.LSASimilarity)1 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)1