@Test public void testAnnotatorWithAllEmbeddings() throws Exception { final JCas jCas = JCasFactory.createJCas("de.julielab.jcore.types.jcore-semantics-biology-types"); final AnalysisEngine engine = AnalysisEngineFactory.createEngine(FlairNerAnnotator.class, FlairNerAnnotator.PARAM_STORE_EMBEDDINGS, FlairNerAnnotator.StoreEmbeddings.ALL, FlairNerAnnotator.PARAM_ANNOTATION_TYPE, Gene.class.getCanonicalName(), FlairNerAnnotator.PARAM_FLAIR_MODEL, "src/test/resources/genes-small-model.pt"); String text = "Knockdown of SUB1 homolog by siRNA inhibits the early stages of HIV-1 replication in 293T cells infected with VSV-G pseudotyped HIV-1 ."; jCas.setDocumentText(text); Sentence s = new Sentence(jCas, 0, text.length()); addTokens(jCas); s.addToIndexes(); engine.process(jCas); for (Token token : jCas.<Token>getAnnotationIndex(Token.type)) { assertThat(token.getEmbeddingVectors()).isNotNull(); assertThat(token.getEmbeddingVectors()).hasSize(1); final EmbeddingVector embedding = (EmbeddingVector) token.getEmbeddingVectors().get(0); assertThat(embedding.getSource()).isEqualTo("src/test/resources/genes-small-model.pt"); assertThat(embedding.getVector()).hasSize(1024); } }
@Override public void process(final JCas aJCas) throws AnalysisEngineProcessException { int i = 0; final AnnotationIndex<Sentence> sentIndex = aJCas.getAnnotationIndex(Sentence.class); Map<String, Sentence> sentenceMap = new HashMap<>(); for (Sentence sentence : sentIndex) { if (sentence.getId() == null) sentence.setId("s" + i++); sentenceMap.put(sentence.getId(), sentence); } try { final AnnotationAdderHelper helper = new AnnotationAdderHelper(); final NerTaggingResponse taggingResponse = connector.tagSentences(StreamSupport.stream(sentIndex.spliterator(), false)); final List<TaggedEntity> taggedEntities = taggingResponse.getTaggedEntities(); for (TaggedEntity entity : taggedEntities) { final Sentence sentence = sentenceMap.get(entity.getDocumentId()); EntityMention em = (EntityMention) JCoReAnnotationTools.getAnnotationByClassName(aJCas, entityClass); helper.setAnnotationOffsetsRelativeToSentence(sentence, em, entity, adderConfig); em.setSpecificType(entity.getTag()); em.setConfidence(String.valueOf(entity.getLabelConfidence())); em.setComponentId(componentId); em.addToIndexes(); } addTokenEmbeddings(aJCas, sentenceMap, helper, taggingResponse); } catch (IOException e) { log.error("Could not tag entities", e); throw new AnalysisEngineProcessException(e); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { log.error("Could not create an instance of the entity class {}", entityClass); throw new AnalysisEngineProcessException(e); } catch (CASException e) { log.error("Could not set the entity offsets", e); throw new AnalysisEngineProcessException(e); } catch (AnnotationOffsetException e) { final String docId = JCoReTools.getDocId(aJCas); log.error("Could not set the offsets of an annotation in document {}", docId); throw new AnalysisEngineProcessException(e); } }
FlairNerAnnotator extends JCasAnnotator_ImplBase { @Override public void process(final JCas aJCas) throws AnalysisEngineProcessException { int i = 0; final AnnotationIndex<Sentence> sentIndex = aJCas.getAnnotationIndex(Sentence.class); Map<String, Sentence> sentenceMap = new HashMap<>(); for (Sentence sentence : sentIndex) { if (sentence.getId() == null) sentence.setId("s" + i++); sentenceMap.put(sentence.getId(), sentence); } try { final AnnotationAdderHelper helper = new AnnotationAdderHelper(); final NerTaggingResponse taggingResponse = connector.tagSentences(StreamSupport.stream(sentIndex.spliterator(), false)); final List<TaggedEntity> taggedEntities = taggingResponse.getTaggedEntities(); for (TaggedEntity entity : taggedEntities) { final Sentence sentence = sentenceMap.get(entity.getDocumentId()); EntityMention em = (EntityMention) JCoReAnnotationTools.getAnnotationByClassName(aJCas, entityClass); helper.setAnnotationOffsetsRelativeToSentence(sentence, em, entity, adderConfig); em.setSpecificType(entity.getTag()); em.setConfidence(String.valueOf(entity.getLabelConfidence())); em.setComponentId(componentId); em.addToIndexes(); } addTokenEmbeddings(aJCas, sentenceMap, helper, taggingResponse); } catch (IOException e) { log.error("Could not tag entities", e); throw new AnalysisEngineProcessException(e); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { log.error("Could not create an instance of the entity class {}", entityClass); throw new AnalysisEngineProcessException(e); } catch (CASException e) { log.error("Could not set the entity offsets", e); throw new AnalysisEngineProcessException(e); } catch (AnnotationOffsetException e) { final String docId = JCoReTools.getDocId(aJCas); log.error("Could not set the offsets of an annotation in document {}", docId); throw new AnalysisEngineProcessException(e); } } }
FlairNerAnnotator extends JCasAnnotator_ImplBase { @Override public void process(final JCas aJCas) throws AnalysisEngineProcessException { int i = 0; final AnnotationIndex<Sentence> sentIndex = aJCas.getAnnotationIndex(Sentence.class); Map<String, Sentence> sentenceMap = new HashMap<>(); for (Sentence sentence : sentIndex) { if (sentence.getId() == null) sentence.setId("s" + i++); sentenceMap.put(sentence.getId(), sentence); } try { final AnnotationAdderHelper helper = new AnnotationAdderHelper(); final NerTaggingResponse taggingResponse = connector.tagSentences(StreamSupport.stream(sentIndex.spliterator(), false)); final List<TaggedEntity> taggedEntities = taggingResponse.getTaggedEntities(); for (TaggedEntity entity : taggedEntities) { final Sentence sentence = sentenceMap.get(entity.getDocumentId()); EntityMention em = (EntityMention) JCoReAnnotationTools.getAnnotationByClassName(aJCas, entityClass); helper.setAnnotationOffsetsRelativeToSentence(sentence, em, entity, adderConfig); em.setSpecificType(entity.getTag()); em.setConfidence(String.valueOf(entity.getLabelConfidence())); em.setComponentId(componentId); em.addToIndexes(); } addTokenEmbeddings(aJCas, sentenceMap, helper, taggingResponse); } catch (IOException e) { log.error("Could not tag entities", e); throw new AnalysisEngineProcessException(e); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { log.error("Could not create an instance of the entity class {}", entityClass); throw new AnalysisEngineProcessException(e); } catch (CASException e) { log.error("Could not set the entity offsets", e); throw new AnalysisEngineProcessException(e); } catch (AnnotationOffsetException e) { final String docId = JCoReTools.getDocId(aJCas); log.error("Could not set the offsets of an annotation in document {}", docId); throw new AnalysisEngineProcessException(e); } }  }
FlairNerAnnotator extends JCasAnnotator_ImplBase { @Override public void process(final JCas aJCas) throws AnalysisEngineProcessException { int i = 0; final AnnotationIndex<Sentence> sentIndex = aJCas.getAnnotationIndex(Sentence.class); Map<String, Sentence> sentenceMap = new HashMap<>(); for (Sentence sentence : sentIndex) { if (sentence.getId() == null) sentence.setId("s" + i++); sentenceMap.put(sentence.getId(), sentence); } try { final AnnotationAdderHelper helper = new AnnotationAdderHelper(); final NerTaggingResponse taggingResponse = connector.tagSentences(StreamSupport.stream(sentIndex.spliterator(), false)); final List<TaggedEntity> taggedEntities = taggingResponse.getTaggedEntities(); for (TaggedEntity entity : taggedEntities) { final Sentence sentence = sentenceMap.get(entity.getDocumentId()); EntityMention em = (EntityMention) JCoReAnnotationTools.getAnnotationByClassName(aJCas, entityClass); helper.setAnnotationOffsetsRelativeToSentence(sentence, em, entity, adderConfig); em.setSpecificType(entity.getTag()); em.setConfidence(String.valueOf(entity.getLabelConfidence())); em.setComponentId(componentId); em.addToIndexes(); } addTokenEmbeddings(aJCas, sentenceMap, helper, taggingResponse); } catch (IOException e) { log.error("Could not tag entities", e); throw new AnalysisEngineProcessException(e); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { log.error("Could not create an instance of the entity class {}", entityClass); throw new AnalysisEngineProcessException(e); } catch (CASException e) { log.error("Could not set the entity offsets", e); throw new AnalysisEngineProcessException(e); } catch (AnnotationOffsetException e) { final String docId = JCoReTools.getDocId(aJCas); log.error("Could not set the offsets of an annotation in document {}", docId); throw new AnalysisEngineProcessException(e); } }  @Override void initialize(final UimaContext aContext); @Override void process(final JCas aJCas); @Override void collectionProcessComplete(); }
FlairNerAnnotator extends JCasAnnotator_ImplBase { @Override public void process(final JCas aJCas) throws AnalysisEngineProcessException { int i = 0; final AnnotationIndex<Sentence> sentIndex = aJCas.getAnnotationIndex(Sentence.class); Map<String, Sentence> sentenceMap = new HashMap<>(); for (Sentence sentence : sentIndex) { if (sentence.getId() == null) sentence.setId("s" + i++); sentenceMap.put(sentence.getId(), sentence); } try { final AnnotationAdderHelper helper = new AnnotationAdderHelper(); final NerTaggingResponse taggingResponse = connector.tagSentences(StreamSupport.stream(sentIndex.spliterator(), false)); final List<TaggedEntity> taggedEntities = taggingResponse.getTaggedEntities(); for (TaggedEntity entity : taggedEntities) { final Sentence sentence = sentenceMap.get(entity.getDocumentId()); EntityMention em = (EntityMention) JCoReAnnotationTools.getAnnotationByClassName(aJCas, entityClass); helper.setAnnotationOffsetsRelativeToSentence(sentence, em, entity, adderConfig); em.setSpecificType(entity.getTag()); em.setConfidence(String.valueOf(entity.getLabelConfidence())); em.setComponentId(componentId); em.addToIndexes(); } addTokenEmbeddings(aJCas, sentenceMap, helper, taggingResponse); } catch (IOException e) { log.error("Could not tag entities", e); throw new AnalysisEngineProcessException(e); } catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException | ClassNotFoundException e) { log.error("Could not create an instance of the entity class {}", entityClass); throw new AnalysisEngineProcessException(e); } catch (CASException e) { log.error("Could not set the entity offsets", e); throw new AnalysisEngineProcessException(e); } catch (AnnotationOffsetException e) { final String docId = JCoReTools.getDocId(aJCas); log.error("Could not set the offsets of an annotation in document {}", docId); throw new AnalysisEngineProcessException(e); } }  @Override void initialize(final UimaContext aContext); @Override void process(final JCas aJCas); @Override void collectionProcessComplete(); static final String PARAM_ANNOTATION_TYPE; static final String PARAM_FLAIR_MODEL; static final String PARAM_PYTHON_EXECUTABLE; static final String PARAM_STORE_EMBEDDINGS; static final String PARAM_GPU_NUM; static final String PARAM_COMPONENT_ID; static final String GPU_NUM_SYS_PROP; }