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

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#setBooleanValue() . 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: StringMatchingRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    Trie<DictEntry> dict = aContext.get(KEY_MODEL).orElseThrow(() -> 
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));

    Type predictedType = getPredictedType(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);
    Feature scoreFeature = getScoreFeature(aCas);

    List<Sample> data = predict(0, aCas, dict);
    
    for (Sample sample : data) {
        for (Span span : sample.getSpans()) {
            AnnotationFS annotation = aCas.createAnnotation(predictedType, span.getBegin(),
                    span.getEnd());
            annotation.setStringValue(predictedFeature, span.getLabel());
            annotation.setDoubleValue(scoreFeature, span.getScore());
            annotation.setBooleanValue(isPredictionFeature, true);
            aCas.addFsToIndexes(annotation);
        }
    }
}
 
Example 2
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 3
Source File: DataMajorityNerRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    DataMajorityModel model = aContext.get(KEY_MODEL).orElseThrow(() ->
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));

    // Make the predictions
    Type tokenType = CasUtil.getAnnotationType(aCas, Token.class);
    Collection<AnnotationFS> candidates = CasUtil.select(aCas, tokenType);
    List<Annotation> predictions = predict(candidates, model);

    // Add predictions to the CAS
    Type predictedType = getPredictedType(aCas);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature scoreExplanationFeature = getScoreExplanationFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);

    for (Annotation ann : predictions) {
        AnnotationFS annotation = aCas.createAnnotation(predictedType, ann.begin, ann.end);
        annotation.setStringValue(predictedFeature, ann.label);
        annotation.setDoubleValue(scoreFeature, ann.score);
        annotation.setStringValue(scoreExplanationFeature, ann.explanation);
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}
 
Example 4
Source File: OpenNlpDoccatRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    DoccatModel model = aContext.get(KEY_MODEL).orElseThrow(() -> 
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
    
    DocumentCategorizerME finder = new DocumentCategorizerME(model);

    Type sentenceType = getType(aCas, Sentence.class);
    Type predictedType = getPredictedType(aCas);
    Type tokenType = getType(aCas, Token.class);
    Feature scoreFeature = getScoreFeature(aCas);
    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);

    int predictionCount = 0;
    for (AnnotationFS sentence : select(aCas, sentenceType)) {
        if (predictionCount >= traits.getPredictionLimit()) {
            break;
        }
        predictionCount++;
        
        List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence);
        String[] tokens = tokenAnnotations.stream()
            .map(AnnotationFS::getCoveredText)
            .toArray(String[]::new);

        double[] outcome = finder.categorize(tokens);
        String label = finder.getBestCategory(outcome);
        
        AnnotationFS annotation = aCas.createAnnotation(predictedType, sentence.getBegin(),
                sentence.getEnd());
        annotation.setStringValue(predictedFeature, label);
        annotation.setDoubleValue(scoreFeature, NumberUtils.max(outcome));
        annotation.setBooleanValue(isPredictionFeature, true);
        aCas.addFsToIndexes(annotation);
    }
}
 
Example 5
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testClone() throws Exception {
  createExampleFS(cas);
  // get the example FS
  CAS englishView = cas.getView("EnglishDocument");
  FSIterator iter = englishView.getAnnotationIndex().iterator();
  // skip document annotation
  iter.moveToNext();
  // the exampleType fs
  AnnotationFS fs = (AnnotationFS) iter.get();

  // clone it
  AnnotationFS clone = (AnnotationFS) fs.clone();

  // substitute the clone for the original in the index,
  // and validate that it was correctly copied
  englishView.removeFsFromIndexes(fs);
  englishView.addFsToIndexes(clone);
  validateFSData(cas);

  // editing the original FS should not change the clone
  englishView.removeFsFromIndexes(fs);  // does nothing, is not in the index, the clone is
  fs.setStringValue(stringFeature, "foo");
  fs.setFloatValue(floatFeature, -1f);
  fs.setByteValue(byteFeature, (byte) -1);
  fs.setBooleanValue(booleanFeature, false);
  fs.setShortValue(shortFeature, (short) -1);
  fs.setLongValue(longFeature, -1);
  fs.setDoubleValue(doubleFeature, -1);
  fs.setBegin(clone.getBegin() + 1);  // to be sure that fs is beyond the original
  englishView.addFsToIndexes(fs);  // will add, is no longer "equal" to the clone
  validateFSData(cas);
}
 
Example 6
Source File: OpenNlpPosRecommender.java    From inception with Apache License 2.0 4 votes vote down vote up
@Override
    public void predict(RecommenderContext aContext, CAS aCas)
        throws RecommendationException
    {
        POSModel model = aContext.get(KEY_MODEL).orElseThrow(() -> 
                new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
        
        POSTaggerME tagger = new POSTaggerME(model);

        Type sentenceType = getType(aCas, Sentence.class);
        Type predictedType = getPredictedType(aCas);
        Type tokenType = getType(aCas, Token.class);

        Feature scoreFeature = getScoreFeature(aCas);
        Feature predictedFeature = getPredictedFeature(aCas);
        Feature isPredictionFeature = getIsPredictionFeature(aCas);

        int predictionCount = 0;
        for (AnnotationFS sentence : select(aCas, sentenceType)) {
            if (predictionCount >= traits.getPredictionLimit()) {
                break;
            }
            predictionCount++;
            
            List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence);
            String[] tokens = tokenAnnotations.stream()
                .map(AnnotationFS::getCoveredText)
                .toArray(String[]::new);

            Sequence[] bestSequences = tagger.topKSequences(tokens);

//            LOG.debug("Total number of sequences predicted: {}", bestSequences.length);

            for (int s = 0; s < Math.min(bestSequences.length, maxRecommendations); s++) {
                Sequence sequence = bestSequences[s];
                List<String> outcomes = sequence.getOutcomes();
                double[] probabilities = sequence.getProbs();

//                LOG.debug("Sequence {} score {}", s, sequence.getScore());
//                LOG.debug("Outcomes: {}", outcomes);
//                LOG.debug("Probabilities: {}", asList(probabilities));

                for (int i = 0; i < outcomes.size(); i++) {
                    String label = outcomes.get(i);

                    // Do not return PADded tokens
                    if (PAD.equals(label)) {
                        continue;
                    }

                    AnnotationFS token = tokenAnnotations.get(i);
                    int begin = token.getBegin();
                    int end = token.getEnd();
                    double confidence = probabilities[i];

                    // Create the prediction
                    AnnotationFS annotation = aCas.createAnnotation(predictedType, begin, end);
                    annotation.setStringValue(predictedFeature, label);
                    annotation.setDoubleValue(scoreFeature, confidence);
                    annotation.setBooleanValue(isPredictionFeature, true);
                    aCas.addFsToIndexes(annotation);
                }
            }
        }
    }
 
Example 7
Source File: OpenNlpNerRecommender.java    From inception with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    TokenNameFinderModel model = aContext.get(KEY_MODEL).orElseThrow(() -> 
            new RecommendationException("Key [" + KEY_MODEL + "] not found in context"));
    
    NameFinderME finder = new NameFinderME(model);

    Type sentenceType = getType(aCas, Sentence.class);
    Type tokenType = getType(aCas, Token.class);
    Type predictedType = getPredictedType(aCas);

    Feature predictedFeature = getPredictedFeature(aCas);
    Feature isPredictionFeature = getIsPredictionFeature(aCas);
    Feature scoreFeature = getScoreFeature(aCas);

    int predictionCount = 0;
    for (AnnotationFS sentence : select(aCas, sentenceType)) {
        if (predictionCount >= traits.getPredictionLimit()) {
            break;
        }
        predictionCount++;
        
        List<AnnotationFS> tokenAnnotations = selectCovered(tokenType, sentence);
        String[] tokens = tokenAnnotations.stream()
            .map(AnnotationFS::getCoveredText)
            .toArray(String[]::new);

        for (Span prediction : finder.find(tokens)) {
            String label = prediction.getType();
            if (NameSample.DEFAULT_TYPE.equals(label)) {
                continue;
            }
            int begin = tokenAnnotations.get(prediction.getStart()).getBegin();
            int end = tokenAnnotations.get(prediction.getEnd() - 1).getEnd();
            AnnotationFS annotation = aCas.createAnnotation(predictedType, begin, end);
            annotation.setStringValue(predictedFeature, label);
            annotation.setDoubleValue(scoreFeature, prediction.getProb());
            annotation.setBooleanValue(isPredictionFeature, true);

            aCas.addFsToIndexes(annotation);
        }
    }
}
 
Example 8
Source File: LappsGridRecommender.java    From inception with Apache License 2.0 4 votes vote down vote up
@Override
public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException
{
    try {
        Container container = new Container();
        new DKPro2Lif().convert(aCas.getJCas(), container);

        String request = new Data<>(Discriminators.Uri.LIF, container).asJson();
        String response = client.execute(request);

        DataContainer result = Serializer.parse(response, DataContainer.class);
        
        aCas.reset();
        new Lif2DKPro().convert(result.getPayload(), aCas.getJCas());
        
        Feature isPredictionFeature = getIsPredictionFeature(aCas);
        for (AnnotationFS predictedAnnotation : select(aCas, getPredictedType(aCas))) {
            predictedAnnotation.setBooleanValue(isPredictionFeature, true);
        }
        
        // Drop the tokens we got from the remote service since their boundaries might not
        // match ours.
        select(aCas, getType(aCas, Token.class)).forEach(aCas::removeFsFromIndexes);
        
        // If the remote service did not return tokens (or if we didn't find them...), then
        // let's just re-add the tokens that we originally sent. We need the tokens later
        // when extracting the predicted annotations
        Type tokenType = getType(aCas, Token.class);
        if (select(aCas, getType(aCas, Token.class)).isEmpty()) {
            container.getView(0).getAnnotations().stream()
                .filter(a -> Discriminators.Uri.TOKEN.equals(a.getAtType()))
                .forEach(token -> {
                    AnnotationFS t = aCas.createAnnotation(tokenType,
                            token.getStart().intValue(), token.getEnd().intValue());
                    aCas.addFsToIndexes(t);
                });
        }
    } catch (Exception e) {
        throw new RecommendationException("Cannot predict", e);
    }
}
 
Example 9
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 10
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;
}