Java Code Examples for org.apache.uima.cas.text.AnnotationFS#setIntValue()

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#setIntValue() . 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: CasOutputDestination.java    From biomedicus with Apache License 2.0 6 votes vote down vote up
@Override
public void controlWordEncountered(KeywordAction keywordAction) {
  AnnotationFS annotation;
  int currentTextIndex = sofaBuilder.length();
  String controlWord = keywordAction.getControlWord();

  Type type;
  if (annotationTypeForControlWord.containsKey(controlWord)) {
    type = annotationTypeForControlWord.get(controlWord);
  } else {
    return;
  }
  annotation = destinationView.createAnnotation(type, currentTextIndex,
      currentTextIndex);
  Feature paramFeature = type.getFeatureByBaseName("param");
  if (keywordAction.hasParameter()) {
    annotation.setIntValue(paramFeature, keywordAction.getParameter());
  }
  Feature indexFeature = type.getFeatureByBaseName("index");
  annotation.setIntValue(indexFeature, keywordAction.getBegin());
  Feature knownFeature = type.getFeatureByBaseName("known");
  annotation.setBooleanValue(knownFeature, true);

  destinationView.addFsToIndexes(annotation);
}
 
Example 2
Source File: PropertyCasMapping.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
@Nullable
AnnotationFS getAnnotation(CAS cas, int begin, int end, int value) {
  if (begin < 0) {
    throw new IllegalArgumentException("Begin: " + begin + "before 0.");
  }

  if (end < begin) {
    throw new IllegalArgumentException(
        annotationClassName + " - illegal annotation span at begin: " + begin
            + " end: " + end);
  }

  if (!zeroLengthEmitted && end == begin) {
    return null;
  }

  TypeSystem typeSystem = cas.getTypeSystem();
  Type type = typeSystem.getType(annotationClassName);
  if (type == null) {
    throw new IllegalStateException("Annotation class not found: " + annotationClassName);
  }
  AnnotationFS annotation = cas.createAnnotation(type, begin, end);
  if (valueIncluded) {
    Feature valueFeature = type.getFeatureByBaseName("value");
    annotation.setIntValue(valueFeature, value);
  }
  return annotation;
}
 
Example 3
Source File: CasIndexListener.java    From biomedicus with Apache License 2.0 5 votes vote down vote up
@Override
public void wroteToDestination(String destinationName,
    int destinationIndex,
    TextRange originalDocumentTextLocation) {
  AnnotationFS viewIndex = originalDocumentView
      .createAnnotation(viewIndexType, originalDocumentTextLocation.getStartIndex(),
          originalDocumentTextLocation.getEndIndex());
  viewIndex.setStringValue(destinationNameFeature, destinationName);
  viewIndex.setIntValue(destinationIndexFeature, destinationIndex);
  originalDocumentView.addFsToIndexes(viewIndex);
}
 
Example 4
Source File: LowerRightAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Lower right annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void lowerRightAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  
  Type annotationType = annotation.getType();
  Feature endFeature = annotationType.getFeatureByBaseName("end");
  
  if (annotation.getBegin() < annotation.getEnd()) {
    annotation.setIntValue(endFeature, annotation.getEnd() - 1);
  }
  
  document.update(annotation);
}
 
Example 5
Source File: WideLeftAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Widens the annotation and sends and sends an update notification
 * to the provided document.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void wideLeftAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature beginFeature = annotationType.getFeatureByBaseName("begin");

  if (annotation.getBegin() > 0) {
    annotation.setIntValue(beginFeature, annotation.getBegin() - 1);
  }

  document.update(annotation);
}
 
Example 6
Source File: WideRightAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Wide right annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void wideRightAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature endFeature = annotationType.getFeatureByBaseName("end");
  
  if (annotation.getEnd() < document.getCAS().getDocumentText().length()) {
    annotation.setIntValue(endFeature, annotation.getEnd() + 1);
  }
  
  document.update(annotation);
}
 
Example 7
Source File: LowerLeftAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Lower left annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void lowerLeftAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature beginFeature = annotationType.getFeatureByBaseName("begin");

  if (annotation.getBegin() < annotation.getEnd()) {
    annotation.setIntValue(beginFeature, annotation.getBegin() + 1);
  }

  document.update(annotation);
}
 
Example 8
Source File: CasOutputDestination.java    From biomedicus with Apache License 2.0 4 votes vote down vote up
@Override
public int writeChar(char ch, State state) {
  for (AnnotationPropertyWatcher propertyWatcher : annotationPropertyWatchers) {
    AnnotationFS newAnnotation = propertyWatcher
        .handleChanges(sofaBuilder.length(), state, destinationView);
    if (newAnnotation != null) {
      completedAnnotations.add(newAnnotation);
    }
  }

  if (state.getPropertyValue("CharacterFormatting", "Hidden") == 0
      && (writeTables || state.getPropertyValue("ParagraphFormatting", "InTable") == 0)) {
    if (!isValidXml(ch)) {
      // add zero-width space and annotate it as an illegal xml character.
      sofaBuilder.append((char) 0x200B);
      AnnotationFS annotation = destinationView.createAnnotation(
          illegalCharType,
          sofaBuilder.length() - 1,
          sofaBuilder.length()
      );
      annotation.setIntValue(valueFeat, (int) ch);
      completedAnnotations.add(annotation);
    } else {
      int superSub = state.getPropertyValue("CharacterFormatting", "SuperSub");
      if (superSub > 0) {
        if (!inSuperSub) {
          inSuperSub = true;
          if (Character.isDigit(sofaBuilder.charAt(sofaBuilder.length() - 1))
              && Character.isDigit(ch)) {
            sofaBuilder.append('^');
          }
          sofaBuilder.append('(');
        }
      } else {
        if (inSuperSub) {
          sofaBuilder.append(')');
          inSuperSub = false;
        }
      }

      sofaBuilder.append(ch);
    }
    return sofaBuilder.length() - 1;
  } else {
    return -1;
  }
}
 
Example 9
Source File: WebannoTsv3Reader.java    From webanno with Apache License 2.0 4 votes vote down vote up
private int addAnnotationWithNoFeature(JCas aJCas, Type aType, AnnotationUnit aUnit,
        List<AnnotationFS> aAnnos,
        Map<AnnotationUnit, Map<Integer, AnnotationFS>> aMultiTokUnits, int aEnd, int aRef)
{
    String anno = annotationsPerPostion.get(aType).get(aUnit).get(0);
    if (!anno.equals("_")) {
        int i = 0;
        String stackedAnnoRegex = "(?<!\\\\)" + Pattern.quote("|");
        for (String mAnnos : anno.split(stackedAnnoRegex)) {
            String multipleSlotAnno = "(?<!\\\\)" + Pattern.quote(";");
            for (String mAnno : mAnnos.split(multipleSlotAnno)) {
                String depRef = "";
                if (mAnno.endsWith("]")) {
                    depRef = mAnno.substring(mAnno.indexOf("[") + 1, mAnno.length() - 1);
                    aRef = depRef.contains("_") ? 0
                            : Integer.valueOf(mAnno.substring(mAnno.indexOf("[") + 1,
                                    mAnno.length() - 1));
                    mAnno = mAnno.substring(0, mAnno.indexOf("["));
                }

                boolean isMultitoken = false;
                AnnotationFS multiAnnoFs = null;

                if (!aMultiTokUnits.isEmpty()) {
                    for (AnnotationUnit u : aMultiTokUnits.keySet()) {
                        for (Integer r : aMultiTokUnits.get(u).keySet()) {
                            if (aRef == r) {
                                isMultitoken = true;
                                multiAnnoFs = aMultiTokUnits.get(u).get(r);
                                break;
                            }
                        }
                    }
                }

                if (isMultitoken) {

                    Feature endF = aType.getFeatureByBaseName(CAS.FEATURE_BASE_NAME_END);
                    multiAnnoFs.getCAS().removeFsFromIndexes(multiAnnoFs);
                    multiAnnoFs.setIntValue(endF, aEnd);
                    multiAnnoFs.getCAS().addFsToIndexes(multiAnnoFs);
                    setAnnoRefPerUnit(aUnit, aType, aRef, multiAnnoFs);

                }
                else {

                    aMultiTokUnits.putIfAbsent(aUnit, new HashMap<>());
                    aMultiTokUnits.get(aUnit).put(aRef, aAnnos.get(i));
                    aJCas.addFsToIndexes(aAnnos.get(i));
                    setAnnoRefPerUnit(aUnit, aType, aRef, aAnnos.get(i));
                }
                aRef++;
            }
            i++;
        }
    }
    return aRef;
}
 
Example 10
Source File: IndexSerializationTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Test driver.
 */
public void testMain() throws Exception {

  for (int i = 0; i < 10; i++) {
    cas.getIndexRepository().addFS(cas.createAnnotation(annotationType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(sentenceType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
  }
  for (int i = 19; i >= 10; i--) {
    cas.getIndexRepository().addFS(cas.createAnnotation(annotationType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(sentenceType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
  }

  AnnotationFS searchAnnot = cas.createAnnotation(annotationType, 0, 1);
  assertTrue(cas.getAnnotationIndex().find(searchAnnot) != null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).find(searchAnnot) != null);
  // find() does not produce useful results on bag indexes, since the comparator
  // is not defined.
  // assertTrue(cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).find(searchAnnot) != null);

  searchAnnot.setIntValue(endFeature, 4);
  assertTrue(cas.getAnnotationIndex().find(searchAnnot) == null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).find(searchAnnot) == null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).find(searchAnnot) == null);

  final int ordSize = cas.getAnnotationIndex().size();
  final int setSize = cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).size();
  final int bagSize = cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).size();
  // System.out.println("Before serialization\n");
  // System.out.println("Size of ordered index: " + ordSize);
  // System.out.println("Size of set index: " + setSize);
  // System.out.println("Size of bag index: " + bagSize);

  CASCompleteSerializer cs;
  cs = Serialization.serializeCASComplete(casMgr);
  // casMgr = CASFactory.createCAS();
  CASMgr realCasMgr = CASFactory.createCAS();  // creates base view, but no ts, so no ir
  ((CASImpl) realCasMgr).commitTypeSystem();   // also makes index repo (which will be replaced), but doesn't init the built-in indexes
  Serialization.deserializeCASComplete(cs, realCasMgr);
  cas = ((CASImpl) realCasMgr).getCurrentView();
  casMgr = (CASMgr) cas;

  // System.out.println("After serialization\n");
  FSIndex<? extends FeatureStructure> index = cas.getAnnotationIndex();
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.SORTED_INDEX);
  // System.out.println("Size of ordered index: " + index.size());
  assertTrue(index.size() == ordSize);

  index = cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX);
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.BAG_INDEX);
  // System.out.println("Size of bag index: " + index.size());
  assertTrue(index.size() == bagSize);

  index = cas.getIndexRepository().getIndex(ANNOT_SET_INDEX);
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.SET_INDEX);
  // System.out.println("Size of set index: " + index.size());
  // System.out.println("Should be: " + setSize);
  assertTrue(index.size() == setSize);

}