SentenceFactory { public List<OdinSentence> create() { List<Entry<Sentence, List<WordToken>>> entrySet = indexWords.entrySet().stream() .sorted(Comparator.comparing(e -> e.getKey().getBegin())) .collect(toList()); List<OdinSentence> sentences = new ArrayList<>(); for (Entry<Sentence, List<WordToken>> e : entrySet) { sentences.add(create(sentences.size(), e.getKey(), e.getValue())); } return sentences; } SentenceFactory(JCas jCas); protected  SentenceFactory(
      Map<Sentence, List<WordToken>> indexWords,
      Map<WordToken, List<Entity>> indexEntities,
      Map<WordToken, List<PhraseChunk>> indexChunks,
      Map<Sentence, List<Dependency>> indexDependency); List<OdinSentence> create();  }
@Test public void canCreateSentences() throws UIMAException { JCas jCas = JCasFactory.createJCas(); SentenceFactory factory = new SentenceFactory(jCas); List<OdinSentence> sentences = factory.create(); assertNotNull(sentences); }
@Test public void canCreateSentenceFromAnnotations() throws UIMAException { JCas jCas = JCasFactory.createJCas(); addWordTokens(jCas); SentenceFactory factory = new SentenceFactory(jCas); List<OdinSentence> sentences = factory.create(); assertEquals(1, sentences.size()); Sentence sentence = sentences.get(0); assertEquals( ImmutableSet.of("This", "is", "a", "sentence", "."), ImmutableSet.copyOf(sentence.words())); assertTrue(Arrays.equals(new int[] {0, 5, 8, 10, 18}, sentence.startOffsets())); assertTrue(Arrays.equals(new int[] {4, 7, 9, 18, 19}, sentence.endOffsets())); assertEquals( ImmutableSet.of("this", "is", "a", "lemma", "."), ImmutableSet.copyOf(sentence.lemmas().get())); assertEquals( ImmutableSet.of("DT", "VBZ", "DT", "NN", "."), ImmutableSet.copyOf(sentence.tags().get())); }
@Test public void canCreateSentenceWithEntities() throws UIMAException { JCas jCas = JCasFactory.createJCas(); addWordTokens(jCas); Organisation organisation = Annotations.createOrganisation(jCas, 5, 7, "test"); organisation.setIsNormalised(true); Annotations.createPerson(jCas, 10, 18, "sentence"); SentenceFactory factory = new SentenceFactory(jCas); Sentence sentence = factory.create().get(0); assertEquals( ImmutableSet.of( MISSING_VALUE, Organisation.class.getSimpleName(), MISSING_VALUE, Person.class.getSimpleName(), MISSING_VALUE), ImmutableSet.copyOf(sentence.entities().get())); assertEquals( ImmutableSet.of(MISSING_VALUE, "test", MISSING_VALUE, MISSING_VALUE, MISSING_VALUE), ImmutableSet.copyOf(sentence.norms().get())); }
@Test public void canCreateSentenceWithPhraseChunks() throws UIMAException { JCas jCas = JCasFactory.createJCas(); addWordTokens(jCas); String chunk = "CHUNK"; PhraseChunk c1 = new PhraseChunk(jCas); c1.setBegin(0); c1.setEnd(9); c1.setChunkType(chunk); c1.addToIndexes(); SentenceFactory factory = new SentenceFactory(jCas); Sentence sentence = factory.create().get(0); assertEquals( ImmutableSet.of(chunk, chunk, chunk, MISSING_VALUE, MISSING_VALUE, MISSING_VALUE), ImmutableSet.copyOf(sentence.chunks().get())); }
@Test public void canCreateSentenceWithDependencies() throws UIMAException { JCas jCas = JCasFactory.createJCas(); addWordTokens(jCas); Annotations.createDependency(jCas, wt4, wt4, MaltParser.ROOT); Annotations.createDependency(jCas, wt4, wt1, "nsubj"); Annotations.createDependency(jCas, wt4, wt2, "cop"); Annotations.createDependency(jCas, wt4, wt3, "det"); Annotations.createDependency(jCas, wt4, wt5, "punct"); SentenceFactory factory = new SentenceFactory(jCas); Sentence sentence = factory.create().get(0); Option<DirectedGraph<String>> dependencies = sentence.dependencies(); assertTrue(dependencies.isDefined()); GraphMap graphs = sentence.graphs(); assertNotNull(graphs); Option<DirectedGraph<String>> graph = sentence.dependencies(); assertTrue(graph.isDefined()); DirectedGraph<String> directedGraph = graph.get(); assertTrue(directedGraph.hasEdge(3, 3, "root")); assertTrue(directedGraph.hasEdge(3, 0, "nsubj")); assertTrue(directedGraph.hasEdge(3, 1, "cop")); assertTrue(directedGraph.hasEdge(3, 2, "det")); assertTrue(directedGraph.hasEdge(3, 4, "punct")); }
@Test public void canCreateMultipleSentenceWithOffsets() throws UIMAException { JCas jCas = JCasFactory.createJCas(); jCas.setDocumentText("This is the first sentence. This is the second."); Annotations.createWordTokens(jCas); Annotations.createSentences(jCas); SentenceFactory factory = new SentenceFactory(jCas); List<OdinSentence> sentences = factory.create(); Sentence sentence1 = sentences.get(0); Sentence sentence2 = sentences.get(1); assertTrue( Arrays.toString(sentence1.startOffsets()), Arrays.equals(new int[] {0, 5, 8, 12, 18, 26}, sentence1.startOffsets())); assertTrue( Arrays.toString(sentence1.endOffsets()), Arrays.equals(new int[] {4, 7, 11, 17, 26, 27}, sentence1.endOffsets())); assertTrue( Arrays.toString(sentence2.startOffsets()), Arrays.equals(new int[] {28, 33, 36, 40, 46}, sentence2.startOffsets())); assertTrue( Arrays.toString(sentence2.endOffsets()), Arrays.equals(new int[] {32, 35, 39, 46, 47}, sentence2.endOffsets())); }