Skip to content

Commit a57d029

Browse files
author
Nils Reiter
committed
fix for #362
Merge branch 'feature/jump-to-mentions-362' into release/2.0.0 * feature/jump-to-mentions-362: Adapted select actions to compare view Removed some commands to the abstract text window Reimplementation of getNextMention() & co using select API of UIMA 3
2 parents fba87c4 + a506c65 commit a57d029

File tree

6 files changed

+104
-47
lines changed

6 files changed

+104
-47
lines changed

src/main/java/de/unistuttgart/ims/coref/annotator/AbstractTextWindow.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.awt.Font;
77
import java.awt.Point;
88
import java.awt.Rectangle;
9+
import java.awt.event.KeyEvent;
910
import java.beans.PropertyChangeListener;
1011
import java.beans.PropertyChangeSupport;
1112
import java.util.Enumeration;
@@ -25,6 +26,7 @@
2526
import javax.swing.JTextPane;
2627
import javax.swing.JTree;
2728
import javax.swing.JViewport;
29+
import javax.swing.KeyStroke;
2830
import javax.swing.SwingUtilities;
2931
import javax.swing.event.ChangeEvent;
3032
import javax.swing.event.ChangeListener;
@@ -52,6 +54,8 @@
5254
import org.kordamp.ikonli.materialdesign.MaterialDesign;
5355
import org.kordamp.ikonli.swing.FontIcon;
5456

57+
import de.unistuttgart.ims.coref.annotator.action.SelectNextMentionAction;
58+
import de.unistuttgart.ims.coref.annotator.action.SelectPreviousMentionAction;
5559
import de.unistuttgart.ims.coref.annotator.action.ViewFontFamilySelectAction;
5660
import de.unistuttgart.ims.coref.annotator.action.ViewFontSizeDecreaseAction;
5761
import de.unistuttgart.ims.coref.annotator.action.ViewFontSizeIncreaseAction;
@@ -176,6 +180,17 @@ public JCas getJCas() {
176180
return getDocumentModel().getJcas();
177181
}
178182

183+
public void annotationSelected(Annotation m) {
184+
if (m != null) {
185+
textPane.setSelectionStart(m.getBegin());
186+
textPane.setSelectionEnd(m.getEnd());
187+
// textPane.setCaretPosition(m.getEnd());
188+
textPane.getCaret().setSelectionVisible(true);
189+
} else {
190+
textPane.getCaret().setSelectionVisible(false);
191+
}
192+
}
193+
179194
@Override
180195
public void entityEvent(FeatureStructureEvent event) {
181196
Event.Type eventType = event.getType();
@@ -510,6 +525,12 @@ protected void initializeWindow() {
510525
textPane = new JTextPane();
511526
textPane.setDragEnabled(true);
512527
textPane.setEditable(false);
528+
textPane.getActionMap().put(SelectNextMentionAction.class, new SelectNextMentionAction(this));
529+
textPane.getActionMap().put(SelectPreviousMentionAction.class, new SelectPreviousMentionAction(this));
530+
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK),
531+
SelectNextMentionAction.class);
532+
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK),
533+
SelectPreviousMentionAction.class);
513534

514535
getMiscLabel().setText("Style: " + Annotator.app.getPluginManager().getDefaultStylePlugin().getName());
515536
getMiscLabel().setToolTipText(Annotator.app.getPluginManager().getDefaultStylePlugin().getDescription());

src/main/java/de/unistuttgart/ims/coref/annotator/CompareMentionsWindow.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.beans.PropertyChangeEvent;
1414
import java.beans.PropertyChangeListener;
1515
import java.io.File;
16+
import java.util.NoSuchElementException;
1617
import java.util.Set;
1718
import java.util.function.Consumer;
1819
import java.util.prefs.PreferenceChangeEvent;
@@ -832,6 +833,7 @@ public static class IntersectModel {
832833
ImmutableList<DocumentModel> documentModels;
833834
MutableSet<Spans> spanIntersection = null;
834835
MutableMap<DocumentModel, MutableMap<Spans, Mention>> spanMentionMap = Maps.mutable.empty();
836+
MutableSetMultimap<Span, Mention> spanAllMentionsMap = Multimaps.mutable.set.empty();
835837
Span annotatedRange = new Span(Integer.MAX_VALUE, Integer.MIN_VALUE);
836838
Span overlappingPart = new Span(Integer.MIN_VALUE, Integer.MAX_VALUE);
837839

@@ -852,6 +854,8 @@ public void calculateIntersection() {
852854
span = new ExtendedSpan(m);
853855
else
854856
span = new Spans(m);
857+
for (Span sp : span)
858+
spanAllMentionsMap.put(sp, m);
855859

856860
spanMentionMap.get(dm).put(span, m);
857861
spans.add(span);
@@ -903,4 +907,24 @@ public NotComparableException(String string) {
903907

904908
}
905909

910+
public MentionSurface getNextMentionSurface(int position) {
911+
try {
912+
return documentModels.collect(dm -> dm.getCoreferenceModel())
913+
.collect(cm -> cm.getNextMentionSurface(position)).reject(ms -> ms == null)
914+
.minBy(m -> m.getBegin());
915+
} catch (NoSuchElementException e) {
916+
return null;
917+
}
918+
}
919+
920+
public MentionSurface getPreviousMentionSurface(int position) {
921+
try {
922+
return documentModels.collect(dm -> dm.getCoreferenceModel())
923+
.collect(cm -> cm.getPreviousMentionSurface(position)).reject(ms -> ms == null)
924+
.maxBy(m -> m.getEnd());
925+
} catch (NoSuchElementException e) {
926+
return null;
927+
}
928+
}
929+
906930
}

src/main/java/de/unistuttgart/ims/coref/annotator/DocumentWindow.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
import org.apache.uima.fit.util.JCasUtil;
8989
import org.apache.uima.jcas.JCas;
9090
import org.apache.uima.jcas.cas.TOP;
91-
import org.apache.uima.jcas.tcas.Annotation;
9291
import org.eclipse.collections.api.list.ImmutableList;
9392
import org.eclipse.collections.api.list.MutableList;
9493
import org.eclipse.collections.api.set.ImmutableSet;
@@ -124,8 +123,6 @@
124123
import de.unistuttgart.ims.coref.annotator.action.RemoveSingletons;
125124
import de.unistuttgart.ims.coref.annotator.action.RenameAllEntitiesAction;
126125
import de.unistuttgart.ims.coref.annotator.action.RenameEntityAction;
127-
import de.unistuttgart.ims.coref.annotator.action.SelectNextMentionAction;
128-
import de.unistuttgart.ims.coref.annotator.action.SelectPreviousMentionAction;
129126
import de.unistuttgart.ims.coref.annotator.action.SetLanguageAction;
130127
import de.unistuttgart.ims.coref.annotator.action.ShowASelectedMentionInTreeAction;
131128
import de.unistuttgart.ims.coref.annotator.action.ShowDocumentStatistics;
@@ -354,19 +351,13 @@ protected void initializeWindow() {
354351
textPane.getActionMap().put(DeleteAction.class, actions.deleteAction);
355352
textPane.getActionMap().put(CopyAction.class, new CopyAction(this));
356353
textPane.getActionMap().put(DeleteAllMentionsInSelection.class, actions.deleteAllAction);
357-
textPane.getActionMap().put(SelectNextMentionAction.class, new SelectNextMentionAction(this));
358-
textPane.getActionMap().put(SelectPreviousMentionAction.class, new SelectPreviousMentionAction(this));
359354
textPane.getInputMap().put(
360355
KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()),
361356
CopyAction.class);
362357
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), DeleteAction.class);
363358
textPane.getInputMap().put(
364359
KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()),
365360
DeleteAllMentionsInSelection.class);
366-
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, KeyEvent.ALT_DOWN_MASK),
367-
SelectNextMentionAction.class);
368-
textPane.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, KeyEvent.ALT_DOWN_MASK),
369-
SelectPreviousMentionAction.class);
370361

371362
textPane.addCaretListener(actions.showASelectedMentionInTree);
372363

@@ -984,17 +975,6 @@ public void run() {
984975

985976
}
986977

987-
public void annotationSelected(Annotation m) {
988-
if (m != null) {
989-
textPane.setSelectionStart(m.getBegin());
990-
textPane.setSelectionEnd(m.getEnd());
991-
// textPane.setCaretPosition(m.getEnd());
992-
textPane.getCaret().setSelectionVisible(true);
993-
} else {
994-
textPane.getCaret().setSelectionVisible(false);
995-
}
996-
}
997-
998978
class MyTreeTransferHandler extends TransferHandler {
999979

1000980
CATreeNode targetNode;

src/main/java/de/unistuttgart/ims/coref/annotator/action/SelectNextMentionAction.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44

55
import org.kordamp.ikonli.materialdesign.MaterialDesign;
66

7+
import de.unistuttgart.ims.coref.annotator.AbstractTextWindow;
8+
import de.unistuttgart.ims.coref.annotator.CompareMentionsWindow;
79
import de.unistuttgart.ims.coref.annotator.DocumentWindow;
8-
import de.unistuttgart.ims.coref.annotator.api.v2.Mention;
10+
import de.unistuttgart.ims.coref.annotator.api.v2.MentionSurface;
911

10-
public class SelectNextMentionAction extends TargetedIkonAction<DocumentWindow> {
12+
public class SelectNextMentionAction extends TargetedIkonAction<AbstractTextWindow> {
1113
private static final long serialVersionUID = 1L;
1214

13-
public SelectNextMentionAction(DocumentWindow dw) {
15+
public SelectNextMentionAction(AbstractTextWindow dw) {
1416
super(dw, MaterialDesign.MDI_ARROW_RIGHT);
1517
}
1618

1719
@Override
1820
public void actionPerformed(ActionEvent e) {
19-
int high = getTarget().getTextPane().getSelectionEnd();
20-
21-
Mention nextMention = getTarget().getDocumentModel().getCoreferenceModel().getNextMention(high);
22-
21+
int high = getTarget().getTextPane().getSelectionStart() + 1;
22+
23+
MentionSurface nextMention = null;
24+
if (getTarget() instanceof DocumentWindow) {
25+
nextMention = getTarget().getDocumentModel().getCoreferenceModel().getNextMentionSurface(high);
26+
} else if (getTarget() instanceof CompareMentionsWindow) {
27+
nextMention = ((CompareMentionsWindow) getTarget()).getNextMentionSurface(high);
28+
}
2329
if (nextMention != null)
24-
getTarget().annotationSelected(nextMention.getSurface(0));
25-
30+
getTarget().annotationSelected(nextMention);
2631
}
2732

2833
}

src/main/java/de/unistuttgart/ims/coref/annotator/action/SelectPreviousMentionAction.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44

55
import org.kordamp.ikonli.materialdesign.MaterialDesign;
66

7+
import de.unistuttgart.ims.coref.annotator.AbstractTextWindow;
8+
import de.unistuttgart.ims.coref.annotator.CompareMentionsWindow;
79
import de.unistuttgart.ims.coref.annotator.DocumentWindow;
8-
import de.unistuttgart.ims.coref.annotator.api.v2.Mention;
10+
import de.unistuttgart.ims.coref.annotator.api.v2.MentionSurface;
911

10-
public class SelectPreviousMentionAction extends TargetedIkonAction<DocumentWindow> {
12+
public class SelectPreviousMentionAction extends TargetedIkonAction<AbstractTextWindow> {
1113

12-
public SelectPreviousMentionAction(DocumentWindow dw) {
14+
public SelectPreviousMentionAction(AbstractTextWindow dw) {
1315
super(dw, MaterialDesign.MDI_ARROW_LEFT);
1416
}
1517

1618
private static final long serialVersionUID = 1L;
1719

1820
@Override
1921
public void actionPerformed(ActionEvent e) {
20-
int low = getTarget().getTextPane().getSelectionStart();
21-
Mention nextMention = getTarget().getDocumentModel().getCoreferenceModel().getPreviousMention(low);
22+
int low = getTarget().getTextPane().getSelectionEnd() - 1;
23+
MentionSurface nextMention = null;
24+
if (getTarget() instanceof DocumentWindow) {
25+
nextMention = getTarget().getDocumentModel().getCoreferenceModel().getPreviousMentionSurface(low);
26+
} else if (getTarget() instanceof CompareMentionsWindow) {
27+
nextMention = ((CompareMentionsWindow) getTarget()).getPreviousMentionSurface(low);
28+
}
2229

2330
if (nextMention != null)
24-
getTarget().annotationSelected(nextMention.getSurface(0));
31+
getTarget().annotationSelected(nextMention);
2532

2633
}
2734

src/main/java/de/unistuttgart/ims/coref/annotator/document/CoreferenceModel.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,24 +608,44 @@ public ImmutableSortedSet<Mention> getMentions(Entity entity) {
608608
return entityMentionMap.get(entity).toImmutable();
609609
}
610610

611+
@SuppressWarnings("resource")
612+
public MentionSurface getNextMentionSurface(int position) {
613+
try {
614+
return getJCas().select(MentionSurface.class).following(position).get();
615+
} catch (Exception e) {
616+
Annotator.logger.catching(e);
617+
return null;
618+
}
619+
}
620+
621+
@SuppressWarnings("resource")
611622
public Mention getNextMention(int position) {
612-
for (int i = position; i < getDocumentModel().getJcas().getDocumentText().length(); i++) {
613-
MutableSet<Mention> mentions = characterPosition2AnnotationMap.get(i)
614-
.selectInstancesOf(MentionSurface.class).collect(ms -> ms.getMention());
615-
if (!mentions.isEmpty())
616-
return mentions.iterator().next();
623+
try {
624+
return getJCas().select(MentionSurface.class).following(position).get().getMention();
625+
} catch (Exception e) {
626+
Annotator.logger.catching(e);
627+
return null;
617628
}
618-
return null;
619629
}
620630

631+
@SuppressWarnings("resource")
632+
public MentionSurface getPreviousMentionSurface(int position) {
633+
try {
634+
return getJCas().select(MentionSurface.class).preceding(position).backwards().get();
635+
} catch (Exception e) {
636+
Annotator.logger.catching(e);
637+
return null;
638+
}
639+
}
640+
641+
@SuppressWarnings("resource")
621642
public Mention getPreviousMention(int position) {
622-
for (int i = position - 1; i >= 0; i--) {
623-
MutableSet<Mention> mentions = characterPosition2AnnotationMap.get(i)
624-
.selectInstancesOf(MentionSurface.class).collect(ms -> ms.getMention());
625-
if (!mentions.isEmpty())
626-
return mentions.iterator().next();
643+
try {
644+
return getJCas().select(MentionSurface.class).preceding(position).get().getMention();
645+
} catch (Exception e) {
646+
Annotator.logger.catching(e);
647+
return null;
627648
}
628-
return null;
629649
}
630650

631651
/**

0 commit comments

Comments
 (0)