Exemplo n.º 1
0
  @Override
  protected void endRegion(final Style aStyle) throws IOException {
    if (log.isTraceEnabled()) {
      log.trace("</" + aStyle + ">");
    }

    if (regionText == null) {
      throw new IllegalStateException("No region started");
    }

    if (regionStyle != aStyle) {
      throw new IllegalStateException(
          "Current region has style " + regionStyle + ", but closing region has style " + aStyle);
    }

    // Append text
    int begin = text.length();
    sanitize(regionText);
    text.append(regionText.toString());
    int end = text.length();
    text.append('\n');

    // Add annotation
    switch (aStyle) {
      case HEADING:
        if (headingType != null) {
          Type t = cas.getTypeSystem().getType(headingType);
          AnnotationFS a = cas.createAnnotation(t, begin, end);
          cas.addFsToIndexes(a);
        }
        break;
      case PARAGRAPH:
        if (paragraphType != null) {
          Type t = cas.getTypeSystem().getType(paragraphType);
          AnnotationFS a = cas.createAnnotation(t, begin, end);
          cas.addFsToIndexes(a);
        }
        break;
      default:
        throw new IllegalStateException("Unknown region style: " + aStyle);
    }

    regionStyle = null;
    regionText = null;
  }
  /** Performs name finding on the given cas object. */
  public final void process(CAS cas) {

    if (isRemoveExistingAnnotations) {
      final AnnotationComboIterator sentenceNameCombo =
          new AnnotationComboIterator(cas, mSentenceType, mNameType);

      List<AnnotationFS> removeAnnotations = new LinkedList<AnnotationFS>();
      for (AnnotationIteratorPair annotationIteratorPair : sentenceNameCombo) {
        for (AnnotationFS nameAnnotation : annotationIteratorPair.getSubIterator()) {
          removeAnnotations.add(nameAnnotation);
        }
      }

      for (AnnotationFS annotation : removeAnnotations) {
        cas.removeFsFromIndexes(annotation);
      }
    }

    final AnnotationComboIterator sentenceTokenCombo =
        new AnnotationComboIterator(cas, mSentenceType, mTokenType);

    for (AnnotationIteratorPair annotationIteratorPair : sentenceTokenCombo) {

      final List<AnnotationFS> sentenceTokenAnnotationList = new LinkedList<AnnotationFS>();

      final List<String> sentenceTokenList = new LinkedList<String>();

      for (AnnotationFS tokenAnnotation : annotationIteratorPair.getSubIterator()) {

        sentenceTokenAnnotationList.add(tokenAnnotation);

        sentenceTokenList.add(tokenAnnotation.getCoveredText());
      }

      Span[] names =
          find(cas, (String[]) sentenceTokenList.toArray(new String[sentenceTokenList.size()]));

      AnnotationFS nameAnnotations[] = new AnnotationFS[names.length];

      for (int i = 0; i < names.length; i++) {

        int startIndex =
            ((AnnotationFS) sentenceTokenAnnotationList.get(names[i].getStart())).getBegin();

        int endIndex =
            ((AnnotationFS) sentenceTokenAnnotationList.get(names[i].getEnd() - 1)).getEnd();

        nameAnnotations[i] = cas.createAnnotation(mNameType, startIndex, endIndex);

        cas.getIndexRepository().addFS(nameAnnotations[i]);
      }

      postProcessAnnotations(names, nameAnnotations);
    }

    documentDone(cas);
  }