/** * Append a text annotated with the specified annotation. The created annotation is returned and * further properties can be set on it. The annotation is already added to the indexes. * * @param aText * covered text * @param aClass * annotation type * @param <T> * annotation type * @return annotation instance - can be used to set features or determine offsets */ @SuppressWarnings("unchecked") public <T> T add(String aText, Class<T> aClass) { Type type = getType(jcas, aClass); int begin = documentText.length(); add(aText); int end = documentText.length(); AnnotationFS fs = jcas.getCas().createAnnotation(type, begin, end); jcas.addFsToIndexes(fs); return (T) fs; }
public void tail(Node node, int depth) { if (node.getClass().equals(Element.class)) { Element elm = (Element) node; XMLElement anno = builder.add(beginMap.get(node), XMLElement.class); anno.setTag(elm.tagName()); anno.setId(elm.id()); anno.setSelector(elm.cssSelector()); anno.setAttributes(elm.attributes().html()); if (elm.className().isEmpty()) anno.setCls(elm.attr("type")); else anno.setCls(elm.className()); annotationMap.put(elm.cssSelector(), anno); if (!this.preserveWhitespace) if (elm.isBlock() || ArrayUtils.contains(blockElements, elm.tagName())) builder.add("\n"); } }
@Override public void tail(Node node, int depth) { if (node instanceof Element) { Element elm = (Element) node; XMLElement anno = builder.add(beginMap.get(node), XMLElement.class); anno.setTag(elm.tagName()); anno.setId(elm.id()); anno.setSelector(elm.cssSelector()); anno.setAttributes(elm.attributes().html()); if (elm.className().isEmpty()) anno.setCls(elm.attr("type")); else anno.setCls(elm.className()); annotationMap.put(elm.cssSelector(), anno); if (!this.preserveWhitespace) if (elm.isBlock() || ArrayUtils.contains(blockElements, elm.tagName())) builder.add("\n"); } else if (node instanceof XmlDeclaration) { XmlDeclaration xmlDecl = (XmlDeclaration) node; XmlDeclarationAnnotation anno = builder.add(beginMap.get(node), XmlDeclarationAnnotation.class); anno.setOuterHtml(xmlDecl.outerHtml()); } }
private Collection<String> getLemmas(String text) throws ResourceInitializationException, AnalysisEngineProcessException { AnalysisEngine ae = AnalysisEngineFactory.createEngine( createEngineDescription( createEngineDescription(BreakIteratorSegmenter.class), createEngineDescription(OpenNlpPosTagger.class, OpenNlpPosTagger.PARAM_LANGUAGE, originalLanguage.toString().toLowerCase()), createEngineDescription(GateLemmatizer.class))); JCasBuilder cb = new JCasBuilder(ae.newJCas()); cb.add(text); cb.close(); JCas jcas = cb.getJCas(); ae.process(jcas); // Get the lemmas Collection<Lemma> lemmas = JCasUtil.select(jcas, Lemma.class); // Convert to strings Collection<String> strings = new ArrayList<String>(); for (Lemma lemma : lemmas) { strings.add(lemma.getValue().toLowerCase()); } return strings; }
@SuppressWarnings("unused") private void getCas1(JCasBuilder aBuilder, JCas aJCas) { Token token1 = aBuilder.add("Hallo", Token.class); POS pos1 = new POS(aJCas, token1.getBegin(), token1.getEnd()); pos1.setPosValue("I"); pos1.addToIndexes(); token1.setPos(pos1); Token token2 = aBuilder.add("Welt", Token.class); POS pos2 = new POS(aJCas, token2.getBegin(), token2.getEnd()); pos2.setPosValue("N"); pos2.addToIndexes(); token2.setPos(pos2); Token token3 = aBuilder.add("!", Token.class); POS pos3 = new POS(aJCas, token3.getBegin(), token3.getEnd()); pos3.setPosValue("SENT"); token3.setPos(pos3); pos3.addToIndexes(); }
@SuppressWarnings("unused") private void getCas2(JCasBuilder aBuilder, JCas aJCas) { Token token1 = aBuilder.add("Hallo", Token.class); POS pos1 = new POS(aJCas, token1.getBegin(), token1.getEnd()); pos1.setPosValue("O"); pos1.addToIndexes(); token1.setPos(pos1); Token token2 = aBuilder.add("Welt", Token.class); POS pos2 = new POS(aJCas, token2.getBegin(), token2.getEnd()); pos2.setPosValue("N"); pos2.addToIndexes(); token2.setPos(pos2); Token token3 = aBuilder.add("!", Token.class); POS pos3 = new POS(aJCas, token3.getBegin(), token3.getEnd()); pos3.setPosValue("SENT"); token3.setPos(pos3); pos3.addToIndexes(); } }
@Test public void testIsBeginInSameSentence() throws Exception { JCas jcas = JCasFactory.createJCas(); JCasBuilder jb = new JCasBuilder(jcas); Sentence s1 = jb.add("Sentence 1.", Sentence.class); jb.add(" "); Sentence s2 = jb.add("Sentence 2.", Sentence.class); jb.add(" "); Sentence s3 = jb.add(".", Sentence.class); Sentence s4 = jb.add(".", Sentence.class); jb.close(); assertFalse(isBeginInSameSentence(jcas, s2.getBegin(), s2.getEnd())); assertFalse(isBeginInSameSentence(jcas, s2.getEnd(), s2.getBegin())); assertTrue(isBeginInSameSentence(jcas, s1.getBegin() + 1, s1.getEnd() - 1)); assertTrue(isBeginInSameSentence(jcas, s1.getEnd() - 1, s1.getBegin() + 1)); assertFalse(isBeginInSameSentence(jcas, s1.getBegin(), s1.getEnd())); assertFalse(isBeginInSameSentence(jcas, s1.getEnd(), s1.getBegin())); assertFalse(isBeginInSameSentence(jcas, s2.getBegin(), s1.getBegin())); assertFalse(isBeginInSameSentence(jcas, s1.getBegin(), s2.getBegin())); assertFalse(isBeginInSameSentence(jcas, s3.getBegin(), s4.getBegin())); assertTrue(isBeginInSameSentence(jcas, 0, 0)); }