Java Code Examples for org.apache.uima.fit.util.JCasUtil#select()

The following examples show how to use org.apache.uima.fit.util.JCasUtil#select() . 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: StructuralEntityTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcess() throws Exception {

  processJCas(
      PARAM_QUERY,
      "heading[level=1] + Ordered ListItem",
      PARAM_CONFIDENCE,
      "0.5",
      PARAM_TYPE,
      Person.class.getSimpleName(),
      PARAM_SUB_TYPE,
      "sub");

  Collection<Person> people = JCasUtil.select(jCas, Person.class);

  assertEquals(3, people.size());
  Set<String> names = people.stream().map(Person::getCoveredText).collect(Collectors.toSet());
  assertTrue(names.contains(N1));
  assertTrue(names.contains(N2));
  assertTrue(names.contains(N3));

  assertEquals(0.5, people.iterator().next().getConfidence(), 0.0);
  assertEquals("sub", people.iterator().next().getSubType());
}
 
Example 2
Source File: MaltParserTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcess() throws AnalysisEngineProcessException, ResourceInitializationException {
  final String text = "The fox jumps over the dog.";
  jCas.setDocumentText(text);

  processJCas();

  final Collection<Sentence> select = JCasUtil.select(jCas, Sentence.class);
  final Sentence s1 = select.iterator().next();

  final List<Dependency> dependencies = JCasUtil.selectCovered(jCas, Dependency.class, s1);

  // We could test the output here, but its so model dependent its not
  // worth it, as long as annotations have been created"

  // 7 = 6 words + 1 punctuation, each should have a dependency
  assertEquals(7, dependencies.size());
}
 
Example 3
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 4
Source File: StructureContentExtractorTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisableTextBlocks() throws Exception {
  JCas jCas = JCasSingleton.getJCasInstance();

  BaleenContentExtractor contentExtractor = new TestStructureContentExtractor();
  Map<String, Object> map = new HashMap<>();
  map.put(StructureContentExtractor.FIELD_EXTRACT_TEXT_BLOCKS, "false");
  contentExtractor.initialize(new CustomResourceSpecifier_impl(), map);

  contentExtractor.processStream(null, "source", jCas);

  assertEquals("Title\nExample", jCas.getDocumentText());
  Collection<Text> select = JCasUtil.select(jCas, Text.class);
  assertTrue(select.isEmpty());
}
 
Example 5
Source File: UimaSupportTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeWithRelation() {
  UimaSupport support = new UimaSupport(PIPELINE, UimaSupportTest.class, history, monitor, false);

  Person p1 = new Person(jCas);
  p1.setBegin(0);
  p1.setEnd(0);
  p1.addToIndexes();

  Person p2 = new Person(jCas);
  p2.setBegin(0);
  p2.setEnd(0);
  p2.addToIndexes();

  Person p3 = new Person(jCas);
  p3.setBegin(1);
  p3.setEnd(1);
  p3.addToIndexes();

  Relation r = new Relation(jCas);
  r.setBegin(0);
  r.setEnd(1);
  r.setSource(p2);
  r.setTarget(p3);
  r.addToIndexes();

  support.mergeWithExisting(p1, p2);

  List<Person> people = new ArrayList<>(JCasUtil.select(jCas, Person.class));
  List<Relation> relations = new ArrayList<>(JCasUtil.select(jCas, Relation.class));

  assertEquals(2, people.size());
  assertEquals(p1, people.get(0));
  assertEquals(p3, people.get(1));

  assertEquals(1, relations.size());
  assertEquals(r, relations.get(0));
  assertEquals(p1, relations.get(0).getSource());
  assertEquals(p3, relations.get(0).getTarget());
}
 
Example 6
Source File: MentionedAgainTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testDifferentTypes()
    throws AnalysisEngineProcessException, ResourceInitializationException {
  jCas.setDocumentText("John went to London. I saw John there. He's a great guy John.");

  Person p1 = new Person(jCas);
  p1.setBegin(0);
  p1.setEnd(4);
  p1.setValue("John");
  p1.addToIndexes();

  Buzzword b = new Buzzword(jCas);
  b.setBegin(27);
  b.setEnd(31);
  b.setValue("John");
  b.addToIndexes();

  processJCas();

  List<Entity> selectPerson = new ArrayList<>(JCasUtil.select(jCas, Person.class));
  assertEquals(3, selectPerson.size());
  assertEquals("John", selectPerson.get(0).getValue());
  assertEquals("John", selectPerson.get(1).getValue());
  assertEquals("John", selectPerson.get(2).getValue());

  List<Entity> selectBuzzword = new ArrayList<>(JCasUtil.select(jCas, Buzzword.class));
  assertEquals(3, selectBuzzword.size());
  assertEquals("John", selectBuzzword.get(0).getValue());
  assertEquals("John", selectBuzzword.get(1).getValue());
  assertEquals("John", selectBuzzword.get(2).getValue());
}
 
Example 7
Source File: DocumentRelationshipAnnotatorTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentenceThreshold()
    throws AnalysisEngineProcessException, ResourceInitializationException {

  jCas.setDocumentText(SENTENCE_1 + " " + SENTENCE_2 + " " + SENTENCE_3);
  int s2Begin = SENTENCE_1.length() + 1;
  int s3Begin = s2Begin + SENTENCE_2.length() + 1;

  final Sentence s1 = new Sentence(jCas);
  s1.setBegin(0);
  s1.setEnd(SENTENCE_1.length());
  s1.addToIndexes();
  final Sentence s2 = new Sentence(jCas);
  s2.setBegin(s2Begin);
  s2.setEnd(s2Begin + SENTENCE_2.length());
  s2.addToIndexes();
  final Sentence s3 = new Sentence(jCas);
  s3.setBegin(s3Begin);
  s3.setEnd(jCas.getDocumentText().length());
  s3.addToIndexes();

  final Person jon = Annotations.createPerson(jCas, 0, 3, "Jon");
  final Person steve = Annotations.createPerson(jCas, s2Begin + 7, s2Begin + 12, "Steve");
  final Person chris = Annotations.createPerson(jCas, s3Begin + 9, s3Begin + 14, "Chris");
  final Location london = Annotations.createLocation(jCas, 12, 18, "London", "");

  processJCas(DocumentRelationshipAnnotator.PARAM_THRESHOLD, 1);

  final List<Relation> relations = new ArrayList<>(JCasUtil.select(jCas, Relation.class));
  Assert.assertEquals(3, relations.size());
  final Relation js = findRelationBetween(relations, jon, steve);
  final Relation ls = findRelationBetween(relations, london, steve);
  final Relation sc = findRelationBetween(relations, steve, chris);

  assertEquals(1, js.getSentenceDistance());
  assertEquals(1, ls.getSentenceDistance());
  assertEquals(1, sc.getSentenceDistance());
}
 
Example 8
Source File: AddTitleToPerson.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doProcess(JCas jCas) throws AnalysisEngineProcessException {
  // We copy this array as we'll modify people as we go
  Collection<Person> people = JCasUtil.select(jCas, Person.class);

  for (Person p : people) {
    // Find existing titles
    findExistingTitle(p);

    while (makeReplacement(jCas, p)) {
      // Make as many replacements as possible, to capture things like Sir Major General Smith.
    }
  }
}
 
Example 9
Source File: WordNetLemmatizerTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddsLemma() throws UIMAException, ResourceInitializationException {
  jCas.setDocumentText("Is this working?");

  final WordToken t = new WordToken(jCas);
  t.setBegin(jCas.getDocumentText().indexOf("working"));
  t.setEnd(t.getBegin() + "working".length());
  t.setPartOfSpeech("VERB");
  t.addToIndexes();

  processJCas("wordnet", wordnetErd);

  final List<WordToken> out = new ArrayList<>(JCasUtil.select(jCas, WordToken.class));
  assertEquals("work", out.get(0).getLemmas(0).getLemmaForm());
}
 
Example 10
Source File: AnnotationUtilsTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterToTopLevelAnnotations() {
  final Collection<WordToken> select = JCasUtil.select(jCas, WordToken.class);
  final List<WordToken> topLevel = AnnotationUtils.filterToTopLevelAnnotations(select);

  Assert.assertEquals(3, topLevel.size());
  Assert.assertEquals("0123456789", topLevel.get(0).getCoveredText());
  Assert.assertEquals("abcde", topLevel.get(1).getCoveredText());
  Assert.assertEquals("fghij", topLevel.get(2).getCoveredText());
}
 
Example 11
Source File: ResultMention.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public static List<ResultMention> getResultConceptMentionsFromJCas(JCas jCas) {
  List<ResultMention> result = new ArrayList<>();
  for(ConceptEntity ne: JCasUtil.select(jCas, ConceptEntity.class)) {
    ResultEntity re = new ResultEntity();
    re.setScore(ne.getScore());
    re.setKbEntity(new KBIdentifiedEntity(ne.getID()));
    ResultMention rm = new ResultMention();
    rm.setBestEntity(re);
    rm.setCharacterLength(ne.getEnd() - ne.getBegin());
    rm.setCharacterOffset(ne.getBegin());
    rm.setMention(ne.getCoveredText());
    result.add(rm);
  }
  return result;
}
 
Example 12
Source File: JCasBuilderTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuild() throws UIMAException {
  JCas jCas = JCasSingleton.getJCasInstance();

  JCasBuilder builder = new JCasBuilder(jCas);

  assertSame(jCas, builder.getJCas());

  int s = builder.getCurrentOffset();
  assertEquals(s, 0);
  builder.addText("Hello");
  int e = builder.getCurrentOffset();
  assertEquals(e, "Hello".length());

  builder.addAnnotations(Arrays.asList(new Entity(jCas), new Paragraph(jCas)), s, e, 6);

  builder.build();

  assertEquals(jCas.getDocumentText(), "Hello");
  Collection<Base> entities = JCasUtil.select(jCas, Base.class);
  assertEquals(entities.size(), 2);
  Iterator<Base> iterator = entities.iterator();
  Annotation a = iterator.next();
  Annotation b = iterator.next();
  Entity entity;
  Paragraph paragraph;
  if (a instanceof Entity) {
    entity = (Entity) a;
    paragraph = (Paragraph) b;
  } else {
    entity = (Entity) b;
    paragraph = (Paragraph) a;
  }

  assertEquals(entity.getBegin(), 0);
  assertEquals(entity.getEnd(), 5);
  assertEquals(paragraph.getBegin(), 0);
  assertEquals(paragraph.getEnd(), 5);
  assertEquals(paragraph.getDepth(), 6);
}
 
Example 13
Source File: StandaloneArgumentWithSinglePremise.java    From argument-reasoning-comprehension-task with Apache License 2.0 5 votes vote down vote up
public static List<StandaloneArgumentWithSinglePremise> extractPremises(
        StandaloneArgument standaloneArgument)
        throws IOException
{
    List<StandaloneArgumentWithSinglePremise> result = new ArrayList<>();

    JCas jCas = standaloneArgument.getJCas();
    Collection<Premise> premises = new ArrayList<>(JCasUtil.select(jCas, Premise.class));
    if (premises.isEmpty()) {
        System.err.println("Only argument with annotated premises are allowed here");
        return result;
    }

    for (Premise premise : premises) {
        StandalonePremise standalonePremise = new StandalonePremise();

        // fill data
        standalonePremise.setGist(ArgumentUnitUtils.getProperty(premise, "gist"));
        standalonePremise.setDisambiguatedStance(
                ArgumentUnitUtils.getProperty(premise, "disambiguatedStance"));
        standalonePremise.setOriginalText(premise.getCoveredText());
        standalonePremise
                .setPremiseId(createUniquePremiseId(standaloneArgument.getId(), premise));

        StandaloneArgumentWithSinglePremise s = new StandaloneArgumentWithSinglePremise(
                standaloneArgument, standalonePremise);
        result.add(s);
    }

    return result;
}
 
Example 14
Source File: RelationTypeFilter.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doProcess(final JCas jCas) throws AnalysisEngineProcessException {

  final List<Relation> toRemove = new ArrayList<>();

  for (final Relation relation : JCasUtil.select(jCas, Relation.class)) {
    final String type = relation.getRelationshipType().toLowerCase();
    final Set<RelationConstraint> rcs = constraints.get(type);

    boolean remove;
    if (rcs == null || rcs.isEmpty()) {

      // In strict mode we remove
      if (strict) {
        remove = true;
      } else {
        remove = false;
      }

    } else {
      remove = !checkValid(rcs, relation);
    }

    if (remove) {
      toRemove.add(relation);
    }
  }

  removeFromJCasIndex(toRemove);
}
 
Example 15
Source File: EntityFirstSentenceFeature.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
@Override public Map<Integer, Double> extract(JCas jCas) {
  Map<Integer, Double> features = new HashMap<>();

  Collection<Sentence> sentences = JCasUtil.select(jCas, Sentence.class);
  // Check if the first real sentence contains the entity.
  int firstSentenceBegin = 0;
  int firstSentenceEnd = 0;
  //if (sentences.size() > 3) {
    Iterator<Sentence> itr = sentences.iterator();
    //itr.next();
    //itr.next();
    Sentence sentence = itr.next();
    firstSentenceBegin = sentence.getBegin();
    firstSentenceEnd = sentence.getEnd();
  //}

  boolean inFirstSentence = false;
  for (AidaEntity ae : entityMentions) {
    int offset = ae.getBegin();
    inFirstSentence = (offset >= firstSentenceBegin) && (offset < firstSentenceEnd);
    if (inFirstSentence) {
      break;
    }
    if (ae.getBegin() > firstSentenceEnd) {
      break;
    }
  }


  features.put(getId(), inFirstSentence ? 1.0 : 0.0);
  return features;
}
 
Example 16
Source File: RegExRelationshipTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testValueGroup()
    throws AnalysisEngineProcessException, ResourceInitializationException {

  jCas.setDocumentText("Jon visits London. Steve went to Cheltenham");

  final Person jon = Annotations.createPerson(jCas, 0, 3, "Jon");
  final Person steve = Annotations.createPerson(jCas, 19, 24, "Steve");
  final Location london = Annotations.createLocation(jCas, 11, 17, "London", "");
  final Location cheltenham = Annotations.createLocation(jCas, 33, 43, "Cheltenham", "");

  processJCas(
      PARAM_PATTERN,
      "(:Person:)\\s+(visit\\w*|went to)\\s+(:Location:)",
      PARAM_TYPE,
      LOCATED_AT,
      PARAM_SOURCE_GROUP,
      1,
      PARAM_VALUE_GROUP,
      2,
      PARAM_TARGET_GROUP,
      3,
      PARAM_SUB_TYPE,
      SUB);

  final List<Relation> relations = new ArrayList<>(JCasUtil.select(jCas, Relation.class));
  assertEquals(2, relations.size());
  final Relation jlr = findRelationBetween(relations, jon, london);
  final Relation scr = findRelationBetween(relations, steve, cheltenham);

  assertEquals(LOCATED_AT, jlr.getRelationshipType());
  assertEquals(SUB, jlr.getRelationSubType());
  assertEquals("visits", jlr.getValue());
  assertEquals(0, jlr.getBegin());
  assertEquals(17, jlr.getEnd());
  assertEquals(LOCATED_AT, scr.getRelationshipType());
  assertEquals(SUB, scr.getRelationSubType());
  assertEquals("went to", scr.getValue());
  assertEquals(19, scr.getBegin());
  assertEquals(43, scr.getEnd());
}
 
Example 17
Source File: I2b2JdbcWriter.java    From ctakes-docker with Apache License 2.0 4 votes vote down vote up
private void writeConceptRows(final JCas jcas, final String instance, final String patient, final String encounter, final String provider) throws SQLException {

		final Collection<IdentifiedAnnotation> annotations = JCasUtil.select(jcas, IdentifiedAnnotation.class);
		if (annotations.isEmpty()) {
			logger.log(Level.WARNING, "No annotations found. Patient, encounter, provider =  " + patient + ", " + encounter + ", " + provider);
			return;
		}
		
		// Do not use try with resource as it will close the prepared statement, which we do not yet want
		final PreparedStatement preparedStatement = _tableSqlInfoMap.get( tableName ).getPreparedStatement();
		int batchCount = _tableSqlInfoMap.get( tableName ).getBatchCount();

		final Map<I2b2FieldInfo, Object> fieldInfoValues = new EnumMap<>(I2b2FieldInfo.class);
            
            // Example from Thomas DeSain for    FileName: 2597_0036_0002.txt.output.txt
            // <PATIENT_NUM>_<ENCOUNTER_NUM>_<INSTANCE_NUM>.txt.output.txt
            // Document_ID = 2597_0036_0002.txt.output.txt Patient_Num = 2597 Encounter_Num = 0036 Instance_Num = 0002
            // Note the above example includes .txt.output.txt because it used to read text files
            // That part is now left off
            fieldInfoValues.put(I2b2FieldInfo.ENCOUNTER_NUM, encounter+"_"+patient+"_"+instance);
	    fieldInfoValues.put(I2b2FieldInfo.PATIENT_NUM, patient);
	    fieldInfoValues.put(I2b2FieldInfo.PROVIDER_ID, provider);
	    fieldInfoValues.put(I2b2FieldInfo.START_DATE, "" );   // was not filled in by I2b2ReadyFileWriter, not expected to be filled in now
	    fieldInfoValues.put(I2b2FieldInfo.SOURCESYSTEM_CD, CTAKES_VERSION);
	    fieldInfoValues.put(I2b2FieldInfo.INSTANCE_NUM, INSTANCE_NUM_DEFAULT);

		for (IdentifiedAnnotation annotation : annotations) {

			logger.log(Level.FINE, "Anno = " + annotation);
			fieldInfoValues.put(I2b2FieldInfo.I2B2_OBERVATION_BLOB, annotation.getCoveredText());
			
			ArrayList<String> modifierCds = new ArrayList<String>();
			ArrayList<String> tvalChars = new ArrayList<String>(); // Note I knew of an easier way to deal with Pair in Java, I wouldn't rely on modifierCd(1) being associated with tVals(1). 
			final Set<String> codes = new HashSet<String>(); 
		        // A Cui may belong to multiple Tuis, making multiple UmlsConcept objects (one per tui).
		        // I2b2 does NOT want multiple rows of a single Cui just because it has multiple tuis.
			codes.addAll(OntologyConceptUtil.getCuis(annotation));

			// MODIFIER_CD column contains "@" or an attribute name formatted like this "CUSTOM:attributename:" such as "CUSTOM:SECTION:" for "SECTION" 
			modifierCds.add(MODIFIER_CD_DEFAULT); // 

			addNonNull(tvalChars, "");
			
			modifierCds.add(MODIFIER_CD_BEGIN); 
			addNonNull(tvalChars, annotation.getBegin());
			
			modifierCds.add(MODIFIER_CD_END); 
			addNonNull(tvalChars, annotation.getEnd());
			
			modifierCds.add(MODIFIER_CD_POLARITY); 
			addNonNull(tvalChars, (annotation.getPolarity() < 0 ? NEGATED : AFFIRMED));
			
			modifierCds.add(MODIFIER_CD_SUBJECT); 
			addNonNull(tvalChars, annotation.getSubject());
			
			modifierCds.add(MODIFIER_CD_SECTION); 
			addNonNull(tvalChars, annotation.getSegmentID());
			
			modifierCds.add(MODIFIER_CD_UNCERTAINTY); 
			addNonNull(tvalChars, annotation.getUncertainty());
			
			modifierCds.add(MODIFIER_CD_BODY_LOCATION); 
			addNonNull(tvalChars, "");
			
			modifierCds.add(MODIFIER_CD_SEVERITY); 
			addNonNull(tvalChars, "");
			
			modifierCds.add(MODIFIER_CD_DOC_TIME_REL); 
			addNonNull(tvalChars, getDocTimeRel(annotation));
			
			modifierCds.add(MODIFIER_CD_SENT); 
			addNonNull(tvalChars, getSentenceCoverage(jcas, annotation));
			
			for (String code : codes) {
				fieldInfoValues.put(I2b2FieldInfo.CONCEPT_CD, code);
				for (int i = 0; i<modifierCds.size(); i++) {
					String modifierCd = modifierCds.get(i);
					fieldInfoValues.put(I2b2FieldInfo.MODIFIER_CD, modifierCd);
					if (modifierCd.equals(MODIFIER_CD_DEFAULT)) {
						fieldInfoValues.put(I2b2FieldInfo.VALTYPE_CD, "");
						fieldInfoValues.put(I2b2FieldInfo.TVAL_CHAR, tvalChars.get(i)); // "" when MODIFIER_CD_DEFAULT
					} else {
						fieldInfoValues.put(I2b2FieldInfo.VALTYPE_CD, "T");
						fieldInfoValues.put(I2b2FieldInfo.TVAL_CHAR, tvalChars.get(i)); // Attribute value such as offset, or text of sentence
					}
			        logger.log(Level.FINE, "before: batchCount = " + batchCount);
                    logger.log(Level.FINE,"preparedStatement = " + preparedStatement);
					batchCount = writeTableRow( preparedStatement, batchCount, fieldInfoValues );
                    logger.log(Level.FINE," after: batchCount = " + batchCount);
				}
			}
		}
        _tableSqlInfoMap.get( tableName ).setBatchCount( batchCount );
	}
 
Example 18
Source File: PatternExtractorTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testNegationProcess() throws AnalysisEngineProcessException {
  final String text = "The fox did not jump over the dog.";
  jCas.setDocumentText(text);

  final Sentence sentence = new Sentence(jCas);
  sentence.setBegin(0);
  sentence.setEnd(text.length());
  sentence.addToIndexes(jCas);

  int offset = 0;
  while (offset < text.length()) {
    int end = text.indexOf(" ", offset);
    if (end == -1) {
      end = text.indexOf(".", offset);
    }

    if (end > 0) {
      final WordToken wordToken = new WordToken(jCas);
      wordToken.setBegin(offset);
      wordToken.setEnd(end);
      // Fake the POS
      wordToken.setPartOfSpeech("VBZ");
      wordToken.addToIndexes(jCas);
      offset = end + 1;
    } else {
      offset = text.length();
    }
  }

  final Entity fox = new Entity(jCas);
  fox.setBegin(4);
  fox.setEnd(7);
  fox.addToIndexes(jCas);

  final Entity dog = new Entity(jCas);
  dog.setBegin(30);
  dog.setEnd(33);
  dog.addToIndexes(jCas);

  SimplePipeline.runPipeline(jCas, ae);

  final Collection<Pattern> patterns = JCasUtil.select(jCas, Pattern.class);
  Assert.assertEquals(0, patterns.size());
}
 
Example 19
Source File: NerMentionAnnotator.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
	Collection<Sentence> sentences = JCasUtil.select(jCas, Sentence.class);
	for(Sentence sentence : sentences){
		List<Token> tokens = JCasUtil.selectCovered(jCas, Token.class, sentence);
		TextClassificationOutcome previous = null;
		int begin = 0;
		int end = 0;
		for (int i = 0; i < tokens.size(); i++) {
			Token token = tokens.get(i);
			
			List<TextClassificationOutcome> classOutcomes = JCasUtil.selectCovered(jCas, TextClassificationOutcome.class, token);
			TextClassificationOutcome classOutcome = classOutcomes.get(0);
			String outcomeClassType = classOutcome.getOutcome().replaceAll(".-", "");
			
			if(i == tokens.size()-1){
				// we reached the end of the sentence.
				if(previous != null){
					if(outcomeClassType.equals(previous.getOutcome().replaceAll(".-", ""))){
						end = token.getEnd();
						addMentionToCas(jCas, previous, begin, end);
					} else {
						addMentionToCas(jCas, previous, begin, end);
						addMentionToCas(jCas, classOutcome, token.getBegin(), token.getEnd());
					}
				} else {
					addMentionToCas(jCas, classOutcome, token.getBegin(), token.getEnd());
				}
				break;
			}

			
			if(previous == null){
				previous = classOutcome;
				begin = token.getBegin();
				end = token.getEnd();
				continue;
			}
			
			
			if(outcomeClassType.equals(previous.getOutcome().replaceAll(".-", ""))){
				previous = classOutcome;
				end = token.getEnd();
			} else {
				addMentionToCas(jCas, previous, begin, end);
				previous = classOutcome;
				begin = token.getBegin();
				end = token.getEnd();
			}
							
		}
	}
}
 
Example 20
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 3 votes vote down vote up
/**
 * @param jcas
 * @param clasz
 * @param strictClass
 * @return the annotations of this jcas that are strictly equal to
 *         strictClass
 */
public static <T extends TOP> Collection<T> selectStrict(JCas jcas,
        Class<T> clasz, Class<?> strictClass) {

    Collection<T> raw = JCasUtil.select(jcas, clasz);
    return filterStrict(raw, strictClass);
}