Package gate

Examples of gate.AnnotationSet


   * @param featurename
   */
  public static void copyFeature(Map<String, AnnotationSet> bindings,
      String fromBindings, String fromType, String toBindings, String toType,
      String featurename) {
    AnnotationSet toSet = getAnnsForType(getBindings(bindings, toBindings),
        toType);
    AnnotationSet fromSet = getAnnsForType(getBindings(bindings, fromBindings),
        fromType);
    Annotation toAnn = getLongestAnn(toSet);
    Annotation fromAnn = getLongestAnn(fromSet);
    FeatureMap toFm = toAnn.getFeatures();
    FeatureMap fromFm = fromAnn.getFeatures();
View Full Code Here


   * @param toAnn
   * @param featurename
   */
  public static void copyFeature(Map<String, AnnotationSet> bindings,
      String fromBindings, String fromType, Annotation toAnn, String featurename) {
    AnnotationSet fromSet = getAnnsForType(getBindings(bindings, fromBindings),
        fromType);
    Annotation fromAnn = getLongestAnn(fromSet);
    FeatureMap toFm = toAnn.getFeatures();
    FeatureMap fromFm = fromAnn.getFeatures();
    toFm.put(featurename, fromFm.get(featurename));
View Full Code Here

   * @param bindingName
   * @return
   */
  public static AnnotationSet getBindings(Map<String, AnnotationSet> bindings,
      String bindingName) {
    AnnotationSet set = bindings.get(bindingName);
    if (set == null) {
      throw new GateRuntimeException("No bindings named " + bindingName);
    }
    return new ImmutableAnnotationSetImpl(set.getDocument(), set) {
      private static final long serialVersionUID = -6703131102439043539L;
    };
  }
View Full Code Here

   * @param typeName
   * @return
   */
  public static AnnotationSet getAnnsForType(AnnotationSet annset,
      String typeName) {
    AnnotationSet set = annset.get(typeName);
    if (set == null) {
      throw new GateRuntimeException("Got a null set for type " + typeName);
    }
    return new ImmutableAnnotationSetImpl(annset.getDocument(), set) {
      private static final long serialVersionUID = -6703131102439043539L;
View Full Code Here

  }

  public static AnnotationSet getAnnsCoextensiveWorker(AnnotationSet source,
      String type, int start, int end) {
    if (source instanceof gate.annotation.AnnotationSetImpl) {
      AnnotationSet ret = ((AnnotationSetImpl) source).getStrict((long) start,
          (long) end);
      if (type != null) {
        return ret.get(type);
      } else {
        return ret;
      }
    } else {
      AnnotationSet annset = source.getContained((long) start, (long) end);
      List<Annotation> annotationsToAdd = new ArrayList<Annotation>();
      for (Annotation ann : annset) {
        if (startOffset(ann) == start && endOffset(ann) == end) {
          if (type == null || (type != null && ann.getType().equals(type))) {
            annotationsToAdd.add(ann);
          }
        }
      }
      return new ImmutableAnnotationSetImpl(annset.getDocument(),
          annotationsToAdd) {
        private static final long serialVersionUID = -6703131102439043539L;
      };
    }
  }
View Full Code Here

    List<Annotation> ret = new ArrayList<Annotation>();

    int startOffset = startOffset(range);
    int endOffset = endOffset(range);

    AnnotationSet elements = gate.Utils.getContainedAnnotations(inputAS, range,
        type);

    // System.out.println("Number of elements: "+elements.size());

    Set<Integer> offsets = new HashSet<Integer>();
    for (Annotation ann : elements) {
      Integer off = ann.getStartNode().getOffset().intValue();
      offsets.add(off);
    }

    // create the sorted list of offsets
    List<Integer> sortedOffsets = new ArrayList<Integer>();
    sortedOffsets.addAll(offsets);
    Collections.sort(sortedOffsets);
    // System.out.println("Sorted Offsets: "+sortedOffsets);

    int curlength = 0;
    // At each offset where we have potential chain elements, try
    // to start a chain there.
    for (Integer offset : sortedOffsets) {
      // We may already have a chain from some smaller offset, so check first
      // if the longest chain from this offset can ever be longer than what
      // we already have ...
      // System.out.println("Checking at offset "+offset);
      if (endOffset - offset > curlength) {
        // Now, start building the longest chain starting at offset ...
        // First, get all the annotations at this offset
        AnnotationSet offsetAnns = getAnnsStartingAt(elements, type, offset);
        // System.out.println("Annotations here: "+offsetAnns.size());
        // For each annotation, try to find a chain with equal feature values
        // we only form a chain if there is at least one more annotation
        // (i.e. the minimum number of annotations in the chain is 2)
        for (Annotation ann : offsetAnns) {
          // The annotation ann is a candidate for starting a chain ...
          // simple version of finding the chain: for each ann try to
          // find as many annotations with identical feature at the next
          // offset that is > than the end offset of the current ann
          Object val = ann.getFeatures().get(feature);
          // System.out.println("Finding chain for annotation: "+ann);
          if (val == null) {
            // If the candidate for the first element does not have
            // the desired feature set, just ignore it.
            // System.out.println("No feature found, ignoring");
            continue;
          }
          // Make a feature map that contains the feature=value constraint
          // which is needed to check the other elements of the chain.
          FeatureMap constraint = Factory.newFeatureMap();
          constraint.put(feature, val);
          List<Annotation> chain = new ArrayList<Annotation>();
          // add the candidate head to this chain candidate
          chain.add(ann);
          int chainlength = 0;
          // curann is the annotation we process in a loop to find
          // something to append
          Annotation curann = ann;
          while (true) {
            // System.out.println("Chain finding: "+curann);
            // Find the next offset that is a) behind the end of curann and
            // b) a known offset where we have elements
            int curend = curann.getEndNode().getOffset().intValue();
            int nextOffset = offset;
            for (int o : sortedOffsets) {
              if (o >= curend) {
                nextOffset = o;
                break;
              }
            }
            // if we are still at the old offset, no other offset with elements
            // was found, end the loop
            if (nextOffset == offset) {
              break;
            }
            // Get all the elements at the next offset
            AnnotationSet curset = getAnnsStartingAt(elements, nextOffset);
            // System.out.println("Found anns at next offset: "+curset.size());
            // And filter out just those we want: same type and feature value
            curset = curset.get(type, constraint);
            // System.out.println("Found anns that match feature: "+curset.size());
            // if nothing remains, end the loop
            if (curset == null || curset.size() == 0) {
              break;
            }
            // we just pick an arbitrary element here to add to the chain,
            // but instead we should use ALL elements to continue.
            // This means that for each chain we already have, we create
            // as many new chains as we find candidates here.
            curann = curset.iterator().next();
            chain.add(curann);
            chainlength = endOffset(curann) - offset;
          }
          // if the new chain is longer ...
          if (chainlength > curlength) {
View Full Code Here

    int startOffset = startOffset(range);
    int endOffset = endOffset(range);

    // Find all the offsets where we element annotations start
    List<Integer> sortedOffsets = new ArrayList<Integer>();
    AnnotationSet elements = gate.Utils.getContainedAnnotations(inputAS, range,
        type);
    // System.out.println("Number of elements: "+elements.size());
    Set<Integer> offsets = new HashSet<Integer>();
    for (Annotation ann : elements) {
      Integer off = ann.getStartNode().getOffset().intValue();
      offsets.add(off);
    }

    // create the sorted list of offsets
    sortedOffsets.addAll(offsets);
    Collections.sort(sortedOffsets);
    // System.out.println("Sorted Offsets: "+sortedOffsets);

    Set<AnnotationChain> chains = new HashSet<AnnotationChain>();

    // At each offset we either try to make the existing chains longer
    // or we try to start new chains.
    for (Integer offset : sortedOffsets) {
      AnnotationSet offsetAnns = getAnnsStartingAt(elements, type, offset);
      // first check all the chains we already have to see if we can
      // add one or more of the annotations here:
      // For each chain
      // if the end of the chain is beyond the offset, ignore the chain
      // find all annotations that match the chain value
      // if there is exactly one annotation, add it to the chain,
      // also add mark as added
      // otherwise, make as many total copies of the chain as there are
      // annotations and add each annotation to its corresponding chain
      // also mark as added
      // start a new chain at each annotation that was not already added
      // to a chain.
      Set<Annotation> addedAnnotations = new HashSet<Annotation>();
      for (AnnotationChain chain : chains) {
        Annotation lastEl = chain.getLast();
        // is the end of the last element not beyond the current offset?
        // then the chain can potentially be made longer with annotations
        // from this offset.
        Object value = lastEl.getFeatures().get(feature);
        if (endOffset(lastEl) <= offset) {
          Set<Annotation> candidates = new HashSet<Annotation>();
          for (Annotation offsetAnn : offsetAnns) {
            Object annValue = offsetAnn.getFeatures().get(feature);
            if (annValue == null) {
              continue;
            } // ignore anns with no value
            if (annValue.equals(value)) {
              candidates.add(offsetAnn);
            }
          }
          if (candidates.isEmpty()) {
            continue;
          } // no candidates for this chain
          if (candidates.size() == 1) {
            // add the annotation to the current chain
            Annotation a = candidates.iterator().next();
            chain.addLast(a);
            addedAnnotations.add(a);
          } else {
            // more than one canditate, we need to duplicate the current chain
            System.err.println("Need to duplicate chain, not yet implemented!");
            Annotation a = candidates.iterator().next();
            chain.addLast(a);
            addedAnnotations.add(a);
          }
        } // offset compatible with current chain
      } // for each chain

      // Now that we have made all the chains longer where possible, try
      // to start new chains here but only if the sequenceType does not ask
      // for only sequences starting at the beginning
      if (
      // either we have requested all chains from any offset ...
      (sequenceType == SequenceType.ALL || sequenceType == SequenceType.LONGEST)
          // or we are at the starting offset anyways
          || (offset == startOffset)) {
        for (Annotation offsetAnn : offsetAnns) {
          if (!addedAnnotations.contains(offsetAnn)) {
            AnnotationChain newChain = new AnnotationChain();
            newChain.addLast(offsetAnn);
            chains.add(newChain);
          }
        }
      }
    } // for offsets in sorted offsets
    int longestLength = 0;
    Iterator<AnnotationChain> chainIt = chains.iterator();
    while (chainIt.hasNext()) {
      AnnotationChain chain = chainIt.next();
      // if we must cover the full range, remove all chains that do not
      // end at the end of the range.
      // Note that if we must start at the beginning, we already made
      // sure that only chains starting at the beginning were generated
      if (sequenceType == SequenceType.ALL_FULLRANGE) {
        if (endOffset(chain.getLast()) != endOffset)
          chainIt.remove();
      } else {
        // if this is an acceptable chain, find the longest one
        int length = chain.getLength();
        if (length > longestLength) {
          longestLength = length;
        }
      }
    }
    // if we just want the longest chains, iterate again and remove the
    // non-longest ones
    if (sequenceType == SequenceType.LONGEST
        || sequenceType == SequenceType.LONGEST_FROMSTART) {
      chainIt = chains.iterator();
      while (chainIt.hasNext()) {
        AnnotationChain chain = chainIt.next();
        if (chain.getLength() < longestLength) {
          chainIt.remove();
        }
      }
    }
    // now we are left with all chains we want to return
    Set<AnnotationSet> returnSet = new HashSet<AnnotationSet>();
    for (AnnotationChain chain : chains) {
      AnnotationSet aset = new ImmutableAnnotationSetImpl(range.getDocument(),
          chain.getChain()) {
        private static final long serialVersionUID = -6703131102439043539L;
      };
      returnSet.add(aset);
    }
View Full Code Here

   * @param toOffset End offset of covering range
   * @param type Annotation type of the annotations to remove
   */
  public static void removeContainedAnnotations(AnnotationSet whereFrom,
    int fromOffset, int toOffset, String type) {
    AnnotationSet whatToRemove =
      whereFrom.getContained((long)fromOffset, (long)toOffset);
    whereFrom.removeAll(whatToRemove);
  }
View Full Code Here

  @Override
  public boolean doMatch(Object annotValue, AnnotationSet context)
    throws JapeException {

    Annotation annot = (Annotation) annotValue;
    AnnotationSet containedSet = doMatch(annot, context);

    Collection<Annotation> filteredSet = filterMatches(containedSet);

    return filteredSet.isEmpty();
  }
View Full Code Here

  @Override
  public boolean doMatch(Object annotValue, AnnotationSet context)
    throws JapeException {

    Annotation annot = (Annotation) annotValue;
    AnnotationSet containedSet = doMatch(annot, context);

    Collection<Annotation> filteredSet = filterMatches(containedSet);

    return filteredSet.isEmpty();
  }
View Full Code Here

TOP

Related Classes of gate.AnnotationSet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.