de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity Java Examples

The following examples show how to use de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CasMergeTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleCopyToDiffExistingAnnoWithStackingTest()
    throws Exception
{
    neLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);

    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(NamedEntity.class.getTypeName());
    AnnotationFS clickedFs = createNEAnno(jcas, "NN", 0, 0);

    CAS mergeCAs = createJCas().getCas();
    createTokenAnno(mergeCAs, 0, 0);
    AnnotationFS existingFs = mergeCAs.createAnnotation(type, 0, 0);
    Feature posValue = type.getFeatureByBaseName("value");
    existingFs.setStringValue(posValue, "NE");
    mergeCAs.addFsToIndexes(existingFs);

    sut.mergeSpanAnnotation(null, null, neLayer, mergeCAs, clickedFs, true);

    assertEquals(2, selectCovered(mergeCAs, type, 0, 0).size());
}
 
Example #2
Source File: FactLayerInitializer.java    From inception with Apache License 2.0 6 votes vote down vote up
private AnnotationFeature createLinkedFeature(String featureName, String featureUiName,
    String description, String linkedTypeName, AnnotationLayer aAnnotationLayer,
    Project aProject)
{
    AnnotationFeature linkedFeature = new AnnotationFeature();
    linkedFeature.setName(featureName);
    linkedFeature.setUiName(featureUiName);
    linkedFeature.setDescription(description);
    linkedFeature.setType(NamedEntity.class.getName());
    linkedFeature.setProject(aProject);
    linkedFeature.setTagset(null);
    linkedFeature.setMode(MultiValueMode.ARRAY);
    linkedFeature.setLinkMode(LinkMode.WITH_ROLE);
    linkedFeature.setLinkTypeName(linkedTypeName);
    linkedFeature.setLinkTypeRoleFeatureName("role");
    linkedFeature.setLinkTypeTargetFeatureName("target");
    linkedFeature.setLayer(aAnnotationLayer);
    return linkedFeature;
}
 
Example #3
Source File: DataMajorityNerRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
private CAS getTestCasNoLabelLabels() throws Exception
{
    try {
        Dataset ds = loader.load("germeval2014-de", CONTINUE);
        CAS cas = loadData(ds, ds.getDataFiles()[0]).get(0);
        Type neType = getAnnotationType(cas, NamedEntity.class);
        Feature valFeature = neType.getFeatureByBaseName("value");
        select(cas.getJCas(), NamedEntity.class)
                .forEach(ne -> ne.setFeatureValueFromString(valFeature, null));

        return cas;
    }
    catch (Exception e) {
        // Workaround for https://github.com/dkpro/dkpro-core/issues/1469
        assumeThat(e).isNotInstanceOf(FileNotFoundException.class);
        throw e;
    }

}
 
Example #4
Source File: FilterEntitiesByType.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override public void process(JCas jCas) throws AnalysisEngineProcessException {
  Collection<AidaEntity> entities = JCasUtil.select(jCas, AidaEntity.class);
  for(AidaEntity entity: entities) {
    String id = entity.getID().substring(entity.getID().indexOf(":")+1);
    try {
      int intkb = DataAccess.getInternalIdForKBEntity(KBIdentifiedEntity.getKBIdentifiedEntity(id, "YAGO3"));
      Set<Type> types = DataAccess.getTypes(new Entity(id, "YAGO3", intkb));
      if(!types.isEmpty()) {
        boolean typeFound = findTypes(typesToFilterKG, typesToFilterNER, types);
        if(!typeFound) {
          entity.removeFromIndexes();
        }
      } else {
        Collection<NamedEntity> nes = JCasUtil.selectCovered(jCas, NamedEntity.class, entity);
        for(NamedEntity ne: nes) {
          if(!typesToFilterNER.contains(ne.getValue().toLowerCase())) {
            entity.removeFromIndexes();
            break;
          }
        }
      }
    } catch (EntityLinkingDataAccessException e) {
      throw new AnalysisEngineProcessException(e);
    }
  }
}
 
Example #5
Source File: Tcf2DKPro.java    From inception with Apache License 2.0 6 votes vote down vote up
public void convertNamedEntities(JCas aJCas, TextCorpus aCorpusData,
        Map<String, Token> aTokens)
{
    if (aCorpusData.getNamedEntitiesLayer() == null) {
        // No layer to read from.
        return;
    }

    for (int i = 0; i < aCorpusData.getNamedEntitiesLayer().size(); i++) {
        // get the named entity
        eu.clarin.weblicht.wlfxb.tc.api.NamedEntity entity = aCorpusData
                .getNamedEntitiesLayer().getEntity(i);

        eu.clarin.weblicht.wlfxb.tc.api.Token[] namedEntityTokens = aCorpusData
                .getNamedEntitiesLayer().getTokens(entity);

        NamedEntity outNamedEntity = new NamedEntity(aJCas);

        outNamedEntity.setBegin(getOffsets(namedEntityTokens, aTokens)[0]);
        outNamedEntity.setEnd(getOffsets(namedEntityTokens, aTokens)[1]);
        outNamedEntity.setValue(entity.getType());
        outNamedEntity.addToIndexes();
    }

}
 
Example #6
Source File: PredictionsWriter.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
private <T extends Annotation> String getTag(Class<T> clazz, JCas jCas, Token token) {
        List<T> annotationList = JCasUtil.selectCovering(jCas, clazz, token);

        String value = annotationList.isEmpty()? "OTH" : clazz == NamedEntity.class?
                ((NamedEntity)annotationList.get(0)).getValue():
                ((TextClassificationOutcome)annotationList.get(0)).getOutcome();

        if (!"OTH".equals(value)) {

//                        spanish uses different signs for class labels...
            if ("es".equals(language)) {
                value = value.replace("LUG", "LOC")
                        .replace("OTROS", "MISC")
                        .replace("PERS", "PER");
            }
            value = value.replace("ORGANIZATION", "ORG")
                    .replace("LOCATION", "LOC")
                    .replace("PERSON", "PER");
//                        todo add chunk tags if they are not predicted rethink about spanish!
//                        if (!value.contains("-")) {
//
//                            value = ("OTH".equals(prevNE) || !prevNE.contains(value) ? "B-" : "I-") + value;
//                        }
        }
        return value;
    }
 
Example #7
Source File: SingleLabelAnnotator.java    From ambiverse-nlu with Apache License 2.0 6 votes vote down vote up
@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {

	Collection<NamedEntity> namedEntities = JCasUtil.select(aJCas, NamedEntity.class);
	for (NamedEntity entity : namedEntities) {
		entity.setValue(entity.getValue().replace("PERSON", "ENTITY")
				.replace("PER", "ENTITY")
				.replace("LOCATION", "ENTITY")
				.replace("LOC", "ENTITY")
				.replace("ORGANIZATION", "ENTITY")
				.replace("ORG", "ENTITY")
				.replace("MISC", "ENTITY"));
	}
	Collection<TextClassificationOutcome> outcomes = JCasUtil.select(aJCas, TextClassificationOutcome.class);
	for (TextClassificationOutcome outcome : outcomes) {
		if (outcome.getOutcome().equals("OTH")) {
			continue;
		}
		outcome.setOutcome("ENTITY");
	}
}
 
Example #8
Source File: WeblichtRecommenderFactoryImpl.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accepts(AnnotationLayer aLayer, AnnotationFeature aFeature)
{
    if (Lemma.class.getName().equals(aLayer.getName())) {
        if ("value".equals(aFeature.getName())) {
            return true;
        }
    }

    if (POS.class.getName().equals(aLayer.getName())) {
        if ("PosValue".equals(aFeature.getName())) {
            return true;
        }
        if ("coarseValue".equals(aFeature.getName())) {
            return true;
        }
    }

    if (NamedEntity.class.getName().equals(aLayer.getName())) {
        if ("value".equals(aFeature.getName())) {
            return true;
        }
    }
    
    return false;
}
 
Example #9
Source File: StringMatchingRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
public void thatPredictionForNoLabelAnnosWorks() throws Exception
{
    StringMatchingRecommender sut = new StringMatchingRecommender(recommender, traits);
    CAS cas = getTestCasNoLabelLabels();
    RecommenderTestHelper.addScoreFeature(cas, NamedEntity.class, "value");

    sut.train(context, asList(cas));

    sut.predict(context, cas);

    List<NamedEntity> predictions = getPredictions(cas, NamedEntity.class);

    assertThat(predictions).as("Has all null labels").extracting(NamedEntity::getValue)
            .containsOnlyNulls();
}
 
Example #10
Source File: StringMatchingRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
private CAS getTestCasNoLabelLabels() throws Exception
{
    try {
        Dataset ds = loader.load("germeval2014-de", CONTINUE);
        CAS cas = loadData(ds, ds.getDataFiles()[0]).get(0);
        Type neType = CasUtil.getAnnotationType(cas, NamedEntity.class);
        Feature valFeature = neType.getFeatureByBaseName("value");
        select(cas.getJCas(), NamedEntity.class)
                .forEach(ne -> ne.setFeatureValueFromString(valFeature, null));

        return cas;
    }
    catch (Exception e) {
        // Workaround for https://github.com/dkpro/dkpro-core/issues/1469
        assumeThat(e).isNotInstanceOf(FileNotFoundException.class);
        throw e;
    }
}
 
Example #11
Source File: OpenNlpDoccatRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
public void thatPredictionWorks() throws Exception
{
    OpenNlpDoccatRecommender sut = new OpenNlpDoccatRecommender(recommender, traits);
    List<CAS> casList = loadArxivData();
    
    CAS cas = casList.get(0);
    try (CasStorageSession session = CasStorageSession.open()) {
        session.add("testCas", EXCLUSIVE_WRITE_ACCESS, cas);
        RecommenderTestHelper.addScoreFeature(cas, NamedEntity.class, "value");
    }

    sut.train(context, asList(cas));

    sut.predict(context, cas);

    List<NamedEntity> predictions = getPredictions(cas, NamedEntity.class);

    assertThat(predictions).as("Predictions have been written to CAS")
        .isNotEmpty();
}
 
Example #12
Source File: VisibilityCalculationTests.java    From inception with Apache License 2.0 6 votes vote down vote up
private CAS getTestCas() throws Exception
{
    String documentText = "Dies ist ein Testtext, ach ist der schoen, der schoenste von allen"
            + " Testtexten.";
    JCas jcas = JCasFactory.createText(documentText, "de");

    NamedEntity neLabel = new NamedEntity(jcas, 0, 3);
    neLabel.setValue("LOC");
    neLabel.addToIndexes();

    // the annotation's feature value is initialized as null
    NamedEntity neNoLabel = new NamedEntity(jcas, 13, 20);
    neNoLabel.addToIndexes();

    return jcas.getCas();
}
 
Example #13
Source File: ExternalRecommenderIntegrationTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
public void thatPredictingSendsCorrectRequest() throws Exception
{
    List<CAS> casses = loadDevelopmentData();
    sut.train(context, casses);
    CAS cas = casses.get(0);
    RecommenderTestHelper.addScoreFeature(cas, NamedEntity.class, "value");
    sut.predict(context, cas);

    PredictionRequest request = fromJsonString(PredictionRequest.class, requestBodies.get(1));

    assertThat(request.getMetadata()).hasNoNullFieldsOrProperties()
        .hasFieldOrPropertyWithValue("projectId", PROJECT_ID)
        .hasFieldOrPropertyWithValue("layer", recommender.getLayer().getName())
        .hasFieldOrPropertyWithValue("feature", recommender.getFeature().getName())
        .hasFieldOrPropertyWithValue("crossSentence", CROSS_SENTENCE)
        .hasFieldOrPropertyWithValue("anchoringMode", ANCHORING_MODE.getId());
    assertThat(request.getDocument())
        .hasFieldOrPropertyWithValue("userId", USER_NAME)
        .hasFieldOrPropertyWithValue("documentId", 0L);
}
 
Example #14
Source File: LappsGridRecommenderConformityTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
@Parameters(method = "getNerServices")
public void testNerConformity(LappsGridService aService) throws Exception
{
    CAS cas = loadData();
    
    predict(aService.getUrl(), cas);

    SoftAssertions softly = new SoftAssertions();
    softly.assertThat(JCasUtil.select(cas.getJCas(), Token.class))
            .as("Prediction should contain Tokens")
            .isNotEmpty();
    softly.assertThat(JCasUtil.select(cas.getJCas(), NamedEntity.class))
            .as("Prediction should contain NER")
            .isNotEmpty();

    softly.assertAll();
}
 
Example #15
Source File: DL4JSequenceRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
public void thatNerPredictionWorks() throws Exception
{
    DL4JSequenceRecommender sut = new DL4JSequenceRecommender(buildNerRecommender(), traits,
            cache);
    JCas jCas = loadNerDevelopmentData();
    CAS cas = jCas.getCas();

    sut.train(context, asList(cas));

    addScoreFeature(cas, NamedEntity.class, "value");
    sut.predict(context, cas);

    List<NamedEntity> predictions = getPredictions(cas, NamedEntity.class);
    assertThat(predictions).as("Predictions have been written to CAS")
        .isNotEmpty();
    
    // check how many labels are not padding labels
    long numWithLabel = predictions.stream()
            .filter(p -> !p.getValue().equals(DL4JSequenceRecommender.NO_LABEL)).count();
    System.out.printf("Predicted %d labels not no_label out of %d.%n", numWithLabel,
            predictions.size());
    
    assertThat(predictions).as("There are predictions other than *No_Label*")
        .anyMatch(l -> !l.getValue().equals(DL4JSequenceRecommender.NO_LABEL));
}
 
Example #16
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotationWithTrailingWhitespace() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("one  two");
    new Token(jcas, 0, 3).addToIndexes();
    new Token(jcas, 5, 8).addToIndexes();
    new Sentence(jcas, 0, 8).addToIndexes();
    
    // NE has trailing whitespace - on export this should be silently dropped
    new NamedEntity(jcas, 0, 4).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #17
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotationWithTrailingWhitespaceAtEnd() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("one two ");
    new Token(jcas, 0, 3).addToIndexes();
    new Token(jcas, 4, 7).addToIndexes();
    new Sentence(jcas, 0, 7).addToIndexes();
    
    // NE has trailing whitespace - on export this should be silently dropped
    new NamedEntity(jcas, 4, 8).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #18
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotationWithLeadingWhitespaceAtStart() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText(" one two");
    new Token(jcas, 1, 4).addToIndexes();
    new Token(jcas, 5, 8).addToIndexes();
    new Sentence(jcas, 1, 8).addToIndexes();
    
    // NE has leading whitespace - on export this should be silently dropped
    new NamedEntity(jcas, 0, 4).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #19
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testAnnotationWithLeadingWhitespace() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("one  two");
    new Token(jcas, 0, 3).addToIndexes();
    new Token(jcas, 5, 8).addToIndexes();
    new Sentence(jcas, 0, 8).addToIndexes();
    
    // NE has leading whitespace - on export this should be silently dropped
    new NamedEntity(jcas, 4, 8).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #20
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroWidthAnnotationBetweenTokenIsMovedToEndOfPreviousToken() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("one  two");
    new Token(jcas, 0, 3).addToIndexes();
    new Token(jcas, 5, 8).addToIndexes();
    new Sentence(jcas, 0, 8).addToIndexes();
    
    // NE is after the end of the last token and should be moved to the end of the last token
    // otherwise it could not be represented in the TSV3 format.
    new NamedEntity(jcas, 4, 4).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #21
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroWidthAnnotationBeyondLastTokenIsMovedToEndOfLastToken() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("one two  ");
    new Token(jcas, 0, 3).addToIndexes();
    new Token(jcas, 4, 7).addToIndexes();
    new Sentence(jcas, 0, 7).addToIndexes();
    
    // NE is after the end of the last token and should be moved to the end of the last token
    // otherwise it could not be represented in the TSV3 format.
    new NamedEntity(jcas, 8, 8).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #22
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroWidthAnnotationBeforeFirstTokenIsMovedToBeginOfFirstToken() throws Exception
{
    JCas jcas = JCasFactory.createJCas();
    
    DocumentMetaData.create(jcas).setDocumentId("doc");
    jcas.setDocumentText("  one two");
    new Token(jcas, 2, 5).addToIndexes();
    new Token(jcas, 6, 9).addToIndexes();
    new Sentence(jcas, 2, 9).addToIndexes();
    
    // NE is after the end of the last token and should be moved to the end of the last token
    // otherwise it could not be represented in the TSV3 format.
    new NamedEntity(jcas, 1, 1).addToIndexes();
    
    writeAndAssertEquals(jcas);
}
 
Example #23
Source File: Tsv3XSerializerTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleSubTokenWithValue() throws Exception
{
    // Create test document
    JCas cas = makeJCasOneSentence("This is a test .");
    addNamedEntity(cas, 1, 3, "PER");
    
    // Set up TSV schema
    TsvSchema schema = new TsvSchema();
    Type namedEntityType = cas.getCasType(NamedEntity.type);
    schema.addColumn(
            new TsvColumn(namedEntityType, LayerType.SPAN, "value", FeatureType.PRIMITIVE));

    // Convert test document content to TSV model
    TsvDocument doc = Tsv3XCasDocumentBuilder.of(schema, cas);

    String expectedSentence = 
            "#Text=This is a test .\n" + 
            "1-1\t0-4\tThis\t_\t\n" + 
            "1-1.1\t1-3\thi\tPER\t\n" + 
            "1-2\t5-7\tis\t_\t\n" + 
            "1-3\t8-9\ta\t_\t\n" + 
            "1-4\t10-14\ttest\t_\t\n" + 
            "1-5\t15-16\t.\t_\t\n";
    assertEquals(expectedSentence, doc.getSentences().get(0).toString());
}
 
Example #24
Source File: Tsv3XSerializerTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleZeroWidthTokenWithoutValue() throws Exception
{
    // Create test document
    JCas cas = makeJCasOneSentence("This is a test .");
    addNamedEntity(cas, 0, 0, null);
    
    // Set up TSV schema
    TsvSchema schema = new TsvSchema();
    Type namedEntityType = cas.getCasType(NamedEntity.type);
    schema.addColumn(
            new TsvColumn(namedEntityType, LayerType.SPAN, "value", FeatureType.PRIMITIVE));

    // Convert test document content to TSV model
    TsvDocument doc = Tsv3XCasDocumentBuilder.of(schema, cas);

    String expectedSentence = 
            "#Text=This is a test .\n" + 
            "1-1\t0-4\tThis\t_\t\n" + 
            "1-1.1\t0-0\t\t*\t\n" + 
            "1-2\t5-7\tis\t_\t\n" + 
            "1-3\t8-9\ta\t_\t\n" + 
            "1-4\t10-14\ttest\t_\t\n" + 
            "1-5\t15-16\t.\t_\t\n";
    assertEquals(expectedSentence, doc.getSentences().get(0).toString());
}
 
Example #25
Source File: SpanRendererTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception
{
    if (jcas == null) {
        jcas = JCasFactory.createJCas();
    }
    else {
        jcas.reset();
    }
    
    project = new Project();
    project.setId(1l);
    project.setMode(PROJECT_TYPE_ANNOTATION);
    
    neLayer = new AnnotationLayer(NamedEntity.class.getName(), "NE", SPAN_TYPE, project, true,
            TOKENS, ANY_OVERLAP);
    neLayer.setId(1l);

    featureSupportRegistry = new FeatureSupportRegistryImpl(asList());
    layerSupportRegistry = new LayerSupportRegistryImpl(asList());
}
 
Example #26
Source File: SpanRendererTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void thatSpanCrossSentenceBehaviorOnRenderGeneratesErrors()
{
    neLayer.setCrossSentence(false);
    
    jcas.setDocumentText(StringUtils.repeat("a", 20));
    
    new Sentence(jcas, 0, 10).addToIndexes();
    new Sentence(jcas, 10, 20).addToIndexes();
    NamedEntity ne = new NamedEntity(jcas, 5, 15);
    ne.addToIndexes();
    
    SpanAdapter adapter = new SpanAdapter(layerSupportRegistry, featureSupportRegistry, null,
            neLayer, () -> asList(), asList(new SpanCrossSentenceBehavior()));
    
    SpanRenderer sut = new SpanRenderer(adapter, layerSupportRegistry, featureSupportRegistry,
            asList(new SpanCrossSentenceBehavior()));
    
    VDocument vdoc = new VDocument();
    sut.render(jcas.getCas(), asList(), vdoc, 0, jcas.getDocumentText().length());
    
    assertThat(vdoc.comments())
            .usingFieldByFieldElementComparator()
            .containsExactlyInAnyOrder(new VComment(ne, VCommentType.ERROR, 
                    "Crossing sentence boundaries is not permitted."));
}
 
Example #27
Source File: SpanAdapterTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void thatAdjacentAnnotationsDoNotOverlap() throws AnnotationException
{
    jcas.setDocumentText("Test.");
    new Sentence(jcas, 0, 5).addToIndexes();
    new Token(jcas, 0, 4).addToIndexes();
    new Token(jcas, 4, 5).addToIndexes();
    new NamedEntity(jcas, 0, 4).addToIndexes();
    new NamedEntity(jcas, 4, 5).addToIndexes();
    
    SpanAdapter sut = new SpanAdapter(layerSupportRegistry, featureSupportRegistry, null,
            neLayer, () -> asList(), behaviors);

    neLayer.setOverlapMode(NO_OVERLAP);
    assertThat(sut.validate(jcas.getCas()))
            .isEmpty();
}
 
Example #28
Source File: CasMergeTest.java    From webanno with Apache License 2.0 6 votes vote down vote up
@Test
public void simpleCopyToEmptyTest()
    throws Exception
{
    AnnotatorState state = new AnnotatorStateImpl(CURATION);
    state.setUser(new User());
    
    CAS jcas = createJCas().getCas();
    AnnotationFS clickedFs = createNEAnno(jcas, "NN", 0, 0);

    CAS curatorCas = createJCas().getCas();
    createTokenAnno(curatorCas, 0, 0);

    sut.mergeSpanAnnotation(null, null, neLayer, curatorCas, clickedFs, false);

    assertThat(selectCovered(curatorCas, getType(curatorCas, NamedEntity.class), 0, 0))
            .hasSize(1);
}
 
Example #29
Source File: OpenNlpNerRecommenderTest.java    From inception with Apache License 2.0 6 votes vote down vote up
@Test
public void thatPredictionWorks() throws Exception
{
    OpenNlpNerRecommender sut = new OpenNlpNerRecommender(recommender, traits);
    List<CAS> casList = loadDevelopmentData();
    
    CAS cas = casList.get(0);
    try (CasStorageSession session = CasStorageSession.open()) {
        session.add("testCas", EXCLUSIVE_WRITE_ACCESS, cas);
        RecommenderTestHelper.addScoreFeature(cas, NamedEntity.class, "value");
    }

    sut.train(context, asList(cas));

    sut.predict(context, cas);

    Collection<NamedEntity> predictions = JCasUtil.select(cas.getJCas(), NamedEntity.class);

    assertThat(predictions).as("Predictions have been written to CAS")
        .isNotEmpty();
}
 
Example #30
Source File: DKPro2Tcf.java    From inception with Apache License 2.0 5 votes vote down vote up
public void writeNamedEntity(JCas aJCas, TextCorpus aTextCorpus,
        Map<Integer, eu.clarin.weblicht.wlfxb.tc.api.Token> aTokensBeginPositionMap)
{
    if (!JCasUtil.exists(aJCas, NamedEntity.class)) {
        // Do nothing if there are no named entities in the CAS
        log.debug("Layer [{}]: empty", TextCorpusLayerTag.NAMED_ENTITIES.getXmlName());
        return;
    }
    
    String tagSetName = "BART";
    for (TagsetDescription tagSet : select(aJCas, TagsetDescription.class)) {
        if (tagSet.getLayer().equals(NamedEntity.class.getName())) {
            tagSetName = tagSet.getName();
            break;
        }
    }

    NamedEntitiesLayer namedEntitiesLayer = aTextCorpus.createNamedEntitiesLayer(tagSetName);

    log.debug("Layer [{}]: created", TextCorpusLayerTag.NAMED_ENTITIES.getXmlName());
    
    for (NamedEntity namedEntity : select(aJCas, NamedEntity.class)) {
        List<Token> tokensInCas = selectCovered(aJCas, Token.class, namedEntity.getBegin(),
                namedEntity.getEnd());
        List<eu.clarin.weblicht.wlfxb.tc.api.Token> tokensInTcf = new ArrayList<>();
        for (Token token : tokensInCas) {
            tokensInTcf.add(aTokensBeginPositionMap.get(token.getBegin()));
        }
        namedEntitiesLayer.addEntity(namedEntity.getValue(), tokensInTcf);
    }
}