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

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#setFeatureValue() . 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: RelationAdapter.java    From webanno with Apache License 2.0 6 votes vote down vote up
private AnnotationFS createRelationAnnotation(CAS cas, AnnotationFS originFS,
        AnnotationFS targetFS)
    throws AnnotationException
{
    if (targetFS == null || originFS == null) {
        throw new IllegalPlacementException("Relation must have a source and a target!");
    }

    // Set the relation offsets in DKPro Core style - the relation recieves the offsets from
    // the dependent
    // If origin and target spans are multiple tokens, dependentFS.getBegin will be the
    // the begin position of the first token and dependentFS.getEnd will be the End
    // position of the last token.
    final Type type = getType(cas, getLayer().getName());
    final Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
    final Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);

    AnnotationFS newAnnotation = cas.createAnnotation(type, targetFS.getBegin(),
            targetFS.getEnd());
    newAnnotation.setFeatureValue(dependentFeature, targetFS);
    newAnnotation.setFeatureValue(governorFeature, originFS);
    cas.addFsToIndexes(newAnnotation);
    return newAnnotation;
}
 
Example 2
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void clearAnnotations(CAS aCas, AnnotationFeature aFeature)
    throws IOException
{
    // Check if annotation layer is attached to another layer
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature
            .getLayer().getAttachType().getName();
    Type attachType = null;
    Feature attachFeature = null;
    if (attachTypeName != null) {
        attachType = CasUtil.getType(aCas, attachTypeName);
        attachFeature = attachType
                .getFeatureByBaseName(aFeature.getLayer().getAttachFeature().getName());
    }
    
    List<AnnotationFS> annotationsToRemove = new ArrayList<>();
    Type type = CasUtil.getType(aCas, aFeature.getLayer().getName());
    annotationsToRemove.addAll(select(aCas, type));
    
    for (AnnotationFS annotation : annotationsToRemove) {
        if (attachFeature != null) {
            // Unattach the annotation to be removed
            for (AnnotationFS attach : selectCovered(attachType, annotation)) {
                FeatureStructure existing = attach.getFeatureValue(attachFeature);
                if (annotation.equals(existing)) {
                    attach.setFeatureValue(attachFeature, null);
                }
            }
        }
        aCas.removeFsFromIndexes(annotation);
    }
}
 
Example 3
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static AnnotationFS makeLinkHostMultiSPanFeatureFS(JCas aCas, int aBegin, int aEnd,
        Feature aSpanFeature, String aValue, FeatureStructure... aLinks)
{
    Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
    AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
            FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
    hostA1.setStringValue(aSpanFeature, aValue);
    aCas.getCas().addFsToIndexes(hostA1);
    return hostA1;
}
 
Example 4
Source File: AgreementTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void makeLinkHostFS(JCas aCas, int aBegin, int aEnd, FeatureStructure... aLinks)
{
    Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
    AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
            FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
    aCas.getCas().addFsToIndexes(hostA1);
}
 
Example 5
Source File: DiffTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static AnnotationFS makeLinkHostMultiSPanFeatureFS(JCas aCas, int aBegin, int aEnd,
        Feature aSpanFeature, String aValue, FeatureStructure... aLinks)
{
    Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
    AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
            FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
    hostA1.setStringValue(aSpanFeature, aValue);
    aCas.getCas().addFsToIndexes(hostA1);
    return hostA1;
}
 
Example 6
Source File: DiffTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void makeLinkHostFS(JCas aCas, int aBegin, int aEnd, FeatureStructure... aLinks)
{
    Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
    AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
            FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
    aCas.getCas().addFsToIndexes(hostA1);
}
 
Example 7
Source File: CurationTestUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static AnnotationFS makeLinkHostMultiSPanFeatureFS(JCas aCas, int aBegin, int aEnd,
        Feature aSpanFeature, String aValue, FeatureStructure... aLinks)
{
    Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
    AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
            FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
    hostA1.setStringValue(aSpanFeature, aValue);
    aCas.getCas().addFsToIndexes(hostA1);
    return hostA1;
}
 
Example 8
Source File: AllAnnotationsIndexedCheckTest.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Test
public void testOK()
    throws Exception
{
    TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory()
            .createTypeSystemDescription();
    
    String refTypeName = "RefType";
    
    TypeDescription refTypeDesc = tsd.addType(refTypeName, null, CAS.TYPE_NAME_ANNOTATION);
    refTypeDesc.addFeature("ref", null, CAS.TYPE_NAME_ANNOTATION);
    
    CAS cas = CasCreationUtils.createCas(tsd, null, null);
    
    Type refType = cas.getTypeSystem().getType(refTypeName);
    
    // A regular index annotation
    AnnotationFS anno1 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
    cas.addFsToIndexes(anno1);

    // An indexed annotation but reachable through an indexe one (below)
    AnnotationFS anno2 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
    cas.addFsToIndexes(anno2);

    // An indexed annotation that references the non-indexed annotation above
    AnnotationFS anno3 = cas.createAnnotation(refType, 0, 1);
    anno3.setFeatureValue(refType.getFeatureByBaseName("ref"), anno2);
    cas.addFsToIndexes(anno3);
    
    List<LogMessage> messages = new ArrayList<>();
    CasDoctor cd = new CasDoctor(AllFeatureStructuresIndexedCheck.class);
    // A project is not required for this check
    boolean result = cd.analyze(null, cas, messages);
    
    messages.forEach(System.out::println);
    
    assertTrue(result);
}
 
Example 9
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static AnnotationFS makeLinkHostFS(JCas aJCas, String aType, int aBegin, int aEnd,
        FeatureStructure... aLinks)
{
    Type hostType = aJCas.getTypeSystem().getType(aType);
    AnnotationFS hostA1 = aJCas.getCas().createAnnotation(hostType, aBegin, aEnd);
    if (aLinks != null) {
        hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
                FSCollectionFactory.createFSArray(aJCas, asList(aLinks)));
    }
    aJCas.getCas().addFsToIndexes(hostA1);
    return hostA1;
}
 
Example 10
Source File: WebannoTsv3Reader.java    From webanno with Apache License 2.0 5 votes vote down vote up
/**
 * update a base annotation with slot annotations
 * 
 * @param linkFSesPerAnno
 *            contains list of slot annotations per a base annotation
 * @param aLinkeF
 *            The link slot annotation feature
 */
private void addSlotAnnotations(Map<AnnotationFS, List<FeatureStructure>> linkFSesPerAnno,
        Feature aLinkeF)
{
    for (AnnotationFS anno : linkFSesPerAnno.keySet()) {
        ArrayFS array = anno.getCAS().createArrayFS(linkFSesPerAnno.get(anno).size());
        array.copyFromArray(
                linkFSesPerAnno.get(anno)
                        .toArray(new FeatureStructure[linkFSesPerAnno.get(anno).size()]),
                0, 0, linkFSesPerAnno.get(anno).size());
        anno.setFeatureValue(aLinkeF, array);
        anno.getCAS().addFsToIndexes(anno);
    }
}
 
Example 11
Source File: TransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  engTcas = aCas.getView(aCas.getSofa(realSofaName));
  // if (engTcas == null)
  // System.out.println(realSofaName + " sofa not found in CAS");

  realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 12
Source File: MultiViewAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  engTcas = aCas;

  // Create the output German text Sofa and open CAS view
  germTcas = aCas.createView("GermanDocument");

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 13
Source File: WebannoTsv2Reader.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a relation layer. For every token, store the governor positions and the dependent
 * annotation
 */

private void createRelationLayer(JCas aJcas, Map<Type, Type> relationayers,
        Map<Type, Map<String, List<AnnotationFS>>> tokenAnnotations,
        Map<Type, Map<String, List<String>>> relationTargets)
{

    for (Type layer : relationayers.keySet()) {

        if (relationTargets.get(layer) == null) {
            continue;
        }
        Feature dependentFeature = layer.getFeatureByBaseName("Dependent");
        Feature governorFeature = layer.getFeatureByBaseName("Governor");

        Map<String, List<String>> tokenIdMaps = relationTargets.get(layer);
        Map<String, List<AnnotationFS>> tokenAnnos = tokenAnnotations
                .get(relationayers.get(layer));
        Map<String, List<AnnotationFS>> relationAnnos = tokenAnnotations.get(layer);
        for (String dependnetId : tokenIdMaps.keySet()) {
            int i = 0;
            for (String governorId : tokenIdMaps.get(dependnetId)) {

                AnnotationFS relationAnno = relationAnnos.get(dependnetId).get(i);
                AnnotationFS dependentAnno = tokenAnnos.get(dependnetId).get(0);
                AnnotationFS governorAnno = tokenAnnos.get(governorId).get(0);

                if (layer.getName().equals(Dependency.class.getName())) {
                    Type tokenType = getType(aJcas.getCas(), Token.class.getName());
                    Feature attachFeature = tokenType.getFeatureByBaseName("pos");
                    AnnotationFS posDependentAnno = dependentAnno;
                    dependentAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType,
                            dependentAnno.getBegin(), dependentAnno.getEnd()).get(0);
                    dependentAnno.setFeatureValue(attachFeature, posDependentAnno);

                    AnnotationFS posGovernorAnno = governorAnno;
                    governorAnno = CasUtil.selectCovered(aJcas.getCas(), tokenType,
                            governorAnno.getBegin(), governorAnno.getEnd()).get(0);
                    governorAnno.setFeatureValue(attachFeature, posGovernorAnno);
                }
                
                // update begin/end of relation annotation
                relationAnno.getCAS().removeFsFromIndexes(relationAnno);
                if (dependentAnno.getEnd() <= governorAnno.getEnd()) {
                    ((Annotation) relationAnno).setBegin(dependentAnno.getBegin());
                    ((Annotation) relationAnno).setEnd(governorAnno.getEnd());
                }
                else {
                    ((Annotation) relationAnno).setBegin(governorAnno.getBegin());
                    ((Annotation) relationAnno).setEnd(dependentAnno.getEnd());
                }
                relationAnno.getCAS().addFsToIndexes(relationAnno);

                relationAnno.setFeatureValue(dependentFeature, dependentAnno);
                relationAnno.setFeatureValue(governorFeature, governorAnno);
                
                relationAnno.getCAS().addFsToIndexes(relationAnno);
                i++;
            }

        }
    }
}
 
Example 14
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private FeatureStructure createExampleFS(CAS parmCas) throws Exception {
  // Create a view
  CAS englishView = parmCas.createView("EnglishDocument");
  // Set the document text
  englishView.setDocumentText("this beer is good");

  // create an FS of exampleType and index it
  AnnotationFS fs = englishView.createAnnotation(exampleType, 1, 5);

  // create Array FSs
  StringArrayFS strArrayFS = parmCas.createStringArrayFS(5);
  strArrayFS.set(0, "zzzzzz");
  strArrayFS.set(1, "yyyyyy");
  strArrayFS.set(2, "xxxxxx");
  strArrayFS.set(3, "wwwwww");
  strArrayFS.set(4, "vvvvvv");

  IntArrayFS intArrayFS = parmCas.createIntArrayFS(5);
  intArrayFS.set(0, Integer.MAX_VALUE);
  intArrayFS.set(1, Integer.MAX_VALUE - 1);
  intArrayFS.set(2, 42);
  intArrayFS.set(3, Integer.MIN_VALUE + 1);
  intArrayFS.set(4, Integer.MIN_VALUE);

  FloatArrayFS floatArrayFS = parmCas.createFloatArrayFS(5);
  floatArrayFS.set(0, Float.MAX_VALUE);
  floatArrayFS.set(1, (float) (Float.MAX_VALUE / 1000.0));
  floatArrayFS.set(2, 42);
  floatArrayFS.set(3, (float) (Float.MIN_VALUE * 1000.0));
  floatArrayFS.set(4, Float.MIN_VALUE);

  ByteArrayFS byteArrayFS = parmCas.createByteArrayFS(5);
  byteArrayFS.set(0, (byte) 8);
  byteArrayFS.set(1, (byte) 16);
  byteArrayFS.set(2, (byte) 64);
  byteArrayFS.set(3, (byte) 128);
  byteArrayFS.set(4, (byte) 255);

  BooleanArrayFS boolArrayFS = parmCas.createBooleanArrayFS(20);
  boolean val = false;
  for (int i = 0; i < 20; i++) {
    boolArrayFS.set(i, val = !val);
  }

  ShortArrayFS shortArrayFS = parmCas.createShortArrayFS(5);
  shortArrayFS.set(0, Short.MAX_VALUE);
  shortArrayFS.set(1, (short) (Short.MAX_VALUE - 1));
  shortArrayFS.set(2, (short) (Short.MAX_VALUE - 2));
  shortArrayFS.set(3, (short) (Short.MAX_VALUE - 3));
  shortArrayFS.set(4, (short) (Short.MAX_VALUE - 4));

  LongArrayFS longArrayFS = parmCas.createLongArrayFS(5);
  longArrayFS.set(0, Long.MAX_VALUE);
  longArrayFS.set(1, Long.MAX_VALUE - 1);
  longArrayFS.set(2, Long.MAX_VALUE - 2);
  longArrayFS.set(3, Long.MAX_VALUE - 3);
  longArrayFS.set(4, Long.MAX_VALUE - 4);

  DoubleArrayFS doubleArrayFS = parmCas.createDoubleArrayFS(5);
  doubleArrayFS.set(0, Double.MAX_VALUE);
  doubleArrayFS.set(1, Double.MIN_VALUE);
  doubleArrayFS.set(2, Double.parseDouble("1.5555"));
  doubleArrayFS.set(3, Double.parseDouble("99.000000005"));
  doubleArrayFS.set(4, Double.parseDouble("4.44444444444444444"));

  // set features of fs
  fs.setStringValue(stringFeature, "aaaaaaa");
  fs.setFloatValue(floatFeature, (float) 99.99);

  fs.setFeatureValue(intArrayFeature, intArrayFS);
  fs.setFeatureValue(floatArrayFeature, floatArrayFS);
  fs.setFeatureValue(stringArrayFeature, strArrayFS);

  // fs.setByteValue(byteFeature, Byte.MAX_VALUE);
  fs.setByteValue(byteFeature, (byte) 'z');
  fs.setFeatureValue(byteArrayFeature, byteArrayFS);
  fs.setBooleanValue(booleanFeature, true);
  fs.setFeatureValue(booleanArrayFeature, boolArrayFS);
  fs.setShortValue(shortFeature, Short.MIN_VALUE);
  fs.setFeatureValue(shortArrayFeature, shortArrayFS);
  fs.setLongValue(longFeature, Long.MIN_VALUE);
  fs.setFeatureValue(longArrayFeature, longArrayFS);
  fs.setDoubleValue(doubleFeature, Double.MAX_VALUE);
  fs.setFeatureValue(doubleArrayFeature, doubleArrayFS);
  
  englishView.getIndexRepository().addFS(fs);
  return fs;
}
 
Example 15
Source File: TcasTransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  // SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  // engTcas = aCas.getView(aCas.getSofa(realSofaName));
  engTcas = aCas;
  // if (engTcas == null )
  // System.out.println(realSofaName + " sofa not found in CAS");

  SofaID realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 16
Source File: SofaExampleAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas) throws AnalysisEngineProcessException {
  CAS englishView, germanView;

  // get the CAS view for the English document
  englishView = aCas.getView("EnglishDocument");

  // Create the German text Sofa and open its view
  germanView = aCas.createView("GermanDocument");

  // Get some necessary Type System constants
  Type annot = englishView.getAnnotationType();
  Type cross = englishView.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = englishView.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = englishView.createAnnotation(annot, engBegin, engEnd);
    englishView.addFsToIndexes(engAnnot);

    // Simple word-by-word translation
    String germWord = translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord);

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germanView.createAnnotation(cross, germBegin, germEnd);
    germanView.addFsToIndexes(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germanView.setDocumentText(translation.toString());

}
 
Example 17
Source File: CasMergeTest.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Test
public void simpleCopyRelationToEmptyAnnoTest()
    throws Exception
{
    CAS jcas = createJCas().getCas();
    Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());

    AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
    AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);

    AnnotationFS originClicked = createPOSAnno(jcas, "NN", 0, 0);
    AnnotationFS targetClicked = createPOSAnno(jcas, "NN", 1, 1);

    jcas.addFsToIndexes(originClicked);
    jcas.addFsToIndexes(targetClicked);

    originClickedToken.setFeatureValue(originClickedToken.getType().getFeatureByBaseName("pos"),
            originClicked);
    targetClickedToken.setFeatureValue(targetClickedToken.getType().getFeatureByBaseName("pos"),
            targetClicked);

    Feature sourceFeature = type.getFeatureByBaseName(FEAT_REL_SOURCE);
    Feature targetFeature = type.getFeatureByBaseName(FEAT_REL_TARGET);

    AnnotationFS clickedFs = jcas.createAnnotation(type, 0, 1);
    clickedFs.setFeatureValue(sourceFeature, originClickedToken);
    clickedFs.setFeatureValue(targetFeature, targetClickedToken);
    jcas.addFsToIndexes(clickedFs);

    CAS mergeCAs = createJCas().getCas();
    AnnotationFS origin = createPOSAnno(mergeCAs, "NN", 0, 0);
    AnnotationFS target = createPOSAnno(mergeCAs, "NN", 1, 1);

    mergeCAs.addFsToIndexes(origin);
    mergeCAs.addFsToIndexes(target);

    AnnotationFS originToken = createTokenAnno(mergeCAs, 0, 0);
    AnnotationFS targetToken = createTokenAnno(mergeCAs, 1, 1);
    originToken.setFeatureValue(originToken.getType().getFeatureByBaseName("pos"), origin);
    targetToken.setFeatureValue(targetToken.getType().getFeatureByBaseName("pos"), target);

    mergeCAs.addFsToIndexes(originToken);
    mergeCAs.addFsToIndexes(targetToken);

    sut.mergeRelationAnnotation(null, null, depLayer, mergeCAs, clickedFs, false);
    
    assertEquals(1, selectCovered(mergeCAs, type, 0, 1).size());
}
 
Example 18
Source File: TransAnnotator.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void process(CAS aCas, ResultSpecification aResultSpec) throws AnnotatorProcessException {
  CAS engTcas, germTcas;

  // get English text Sofa and open CAS view
  SofaID realSofaName = getContext().mapToSofaID("EnglishDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name for English document " +
  // realSofaName.getSofaID());

  engTcas = aCas.getView(aCas.getSofa(realSofaName));
  // if (engTcas == null)
  // System.out.println(realSofaName + " sofa not found in CAS");

  realSofaName = getContext().mapToSofaID("GermanDocument");
  // System.out.println("TRANSANNOTATOR: real sofa name of GermanDocument " +
  // realSofaName.getSofaID());
  // Create the output German text Sofa and open CAS view
  germTcas = aCas.getView(aCas.createSofa(realSofaName, "text"));

  // Get some necessary Type System constants
  Type annot = engTcas.getAnnotationType();
  Type cross = engTcas.getTypeSystem().getType("sofa.test.CrossAnnotation");
  Feature other = cross.getFeatureByBaseName("otherAnnotation");

  // Get the English text
  String engText = engTcas.getDocumentText();

  // Setup for translated text
  int engEnd = 0;
  int germBegin = 0;
  int germEnd = 0;
  StringBuffer translation = new StringBuffer();

  // Parse the English text
  StringTokenizer st = new StringTokenizer(engText);
  while (st.hasMoreTokens()) {
    String thisTok = st.nextToken();
    int engBegin = engText.indexOf(thisTok, engEnd);
    engEnd = engBegin + thisTok.length();

    // Create token annotations on English text
    AnnotationFS engAnnot = engTcas.createAnnotation(annot, engBegin, engEnd);
    engTcas.getIndexRepository().addFS(engAnnot);

    // Simple word-by-word translation
    String germWord = Translate(thisTok);

    // Accumulate the translated text
    if (germBegin > 0) {
      translation.append(' ');
      germBegin += 1;
    }
    translation.append(germWord.toCharArray(), 0, germWord.length());

    // Create token annotations on German text
    germEnd = germBegin + germWord.length();
    AnnotationFS germAnnot = germTcas.createAnnotation(cross, germBegin, germEnd);
    germTcas.getIndexRepository().addFS(germAnnot);

    // add link to English text
    germAnnot.setFeatureValue(other, engAnnot);
    germBegin = germEnd;
  }

  // Finally, set the output tranlation Sofa data
  germTcas.setDocumentText(translation.toString());

}
 
Example 19
Source File: CasAnnotationViewerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void createExampleFS(CAS cas) throws Exception {
  // Set the document text
  cas.setDocumentText("this beer is good");

  // create an FS of exampleType and index it
  AnnotationFS fs = cas.createAnnotation(exampleType, 1, 5);
  cas.getIndexRepository().addFS(fs);

  // create Array FSs
  StringArrayFS strArrayFS = cas.createStringArrayFS(5);
  strArrayFS.set(0, "zzzzzz");
  strArrayFS.set(1, "yyyyyy");
  strArrayFS.set(2, "xxxxxx");
  strArrayFS.set(3, "wwwwww");
  strArrayFS.set(4, "vvvvvv");

  IntArrayFS intArrayFS = cas.createIntArrayFS(5);
  intArrayFS.set(0, Integer.MAX_VALUE);
  intArrayFS.set(1, Integer.MAX_VALUE - 1);
  intArrayFS.set(2, 42);
  intArrayFS.set(3, Integer.MIN_VALUE + 1);
  intArrayFS.set(4, Integer.MIN_VALUE);

  FloatArrayFS floatArrayFS = cas.createFloatArrayFS(5);
  floatArrayFS.set(0, Float.MAX_VALUE);
  floatArrayFS.set(1, (float) (Float.MAX_VALUE / 1000.0));
  floatArrayFS.set(2, (float) 42);
  floatArrayFS.set(3, (float) (Float.MIN_VALUE * 1000.0));
  floatArrayFS.set(4, Float.MIN_VALUE);

  ByteArrayFS byteArrayFS = cas.createByteArrayFS(5);
  byteArrayFS.set(0, (byte) 8);
  byteArrayFS.set(1, (byte) 16);
  byteArrayFS.set(2, (byte) 64);
  byteArrayFS.set(3, (byte) 128);
  byteArrayFS.set(4, (byte) 255);

  BooleanArrayFS boolArrayFS = cas.createBooleanArrayFS(8);
  boolean val = false;
  for (int i = 0; i < 8; i++) {
    boolArrayFS.set(i, val = !val);
  }

  ShortArrayFS shortArrayFS = cas.createShortArrayFS(5);
  shortArrayFS.set(0, Short.MAX_VALUE);
  shortArrayFS.set(1, (short) (Short.MAX_VALUE - 1));
  shortArrayFS.set(2, (short) (Short.MAX_VALUE - 2));
  shortArrayFS.set(3, (short) (Short.MAX_VALUE - 3));
  shortArrayFS.set(4, (short) (Short.MAX_VALUE - 4));

  LongArrayFS longArrayFS = cas.createLongArrayFS(5);
  longArrayFS.set(0, Long.MAX_VALUE);
  longArrayFS.set(1, Long.MAX_VALUE - 1);
  longArrayFS.set(2, Long.MAX_VALUE - 2);
  longArrayFS.set(3, Long.MAX_VALUE - 3);
  longArrayFS.set(4, Long.MAX_VALUE - 4);

  DoubleArrayFS doubleArrayFS = cas.createDoubleArrayFS(5);
  doubleArrayFS.set(0, Double.MAX_VALUE);
  doubleArrayFS.set(1, Double.MIN_VALUE);
  doubleArrayFS.set(2, Double.parseDouble("1.5555"));
  doubleArrayFS.set(3, Double.parseDouble("99.000000005"));
  doubleArrayFS.set(4, Double.parseDouble("4.44444444444444444"));

  // set features of fs
  fs.setStringValue(stringFeature, "aaaaaaa");
  fs.setFloatValue(floatFeature, (float) 99.99);

  fs.setFeatureValue(intArrayFeature, intArrayFS);
  fs.setFeatureValue(floatArrayFeature, floatArrayFS);
  fs.setFeatureValue(stringArrayFeature, strArrayFS);

  // fs.setByteValue(byteFeature, Byte.MAX_VALUE);
  fs.setByteValue(byteFeature, (byte) 'z');
  fs.setFeatureValue(byteArrayFeature, byteArrayFS);
  fs.setBooleanValue(booleanFeature, true);
  fs.setFeatureValue(booleanArrayFeature, boolArrayFS);
  fs.setShortValue(shortFeature, Short.MIN_VALUE);
  fs.setFeatureValue(shortArrayFeature, shortArrayFS);
  fs.setLongValue(longFeature, Long.MIN_VALUE);
  fs.setFeatureValue(longArrayFeature, longArrayFS);
  fs.setDoubleValue(doubleFeature, Double.MAX_VALUE);
  fs.setFeatureValue(doubleArrayFeature, doubleArrayFS);

  cas.getIndexRepository().addFS(fs);
}
 
Example 20
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testOutOfTypeSystemArrayElement() throws Exception {
  //add to type system an annotation type that has an FSArray feature
  TypeDescription testAnnotTypeDesc = typeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation");
  testAnnotTypeDesc.addFeature("arrayFeat", "", "uima.cas.FSArray");
  //populate a CAS with such an array
  CAS cas = CasCreationUtils.createCas(typeSystem, null, null);
  Type testAnnotType = cas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation");
  Type orgType = cas.getTypeSystem().getType(
    "org.apache.uima.testTypeSystem.Organization");
  AnnotationFS orgAnnot1 = cas.createAnnotation(orgType, 0, 10);
  cas.addFsToIndexes(orgAnnot1);
  AnnotationFS orgAnnot2 = cas.createAnnotation(orgType, 10, 20);
  cas.addFsToIndexes(orgAnnot2);
  AnnotationFS testAnnot = cas.createAnnotation(testAnnotType, 0, 20);
  cas.addFsToIndexes(testAnnot);
  ArrayFS arrayFs = cas.createArrayFS(2);
  arrayFs.set(0, orgAnnot1);
  arrayFs.set(1, orgAnnot2);
  Feature arrayFeat = testAnnotType.getFeatureByBaseName("arrayFeat");
  testAnnot.setFeatureValue(arrayFeat, arrayFs);
  
  //serialize to XMI
  String xmiStr = serialize(cas, null);
  
  //deserialize into a CAS that's missing the Organization type
  File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml");
  TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
          new XMLInputSource(partialTypeSystemFile));
  testAnnotTypeDesc = partialTypeSystem.addType("org.apache.uima.testTypeSystem.TestAnnotation", "", "uima.tcas.Annotation");
  testAnnotTypeDesc.addFeature("arrayFeat", "", "uima.cas.FSArray");
  CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, null);
  XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
  deserialize(xmiStr, partialTsCas, sharedData, true, -1);
  
  //check out of type system data
  Type testAnnotType2 = partialTsCas.getTypeSystem().getType("org.apache.uima.testTypeSystem.TestAnnotation");
  FeatureStructure testAnnot2 = partialTsCas.getAnnotationIndex(testAnnotType2).iterator().get(); 
  Feature arrayFeat2 = testAnnotType2.getFeatureByBaseName("arrayFeat");
  FeatureStructure arrayFs2 = testAnnot2.getFeatureValue(arrayFeat2);
  List ootsElems = sharedData.getOutOfTypeSystemElements();
  assertEquals(2, ootsElems.size());
  List ootsArrayElems = sharedData.getOutOfTypeSystemArrayElements((FSArray) arrayFs2);
  assertEquals(2, ootsArrayElems.size());
  for (int i = 0; i < 2; i++) {
    OotsElementData oed = (OotsElementData)ootsElems.get(i);
    XmiArrayElement arel = (XmiArrayElement)ootsArrayElems.get(i);
    assertEquals(oed.xmiId, arel.xmiId);      
  }
  
  //reserialize along with out of type system data
  String xmiStr2 = serialize(partialTsCas, sharedData);
  
  //deserialize into a new CAS and compare
  CAS cas2 = CasCreationUtils.createCas(typeSystem, null, null);
  deserialize(xmiStr2, cas2, null, false, -1);
  
  CasComparer.assertEquals(cas, cas2);    
}