Search in sources :

Example 1 with DictEntry

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

Example 2 with DictEntry

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);
    }
}
Also used : Table(org.eclipse.swt.widgets.Table) SWTException(org.eclipse.swt.SWTException) TableItem(org.eclipse.swt.widgets.TableItem) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) GensimLSASimilarity(edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity) LSASimilarity(edu.cmu.cs.hcii.cogtool.model.LSASimilarity) Combo(org.eclipse.swt.widgets.Combo) ISitedTermSimilarity(edu.cmu.cs.hcii.cogtool.model.ISitedTermSimilarity) Point(org.eclipse.swt.graphics.Point) GensimLSASimilarity(edu.cmu.cs.hcii.cogtool.model.GensimLSASimilarity)

Example 3 with DictEntry

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);
        }
    });
}
Also used : ITermSimilarity(edu.cmu.cs.hcii.cogtool.model.ITermSimilarity) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) 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 4 with DictEntry

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;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) 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 5 with DictEntry

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;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) 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)

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