Search in sources :

Example 6 with DictEntry

use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.

the class DictionaryEditorController method createSetStringAction.

protected IListenerAction createSetStringAction(final int column) {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return DictionaryEditorUI.SetStringParms.class;
        }

        public boolean performAction(Object actionParms) {
            DictionaryEditorUI.SetStringParms p = (DictionaryEditorUI.SetStringParms) actionParms;
            if (p != null) {
                final int rowIndex = p.rowIndex;
                final String newString = p.string;
                IUndoableEdit edit = null;
                DictEntry entry = dictionary.getEntry(rowIndex);
                if (entry == null) {
                    interaction.reportProblem(ERROR_TITLE, NO_ENTRY);
                    return true;
                }
                CogToolLID lid;
                final String goal;
                final String search;
                final String oldGoal;
                final String oldSearch;
                if (column == 0) {
                    goal = newString;
                    oldGoal = entry.goalWord;
                    if (goal.equals(oldGoal)) {
                        interaction.setStatusMessage(STRING_UNCHANGED);
                        return true;
                    }
                    search = entry.searchWord;
                    oldSearch = search;
                    lid = DictionaryEditorLID.SetGoalString;
                } else {
                    goal = entry.goalWord;
                    oldGoal = goal;
                    search = newString;
                    oldSearch = entry.searchWord;
                    if (search.equals(oldSearch)) {
                        interaction.setStatusMessage(STRING_UNCHANGED);
                        return true;
                    }
                    lid = DictionaryEditorLID.SetSearchString;
                }
                if (checkNewEntry(goal, search, entry.algorithm)) {
                    interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
                    return false;
                }
                dictionary.updateEntry(rowIndex, goal, search);
                CompoundUndoableEdit editSeq = new CompoundUndoableEdit(MODIFY_TERM, lid);
                edit = new AUndoableEdit(lid) {

                    @Override
                    public String getPresentationName() {
                        return MODIFY_TERM;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        dictionary.updateEntry(rowIndex, goal, search);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        dictionary.updateEntry(rowIndex, oldGoal, oldSearch);
                    }
                };
                editSeq.addEdit(edit);
                if (entry.algorithm != ITermSimilarity.MANUAL) {
                    double similarity = computeSimilarity(goal, search, entry.algorithm);
                    setSimilarity(rowIndex, similarity, entry.algorithm, editSeq);
                }
                editSeq.end();
                undoMgr.addEdit(editSeq);
            }
            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) CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) 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) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 7 with DictEntry

use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.

the class DictionaryEditorUIModel method updateRow.

public void updateRow(int modifiedRow) {
    DictEntry entry = dictionary.getEntry(modifiedRow);
    if (entry != null) {
        TableItem row = dictTable.getItem(modifiedRow);
        row.setText(DictionaryEditorUIModel.GOAL_COL, entry.goalWord);
        row.setText(DictionaryEditorUIModel.SEARCH_COL, entry.searchWord);
        Combo c = (Combo) row.getData();
        c.select(DictionaryEditorUIModel.getAlgIndex(entry.algorithm));
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry) Combo(org.eclipse.swt.widgets.Combo)

Example 8 with DictEntry

use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.

the class DictionaryEditorUIModel method fillTable.

/**
     * Creates an empty table, or loads values from an existing dictionary
     */
public void fillTable() {
    List<DictEntry> entries = dictionary.getEntries();
    if (entries.size() > 0) {
        Iterator<DictEntry> entryIter = entries.iterator();
        while (entryIter.hasNext()) {
            DictEntry entry = entryIter.next();
            DictValue dValue = dictionary.getValue(entry);
            TableItem row = createEmptyRow();
            fillRow(row, entry, dValue);
        }
    }
    createEmptyRow();
}
Also used : DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) TableItem(org.eclipse.swt.widgets.TableItem) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry)

Example 9 with DictEntry

use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.

the class DictEntryGenerator method generateOneEntry.

private void generateOneEntry(String search, String goal, ITermSimilarity alg, ITermSimilarity.Continuable cont, ISimilarityDictionary dict, boolean computeAll, List<String> computeErrors, ProgressCallback progressCallback) {
    // search = clean(search);
    if (search == null || "".equals(search)) {
        return;
    }
    // TODO what if goal is null/empty?
    DictEntry entry = new DictEntry(goal, search, alg);
    // Even if computeAll is true, don't recompute entries that
    // have already been computed during this execution.
    boolean computed = newDictEntries.containsKey(entry) || updatedDictEntries.containsKey(entry);
    if ((!dict.containsEntry(entry) || computeAll) && !computed) {
        double similarity;
        if (alg == ITermSimilarity.MANUAL) {
            if (dict.containsEntry(entry)) {
                // Don't reset a manually entered similarity!
                return;
            }
            similarity = 0.0;
        } else {
            if (progressCallback != null) {
                progressCallback.updateProgress(0.0, goal + " : " + search);
            }
            similarity = alg.determineSimilarity(goal, search, computeErrors, cont);
        }
        DictValue newValue = new DictValue(similarity);
        if (dict.containsEntry(entry) && cont.isContinuing()) {
            DictValue oldValue = dict.getValue(entry);
            oldDictEntries.put(entry, new DictValue(oldValue.similarity, oldValue.editedDate));
            updatedDictEntries.put(entry, newValue);
        } else {
            newDictEntries.put(entry, newValue);
        }
    }
}
Also used : DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry)

Example 10 with DictEntry

use of edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry in project cogtool by cogtool.

the class DictionaryEditorCmd method importDictionary.

/**
     * Replace prevDict with dict after reading the entries from the file.
     * @param csvFile TODO
     */
// I (dfm) believe the above is a lie. I think prevDict was already replaced 
// by dict by the caller, and prevDict is simply a vestigal bit of data that
// is passed around and encapsulated in undo and redo forms for no reason
// at all. But I'm not sufficiently confident of that diagnosis to zap it
// just yet....
public static boolean importDictionary(final Design design, final ISimilarityDictionary dict, final ISimilarityDictionary prevDict, Interaction interaction, IUndoableEditSequence editSeq, String csvFile) {
    File dataFile = (csvFile != null ? new File(csvFile) : interaction.selectCSVFile());
    if (dataFile == null) {
        return false;
    }
    FileReader reader = null;
    List<String> outputLines = new ArrayList<String>();
    try {
        reader = new FileReader(dataFile);
        FileUtil.readLines(reader, outputLines);
    } catch (IOException e) {
        interaction.reportProblem("File I/O Error", e.getMessage());
        return false;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            interaction.reportProblem("File I/O Error on Close", e.getMessage());
            return false;
        }
    }
    if (outputLines.size() == 0) {
        interaction.reportProblem("Empty file", "Requested .csv file is empty");
        return false;
    }
    if (outputLines.size() < 3) {
        interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
        return false;
    }
    String[] headerCol = CSVSupport.getCells(outputLines.get(0));
    if ((headerCol.length != 2) || !(ISimilarityDictionary.COGTOOL_DICT_HEADER.equals(headerCol[0])) || !headerCol[1].equals(DICT_CSV_VERSION_0)) {
        interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
        return false;
    }
    final Map<DictEntry, DictValue> newEntries = new LinkedHashMap<DictEntry, DictValue>();
    for (int i = 2; i < outputLines.size(); i++) {
        String newLine = outputLines.get(i);
        String[] cols = CSVSupport.getCells(newLine);
        // [required] 0 is goal string
        // [required] 1 is search string
        // [required] 2 is algorithm name
        // [optional] 3 is site (for Google algs) or url (for LSA)
        // [optional] 4 is term space (for LSA)
        String site = (cols.length > 3) ? cols[3] : "";
        String space = (cols.length > 4) ? cols[4] : "";
        DictEntry entry = new DictEntry(cols[0], cols[1], getNameAlgorithm(cols[2], site, space));
        double similarity;
        Date editedDate;
        try {
            similarity = parseDouble(cols[4]);
            editedDate = new PersistentDate(PersistentDate.DATE_FORMAT.parse(cols[5]));
        } catch (Exception e) {
            throw new RcvrParsingException("Parsing error", e);
        }
        DictValue value = new DictValue(similarity, editedDate);
        newEntries.put(entry, value);
    }
    dict.insertEntries(newEntries, false);
    if (editSeq != null) {
        editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportDict) {

            @Override
            public String getPresentationName() {
                return IMPORT_DICTIONARY;
            }

            @Override
            public void redo() {
                super.redo();
                if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
                    design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, dict);
                } else {
                    dict.insertEntries(newEntries, false);
                }
            }

            @Override
            public void undo() {
                super.undo();
                if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
                    design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, prevDict);
                } else {
                    dict.removeEntries(newEntries);
                }
            }
        });
    }
    if (interaction != null) {
        interaction.setStatusMessage("Import successful");
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) IOException(java.io.IOException) PersistentDate(edu.cmu.cs.hcii.cogtool.util.PersistentDate) Date(java.util.Date) PersistentDate(edu.cmu.cs.hcii.cogtool.util.PersistentDate) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FileReader(java.io.FileReader) File(java.io.File)

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