Search in sources :

Example 56 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class CorefEditor method showTypeWiseAnnotations.

/**
 * When user preses the show Toggle button, this will show up annotations
 * of selected Type from selected AnnotationSet
 */
private void showTypeWiseAnnotations() {
    if (typeSpecificHighlightedTags == null) {
        highlightedTypeAnnots = new ArrayList<Annotation>();
        typeSpecificHighlightedTags = new ArrayList<Object>();
    }
    if (showAnnotations.isSelected()) {
        // get the annotationsSet and its type
        AnnotationSet set = getAnnotationSet((String) annotSets.getSelectedItem());
        String type = (String) annotTypes.getSelectedItem();
        if (type == null) {
            try {
                JOptionPane.showMessageDialog(MainFrame.getInstance(), "No annotation type found to display");
            } catch (Exception e) {
                e.printStackTrace();
            }
            showAnnotations.setSelected(false);
            return;
        }
        Color color = AnnotationSetsView.getColor(getAnnotationSet((String) annotSets.getSelectedItem()).getName(), type);
        if (type != null) {
            AnnotationSet typeSet = set.get(type);
            Iterator<Annotation> iter = typeSet.iterator();
            while (iter.hasNext()) {
                Annotation ann = iter.next();
                highlightedTypeAnnots.add(ann);
                try {
                    typeSpecificHighlightedTags.add(highlighter.addHighlight(ann.getStartNode().getOffset().intValue(), ann.getEndNode().getOffset().intValue(), new DefaultHighlighter.DefaultHighlightPainter(color)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            // typeSpecificHighlightedTags.add(textView.addHighlight(ann, getAnnotationSet((String)annotSets.getSelectedItem()),color));
            }
        }
    } else {
        for (int i = 0; i < typeSpecificHighlightedTags.size(); i++) {
            // textView.removeHighlight(typeSpecificHighlightedTags.get(i));
            highlighter.removeHighlight(typeSpecificHighlightedTags.get(i));
        }
        typeSpecificHighlightedTags = new ArrayList<Object>();
        highlightedTypeAnnots = new ArrayList<Annotation>();
        highlightedTypeAnnotsOffsets = null;
    }
    // This is to make process faster.. instead of accessing each annotation and
    // its offset, we create an array with its annotation offsets to search faster
    Collections.sort(highlightedTypeAnnots, new gate.util.OffsetComparator());
    highlightedTypeAnnotsOffsets = new int[highlightedTypeAnnots.size() * 2];
    for (int i = 0, j = 0; j < highlightedTypeAnnots.size(); i += 2, j++) {
        Annotation ann1 = highlightedTypeAnnots.get(j);
        highlightedTypeAnnotsOffsets[i] = ann1.getStartNode().getOffset().intValue();
        highlightedTypeAnnotsOffsets[i + 1] = ann1.getEndNode().getOffset().intValue();
    }
}
Also used : Color(java.awt.Color) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation) Point(java.awt.Point)

Example 57 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class CorefEditor method findOutTheLongestAnnotation.

/**
 * Given arrayList containing Ids of the annotations, and an annotationSet, this method
 * returns the annotations that has longest string among the matches
 */
public Annotation findOutTheLongestAnnotation(List<Integer> matches, AnnotationSet set) {
    if (matches == null || matches.size() == 0) {
        return null;
    }
    int length = 0;
    int index = 0;
    for (int i = 0; i < matches.size(); i++) {
        Annotation currAnn = set.get(matches.get(i));
        int start = currAnn.getStartNode().getOffset().intValue();
        int end = currAnn.getEndNode().getOffset().intValue();
        if ((end - start) > length) {
            length = end - start;
            index = i;
        }
    }
    // so now we now have the longest String annotations at index
    return set.get(matches.get(index));
}
Also used : Point(java.awt.Point) Annotation(gate.Annotation)

Example 58 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class CorefEditor method highlightAnnotations.

/**
 * This methods highlights the annotations
 */
public void highlightAnnotations() {
    if (highlightedTags == null) {
        highlightedTags = new HashMap<CorefTreeNode, List<Object>>();
        highlightedChainAnnots = new ArrayList<Annotation>();
    }
    AnnotationSet annotSet = getAnnotationSet((String) annotSets.getSelectedItem());
    CorefTreeNode annotSetNode = corefAnnotationSetNodesMap.get(annotSets.getSelectedItem());
    if (annotSetNode == null) {
        return;
    }
    Map<CorefTreeNode, List<Integer>> chainMap = corefChains.get(annotSetNode);
    Iterator<CorefTreeNode> iter = chainMap.keySet().iterator();
    while (iter.hasNext()) {
        CorefTreeNode currentNode = iter.next();
        if (currentSelections.get(currentNode.toString()).booleanValue()) {
            if (!highlightedTags.containsKey(currentNode)) {
                // find out the arrayList
                List<Integer> ids = chainMap.get(currentNode);
                ArrayList<Object> highlighTag = new ArrayList<Object>();
                if (ids != null) {
                    for (int i = 0; i < ids.size(); i++) {
                        Annotation ann = annotSet.get(ids.get(i));
                        highlightedChainAnnots.add(ann);
                        Color color = currentColors.get(currentNode.toString());
                        try {
                            highlighTag.add(highlighter.addHighlight(ann.getStartNode().getOffset().intValue(), ann.getEndNode().getOffset().intValue(), new DefaultHighlighter.DefaultHighlightPainter(color)));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    // highlighTag.add(textView.addHighlight(ann, getAnnotationSet((String) annotSets.getSelectedItem()), color));
                    }
                    highlightedTags.put(currentNode, highlighTag);
                }
            }
        } else {
            if (highlightedTags.containsKey(currentNode)) {
                List<Object> highlights = highlightedTags.get(currentNode);
                for (int i = 0; i < highlights.size(); i++) {
                    // textView.removeHighlight(highlights.get(i));
                    highlighter.removeHighlight(highlights.get(i));
                }
                highlightedTags.remove(currentNode);
                List<Integer> ids = chainMap.get(currentNode);
                if (ids != null) {
                    for (int i = 0; i < ids.size(); i++) {
                        Annotation ann = annotSet.get(ids.get(i));
                        highlightedChainAnnots.remove(ann);
                    }
                }
            }
        }
    }
    // This is to make process faster.. instead of accessing each annotation and
    // its offset, we create an array with its annotation offsets to search faster
    Collections.sort(highlightedChainAnnots, new gate.util.OffsetComparator());
    highlightedChainAnnotsOffsets = new int[highlightedChainAnnots.size() * 2];
    for (int i = 0, j = 0; j < highlightedChainAnnots.size(); i += 2, j++) {
        Annotation ann1 = highlightedChainAnnots.get(j);
        highlightedChainAnnotsOffsets[i] = ann1.getStartNode().getOffset().intValue();
        highlightedChainAnnotsOffsets[i + 1] = ann1.getEndNode().getOffset().intValue();
    }
}
Also used : Color(java.awt.Color) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation) Point(java.awt.Point) List(java.util.List) ArrayList(java.util.ArrayList)

Example 59 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationEditor method initListeners.

protected void initListeners() {
    // resize the window when the table changes.
    featuresEditor.addComponentListener(new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            // the table has changed size -> resize the window too!
            popupWindow.pack();
        }
    });
    KeyAdapter keyAdapter = new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            hideTimer.stop();
        }
    };
    typeCombo.getEditor().getEditorComponent().addKeyListener(keyAdapter);
    MouseListener windowMouseListener = new MouseAdapter() {

        @Override
        public void mouseEntered(MouseEvent evt) {
            hideTimer.stop();
        }

        // allow a JWindow to be dragged with a mouse
        @Override
        public void mousePressed(MouseEvent me) {
            pressed = me;
        }
    };
    MouseMotionListener windowMouseMotionListener = new MouseMotionAdapter() {

        Point location;

        // allow a JWindow to be dragged with a mouse
        @Override
        public void mouseDragged(MouseEvent me) {
            location = popupWindow.getLocation(location);
            int x = location.x - pressed.getX() + me.getX();
            int y = location.y - pressed.getY() + me.getY();
            popupWindow.setLocation(x, y);
            pinnedButton.setSelected(true);
        }
    };
    popupWindow.getRootPane().addMouseListener(windowMouseListener);
    popupWindow.getRootPane().addMouseMotionListener(windowMouseMotionListener);
    InputMap inputMap = ((JComponent) popupWindow.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    actionMap = ((JComponent) popupWindow.getContentPane()).getActionMap();
    // add the key-action bindings of this Component to the parent window
    solAction = new StartOffsetLeftAction("", MainFrame.getIcon("extend-left"), SOL_DESC, KeyEvent.VK_LEFT);
    solButton.setAction(solAction);
    setShortCuts(inputMap, SOL_KEY_STROKES, "solAction");
    actionMap.put("solAction", solAction);
    sorAction = new StartOffsetRightAction("", MainFrame.getIcon("extend-right"), SOR_DESC, KeyEvent.VK_RIGHT);
    sorButton.setAction(sorAction);
    setShortCuts(inputMap, SOR_KEY_STROKES, "sorAction");
    actionMap.put("sorAction", sorAction);
    delAction = new DeleteAnnotationAction("", MainFrame.getIcon("remove-annotation"), "Delete the annotation", KeyEvent.VK_DELETE);
    delButton.setAction(delAction);
    inputMap.put(KeyStroke.getKeyStroke("alt DELETE"), "delAction");
    actionMap.put("delAction", delAction);
    eolAction = new EndOffsetLeftAction("", MainFrame.getIcon("extend-left"), EOL_DESC, KeyEvent.VK_LEFT);
    eolButton.setAction(eolAction);
    setShortCuts(inputMap, EOL_KEY_STROKES, "eolAction");
    actionMap.put("eolAction", eolAction);
    eorAction = new EndOffsetRightAction("", MainFrame.getIcon("extend-right"), EOR_DESC, KeyEvent.VK_RIGHT);
    eorButton.setAction(eorAction);
    setShortCuts(inputMap, EOR_KEY_STROKES, "eorAction");
    actionMap.put("eorAction", eorAction);
    pinnedButton.setToolTipText("<html>Press to pin window in place" + "&nbsp;&nbsp;<font color=#667799><small>Ctrl-P" + "&nbsp;&nbsp;</small></font></html>");
    inputMap.put(KeyStroke.getKeyStroke("control P"), "toggle pin");
    actionMap.put("toggle pin", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            pinnedButton.doClick();
        }
    });
    DismissAction dismissAction = new DismissAction("", null, "Close the window", KeyEvent.VK_ESCAPE);
    dismissButton.setAction(dismissAction);
    inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), "dismissAction");
    inputMap.put(KeyStroke.getKeyStroke("alt ESCAPE"), "dismissAction");
    actionMap.put("dismissAction", dismissAction);
    ApplyAction applyAction = new ApplyAction("Apply", null, "", KeyEvent.VK_ENTER);
    inputMap.put(KeyStroke.getKeyStroke("alt ENTER"), "applyAction");
    actionMap.put("applyAction", applyAction);
    typeCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            String newType = typeCombo.getSelectedItem().toString();
            if (ann == null || ann.getType().equals(newType))
                return;
            // annotation editing
            Integer oldId = ann.getId();
            Annotation oldAnn = ann;
            set.remove(ann);
            try {
                set.add(oldId, oldAnn.getStartNode().getOffset(), oldAnn.getEndNode().getOffset(), newType, oldAnn.getFeatures());
                Annotation newAnn = set.get(oldId);
                // update the selection to the new annotation
                getOwner().selectAnnotation(new AnnotationDataImpl(set, newAnn));
                editAnnotation(newAnn, set);
                owner.annotationChanged(newAnn, set, oldAnn.getType());
            } catch (InvalidOffsetException ioe) {
                throw new GateRuntimeException(ioe);
            }
        }
    });
    hideTimer = new Timer(HIDE_DELAY, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            annotationEditorInstance.setVisible(false);
        }
    });
    hideTimer.setRepeats(false);
    AncestorListener textAncestorListener = new AncestorListener() {

        @Override
        public void ancestorAdded(AncestorEvent event) {
            if (wasShowing) {
                annotationEditorInstance.setVisible(true);
            }
            wasShowing = false;
        }

        @Override
        public void ancestorRemoved(AncestorEvent event) {
            if (isShowing()) {
                wasShowing = true;
                popupWindow.dispose();
            }
        }

        @Override
        public void ancestorMoved(AncestorEvent event) {
        }

        private boolean wasShowing = false;
    };
    owner.getTextComponent().addAncestorListener(textAncestorListener);
}
Also used : ActionEvent(java.awt.event.ActionEvent) KeyAdapter(java.awt.event.KeyAdapter) MouseMotionListener(java.awt.event.MouseMotionListener) AncestorEvent(javax.swing.event.AncestorEvent) KeyEvent(java.awt.event.KeyEvent) MouseListener(java.awt.event.MouseListener) GateRuntimeException(gate.util.GateRuntimeException) AnnotationDataImpl(gate.gui.annedit.AnnotationDataImpl) AncestorListener(javax.swing.event.AncestorListener) AbstractAction(javax.swing.AbstractAction) ComponentAdapter(java.awt.event.ComponentAdapter) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JComponent(javax.swing.JComponent) InvalidOffsetException(gate.util.InvalidOffsetException) Point(java.awt.Point) Point(java.awt.Point) Annotation(gate.Annotation) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) ActionListener(java.awt.event.ActionListener) Timer(javax.swing.Timer) InputMap(javax.swing.InputMap) ComponentEvent(java.awt.event.ComponentEvent)

Example 60 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationMerging method removeDuplicate.

/**
 * Remove the duplicate annotations from the merged annotations.
 */
private static void removeDuplicate(HashMap<Annotation, String> mergeAnns) {
    // first copy the annotations into a tempory
    HashMap<Annotation, Integer> mergeAnnsNum = new HashMap<Annotation, Integer>();
    for (Annotation ann : mergeAnns.keySet()) {
        String str = mergeAnns.get(ann);
        int num = 1;
        while (str.contains("-")) {
            ++num;
            str = str.substring(str.indexOf('-') + 1);
        }
        mergeAnnsNum.put(ann, new Integer(num));
    }
    // remove the annotaitons having the same places
    for (Annotation ann : mergeAnnsNum.keySet()) {
        Annotation annT = null;
        int num0 = -1;
        Vector<Annotation> sameAnns = new Vector<Annotation>();
        for (Annotation ann1 : mergeAnnsNum.keySet()) {
            if (ann.coextensive(ann1)) {
                sameAnns.add(ann1);
                int num = mergeAnnsNum.get(ann1).intValue();
                if (num > num0) {
                    annT = ann1;
                    num0 = num;
                }
            }
        }
        // end the inner loop for merged annotations
        // Keep the one which most annotators agree on.
        sameAnns.remove(annT);
        // Remove all others
        for (int i = 0; i < sameAnns.size(); ++i) mergeAnns.remove(sameAnns.elementAt(i));
    }
}
Also used : HashMap(java.util.HashMap) Vector(java.util.Vector) Annotation(gate.Annotation)

Aggregations

Annotation (gate.Annotation)69 AnnotationSet (gate.AnnotationSet)28 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)15 Node (gate.Node)10 HashSet (java.util.HashSet)10 List (java.util.List)10 FeatureMap (gate.FeatureMap)8 Map (java.util.Map)8 TreeSet (java.util.TreeSet)8 Document (gate.Document)7 InvalidOffsetException (gate.util.InvalidOffsetException)7 Point (java.awt.Point)6 LinkedList (java.util.LinkedList)5 Set (java.util.Set)5 StatusListener (gate.event.StatusListener)4 GateRuntimeException (gate.util.GateRuntimeException)3 Color (java.awt.Color)3 Stack (java.util.Stack)3 TreeMap (java.util.TreeMap)3