EventFactory { @SuppressWarnings("unchecked") public Optional<Event> create(Mention mention) { if (mention instanceof EventMention) { EventMention eventMention = (EventMention) mention; Event e = new Event(jCas); e.setBegin(eventMention.startOffset()); e.setEnd(eventMention.endOffset()); e.setValue(eventMention.text()); List<String> labels = JavaConverters.seqAsJavaList(eventMention.labels()); e.setEventType(new StringArray(jCas, labels.size())); for (int i = 0; i < labels.size(); i++) { e.setEventType(i, labels.get(i)); } int startOffset = eventMention.trigger().startOffset(); int endOffset = eventMention.trigger().endOffset(); Optional<WordToken> token = checkFor(WordToken.class, startOffset, endOffset); if (token.isPresent()) { e.setTokens(new FSArray(jCas, 1)); e.setTokens(0, token.get()); } else { e.setTokens(new FSArray(jCas, 0)); } Multimap<String, Entity> arguments = ArrayListMultimap.create(); JavaConverters.mapAsJavaMap(eventMention.arguments()) .forEach( (k, v) -> JavaConverters.seqAsJavaList(v) .forEach( m -> checkFor(Entity.class, m.startOffset(), m.endOffset()) .ifPresent(entity -> arguments.put(k, entity)))); e.setArguments(new StringArray(jCas, arguments.size())); e.setEntities(new FSArray(jCas, arguments.size())); Entry<String, Entity>[] entries = arguments.entries().toArray(new Entry[arguments.size()]); for (int i = 0; i < entries.length; i++) { Entry<String, Entity> entry = entries[i]; e.setArguments(i, entry.getKey()); e.setEntities(i, entry.getValue()); } e.addToIndexes(); return Optional.of(e); } return Optional.empty(); } EventFactory(JCas jCas); @SuppressWarnings("unchecked") Optional<Event> create(Mention mention); void create(Seq<Mention> mentions);  }
@Test public void returnsEmptyWhenNotEvent() throws UIMAException { EventFactory factory = new EventFactory(JCasFactory.createJCas()); assertFalse(factory.create(crossSenteneMention).isPresent()); assertFalse(factory.create(relationMention).isPresent()); assertFalse(factory.create(textMention).isPresent()); }
@Test public void returnsEventWhenEventMention() throws UIMAException { when(document.text()).thenReturn(Option.apply("Baleen was here!")); when(document.sentences()).thenReturn(new Sentence[] {sentence}); when(sentence.startOffsets()).thenReturn(new int[] {0, 7, 11, 15}); when(sentence.endOffsets()).thenReturn(new int[] {6, 10, 15, 16}); JCas jCas = JCasFactory.createJCas(); jCas.setDocumentText("Baleen was here!"); Person person = Annotations.createPerson(jCas, 0, 6, "Baleen"); Location location = Annotations.createLocation(jCas, 11, 15, "here", null); Annotations.createWordTokens(jCas); TextBoundMention textEventMention = new TextBoundMention("was", new Interval(1, 2), 0, document, true, "me"); TextBoundMention textPersonMention = new TextBoundMention("Baleen", new Interval(0, 1), 0, document, true, "me"); TextBoundMention textLocationMention = new TextBoundMention("here", new Interval(2, 3), 0, document, true, "me"); String label = "test"; Map<String, Seq<Mention>> arguments = new HashMap<String, Seq<Mention>>(); arguments.put("person", JavaConversions.asScalaBuffer(ImmutableList.of(textPersonMention))); arguments.put("location", JavaConversions.asScalaBuffer(ImmutableList.of(textLocationMention))); scala.collection.immutable.Map<String, Seq<Mention>> argumentsMap = JavaConverters.mapAsScalaMapConverter(arguments) .asScala() .toMap(Predef.<Tuple2<String, Seq<Mention>>>conforms()); EventMention mention = new EventMention(label, textEventMention, argumentsMap, 0, document, true, "me"); EventFactory factory = new EventFactory(jCas); Optional<Event> create = factory.create(mention); assertTrue(create.isPresent()); Event event = create.get(); assertEquals(0, event.getBegin()); assertEquals(15, event.getEnd()); assertEquals("Baleen was here", event.getValue()); assertEquals(label, event.getEventType().get(0)); FSArray tokens = event.getTokens(); assertEquals(1, tokens.size()); assertEquals("was", ((WordToken) tokens.get(0)).getCoveredText()); StringArray args = event.getArguments(); assertEquals(2, args.size()); assertEquals("person", event.getArguments().get(0)); assertEquals("location", event.getArguments().get(1)); FSArray entities = event.getEntities(); assertEquals(2, entities.size()); assertEquals(person, entities.get(0)); assertEquals(location, entities.get(1)); }
@Test public void returnsEventWhenEventMentionMissingEntities() throws UIMAException { when(document.text()).thenReturn(Option.apply("Baleen was here!")); when(document.sentences()).thenReturn(new Sentence[] {sentence}); when(sentence.startOffsets()).thenReturn(new int[] {0, 7, 11, 15}); when(sentence.endOffsets()).thenReturn(new int[] {6, 10, 15, 16}); JCas jCas = JCasFactory.createJCas(); jCas.setDocumentText("Baleen was here!"); TextBoundMention textEventMention = new TextBoundMention("was", new Interval(1, 2), 0, document, true, "me"); TextBoundMention textPersonMention = new TextBoundMention("Baleen", new Interval(0, 1), 0, document, true, "me"); TextBoundMention textLocationMention = new TextBoundMention("here", new Interval(2, 3), 0, document, true, "me"); String label = "test"; Map<String, Seq<Mention>> arguments = new HashMap<String, Seq<Mention>>(); arguments.put("person", JavaConversions.asScalaBuffer(ImmutableList.of(textPersonMention))); arguments.put("location", JavaConversions.asScalaBuffer(ImmutableList.of(textLocationMention))); scala.collection.immutable.Map<String, Seq<Mention>> argumentsMap = JavaConverters.mapAsScalaMapConverter(arguments) .asScala() .toMap(Predef.<Tuple2<String, Seq<Mention>>>conforms()); EventMention mention = new EventMention(label, textEventMention, argumentsMap, 0, document, true, "me"); EventFactory factory = new EventFactory(jCas); Optional<Event> create = factory.create(mention); assertTrue(create.isPresent()); Event event = create.get(); assertEquals(0, event.getBegin()); assertEquals(15, event.getEnd()); assertEquals("Baleen was here", event.getValue()); assertEquals(label, event.getEventType().get(0)); assertEquals(0, event.getTokens().size()); assertEquals(0, event.getArguments().size()); assertEquals(0, event.getEntities().size()); }