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

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#getCoveredText() . 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: DataMajorityNerRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
private List<Annotation> predict(Collection<AnnotationFS> candidates,
                                 DataMajorityModel aModel)
{
    List<Annotation> result = new ArrayList<>();
    for (AnnotationFS token : candidates) {
        String tokenText = token.getCoveredText();
        if (tokenText.length() > 0 && !Character.isUpperCase(tokenText.codePointAt(0))) {
            continue;
        }

        int begin = token.getBegin();
        int end = token.getEnd();

        Annotation annotation = new Annotation(aModel.majorityLabel, aModel.confidence, 
                aModel.numberOfAnnotations, begin, end);
        result.add(annotation);
    }

    return result;
}
 
Example 2
Source File: AnnotationDetails.java    From inception with Apache License 2.0 6 votes vote down vote up
public AnnotationDetails(FeatureStructure aFS)
{
    addr = WebAnnoCasUtil.getAddr(aFS);
    type = aFS.getType().getName();
    if (aFS instanceof AnnotationFS) {
        AnnotationFS annoFS = (AnnotationFS) aFS;
        begin = annoFS.getBegin();
        end = annoFS.getEnd();
        text = annoFS.getCoveredText();
    }
    else {
        begin = -1;
        end = -1;
        text = null;
    }
}
 
Example 3
Source File: OutlineLabelProvider.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public String getColumnText(Object element, int columnIndex) {
  // there is only one column, if column index something
  // else than 0, then there is an error
  if (columnIndex != 0) {
    // ... just return null
    return null;
  }

  AnnotationFS annotation = (AnnotationFS) ((IAdaptable) element).getAdapter(AnnotationFS.class);

  if (annotation != null) {
    if (annotation.getCoveredText() != null)
      return getStringWithoutNewLine(annotation.getCoveredText());
    else
      return "";
  }
  
  Type type = (Type) ((IAdaptable) element).getAdapter(Type.class);
  
  if (type != null) {
  	return type.getShortName();
  }
  
  return "Unkown type";
}
 
Example 4
Source File: MtasUimaParser.java    From inception with Apache License 2.0 5 votes vote down vote up
private void indexTokenText(AnnotationFS aAnnotation, Range aRange, int aMtasId)
{
    MtasToken mtasToken = new MtasTokenString(aMtasId, MTAS_TOKEN_LABEL,
            aAnnotation.getCoveredText(), aRange.getBegin());
    mtasToken.setOffset(aAnnotation.getBegin(), aAnnotation.getEnd());
    mtasToken.addPositionRange(aRange.getBegin(), aRange.getEnd());
    tokenCollection.add(mtasToken);
}
 
Example 5
Source File: MtasUimaParser.java    From inception with Apache License 2.0 5 votes vote down vote up
private void indexSentenceText(AnnotationFS aAnnotation, Range aRange, int aMtasId)
{
    MtasToken mtasSentence = new MtasTokenString(aMtasId, MTAS_SENTENCE_LABEL,
            aAnnotation.getCoveredText(), aRange.getBegin());
    mtasSentence.setOffset(aAnnotation.getBegin(), aAnnotation.getEnd());
    mtasSentence.addPositionRange(aRange.getBegin(), aRange.getEnd());
    tokenCollection.add(mtasSentence);
}
 
Example 6
Source File: OpenNlpPosRecommender.java    From inception with Apache License 2.0 5 votes vote down vote up
private Optional<POSSample> createPosSample(CAS aCas, AnnotationFS aSentence,
        Collection<AnnotationFS> aTokens)
{
    Type annotationType = getType(aCas, layerName);
    Feature feature = annotationType.getFeatureByBaseName(featureName);

    int numberOfTokens = aTokens.size();
    String[] tokens = new String[numberOfTokens];
    String[] tags = new String[numberOfTokens];

    int withTagCount = 0;

    int i = 0;
    for (AnnotationFS token : aTokens) {
        tokens[i] = token.getCoveredText();
        String tag = getFeatureValueCovering(aCas, token, annotationType, feature);
        tags[i] = tag;

        // If the tag is neither PAD nor null, then there is at
        // least one annotation the trainer can work with.
        if (tag != null & !PAD.equals(tag)) {
            withTagCount++;
        }

        i++;
    }
    
    // Require at least X percent of the sentence to have tags to avoid class imbalance on PAD
    // tag.
    double coverage = ((double) withTagCount * 100) / (double) numberOfTokens;
    if (coverage > traits.getTaggedTokensThreshold()) {
        return Optional.of(new POSSample(tokens, tags));
    }
    else {
        return Optional.empty();
    }
}
 
Example 7
Source File: Selection.java    From webanno with Apache License 2.0 5 votes vote down vote up
public void selectArc(VID aVid, AnnotationFS aOriginFs, AnnotationFS aTargetFs)
{
    selectedAnnotationId = aVid;
    text = "[" + aOriginFs.getCoveredText() + "] - [" + aTargetFs.getCoveredText() + "]";
    beginOffset = Math.min(aOriginFs.getBegin(), aTargetFs.getBegin());
    endOffset = Math.max(aOriginFs.getEnd(), aTargetFs.getEnd());
    
    // Properties used when an arc is selected
    originSpanId = getAddr(aOriginFs);
    targetSpanId = getAddr(aTargetFs);
    
    LOG.debug("Arc: {}", this);
    
    fireSelectionChanged();
}
 
Example 8
Source File: RelationDiffAdapter.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public Position getPosition(int aCasId, FeatureStructure aFS, String aFeature, String aRole,
        int aLinkTargetBegin, int aLinkTargetEnd, LinkCompareBehavior aLinkCompareBehavior)
{
    Type type = aFS.getType();
    AnnotationFS sourceFS = (AnnotationFS) aFS.getFeatureValue(type
            .getFeatureByBaseName(sourceFeature));
    AnnotationFS targetFS = (AnnotationFS) aFS.getFeatureValue(type
            .getFeatureByBaseName(targetFeature));
    
    String collectionId = null;
    String documentId = null;
    try {
        FeatureStructure dmd = WebAnnoCasUtil.getDocumentMetadata(aFS.getCAS());
        collectionId = FSUtil.getFeature(dmd, "collectionId", String.class);
        documentId = FSUtil.getFeature(dmd, "documentId", String.class);
    }
    catch (IllegalArgumentException e) {
        // We use this information only for debugging - so we can ignore if the information
        // is missing.
    }
    
    String linkTargetText = null;
    if (aLinkTargetBegin != -1 && aFS.getCAS().getDocumentText() != null) {
        linkTargetText = aFS.getCAS().getDocumentText()
                .substring(aLinkTargetBegin, aLinkTargetEnd);
    }
    
    return new RelationPosition(collectionId, documentId, aCasId, getType(), 
            sourceFS != null ? sourceFS.getBegin() : -1,
            sourceFS != null ? sourceFS.getEnd() : -1,
            sourceFS != null ? sourceFS.getCoveredText() : null,
            targetFS != null ? targetFS.getBegin() : -1,
            targetFS != null ? targetFS.getEnd() : -1,
            targetFS != null ? targetFS.getCoveredText() : null,
            aFeature, aRole, aLinkTargetBegin, aLinkTargetEnd, linkTargetText,
            aLinkCompareBehavior);
}
 
Example 9
Source File: SpanDiffAdapter.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public Position getPosition(int aCasId, FeatureStructure aFS, String aFeature, String aRole,
        int aLinkTargetBegin, int aLinkTargetEnd, LinkCompareBehavior aLinkCompareBehavior)
{
    AnnotationFS annoFS = (AnnotationFS) aFS;
    
    String collectionId = null;
    String documentId = null;
    try {
        FeatureStructure dmd = WebAnnoCasUtil.getDocumentMetadata(aFS.getCAS());
        collectionId = FSUtil.getFeature(dmd, "collectionId", String.class);
        documentId = FSUtil.getFeature(dmd, "documentId", String.class);
    }
    catch (IllegalArgumentException e) {
        // We use this information only for debugging - so we can ignore if the information
        // is missing.
    }
    
    String linkTargetText = null;
    if (aLinkTargetBegin != -1 && aFS.getCAS().getDocumentText() != null) {
        linkTargetText = aFS.getCAS().getDocumentText()
                .substring(aLinkTargetBegin, aLinkTargetEnd);
    }
    
    return new SpanPosition(collectionId, documentId, aCasId, getType(), annoFS.getBegin(),
            annoFS.getEnd(), annoFS.getCoveredText(), aFeature, aRole, aLinkTargetBegin,
            aLinkTargetEnd, linkTargetText, aLinkCompareBehavior);
}
 
Example 10
Source File: AgreementUtils.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static Object extractLinkFeatureValueForAgreement(FeatureStructure aFs, String aFeature,
        int aLinkIndex, LinkCompareBehavior aLCB)
{
    ArrayFS links = (ArrayFS) aFs.getFeatureValue(aFs.getType().getFeatureByBaseName(
            aFeature));
    FeatureStructure link = links.get(aLinkIndex);
    
    switch (aLCB) {
    case LINK_TARGET_AS_LABEL:
        // FIXME The target feature name should be obtained from the feature
        // definition!
        AnnotationFS target = (AnnotationFS) link.getFeatureValue(link.getType()
                .getFeatureByBaseName("target"));
        
        return target.getBegin() + "-" + target.getEnd() + " ["
                + target.getCoveredText() + "]";
    case LINK_ROLE_AS_LABEL:
        // FIXME The role feature name should be obtained from the feature
        // definition!
        String role = link.getStringValue(link.getType().getFeatureByBaseName(
                "role"));
        
        return role;
    default:
        throw new IllegalStateException("Unknown link target comparison mode ["
                + aLCB + "]");
    }        
}
 
Example 11
Source File: CasTreeViewer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiates a new annotation tree node object.
 *
 * @param aAnnotation the a annotation
 */
public AnnotationTreeNodeObject(AnnotationFS aAnnotation) {
  mAnnotation = aAnnotation;
  mCaption = aAnnotation.getCoveredText();
  if (mCaption.length() > 64)
    mCaption = mCaption.substring(0, 64) + "...";

}
 
Example 12
Source File: AutomationUtil.java    From webanno with Apache License 2.0 4 votes vote down vote up
private static StringBuffer getMiraLine(AnnotationSchemaService aAnnotationService,
        AnnotationFS sentence, AnnotationFeature aLayerFeature, TypeAdapter aAdapter)
    throws CASException
{
    StringBuffer sb = new StringBuffer();

    String tag = "";
    List<String> annotations = new ArrayList<>();
    Map<Integer, String> multAnno = null;
    if (aLayerFeature != null) {
        switch (aLayerFeature.getLayer().getAnchoringMode()) {
        case TOKENS:
            multAnno = getMultipleAnnotation(aAnnotationService, sentence, aLayerFeature);
            break;
        case SINGLE_TOKEN:
            annotations = getAnnotation(aAdapter, sentence, aLayerFeature);
            break;
        default:
            throw new IllegalStateException("Unsupported anchoring mode: ["
                    + aLayerFeature.getLayer().getAnchoringMode() + "]");
        }
    }

    int i = 0;
    for (AnnotationFS token : selectTokensCovered(sentence)) {
        String word = token.getCoveredText();

        char[] words = word.toCharArray();

        String prefix1 = "", prefix2 = "", prefix3 = "", prefix4 = "", suffix1 = "", suffix2 = "", suffix3 = "", suffix4 = "";
        if (
                aLayerFeature == null || 
                AnchoringMode.SINGLE_TOKEN.equals(aLayerFeature.getLayer().getAnchoringMode())
        ) {
            prefix1 = Character.toString(words[0]) + " ";
            prefix2 = (words.length > 1 ? prefix1.trim()
                    + (Character.toString(words[1]).trim().equals("") ? "__nil__" : Character
                            .toString(words[1])) : "__nil__")
                    + " ";
            prefix3 = (words.length > 2 ? prefix2.trim()
                    + (Character.toString(words[2]).trim().equals("") ? "__nil__" : Character
                            .toString(words[2])) : "__nil__")
                    + " ";
            prefix4 = (words.length > 3 ? prefix3.trim()
                    + (Character.toString(words[3]).trim().equals("") ? "__nil__" : Character
                            .toString(words[3])) : "__nil__")
                    + " ";
            suffix1 = Character.toString(words[words.length - 1]) + " ";
            suffix2 = (words.length > 1 ? (Character.toString(words[words.length - 2]).trim()
                    .equals("") ? "__nil__" : Character.toString(words[words.length - 2]))
                    + suffix1.trim() : "__nil__")
                    + " ";
            suffix3 = (words.length > 2 ? (Character.toString(words[words.length - 3]).trim()
                    .equals("") ? "__nil__" : Character.toString(words[words.length - 3]))
                    + suffix2.trim() : "__nil__")
                    + " ";
            suffix4 = (words.length > 3 ? (Character.toString(words[words.length - 4]).trim()
                    .equals("") ? "__nil__" : Character.toString(words[words.length - 4]))
                    + suffix3.trim() : "__nil__")
                    + " ";
        }
        String nl = "\n";

        if (aLayerFeature != null) {
            if (AnchoringMode.TOKENS.equals(aLayerFeature.getLayer().getAnchoringMode())) {
                tag = multAnno.get(getAddr(token)) == null ? "O" : multAnno.get(getAddr(token));
            }
            else {
                tag = annotations.size() == 0 ? NILL : annotations.get(i);
                i++;
            }

        }
        sb.append(word).append(" ").append(prefix1).append(prefix2).append(prefix3)
            .append(prefix4).append(suffix1).append(suffix2).append(suffix3).append(suffix4)
            .append(tag).append(nl);
    }
    return sb;

}