<commit_before>/** 
 * Copyright (c) 2007-2008, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */
package org.cleartk.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import opennlp.tools.util.Span;

import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.jcas.tcas.Annotation;
import org.cleartk.type.ContiguousAnnotation;
import org.cleartk.type.SplitAnnotation;


/**
 * <br>
 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * 
 * @author <NAME>
 * 
 */
public class AnnotationUtil {
	/**
	 * Determines whether the big annotation contains the small annotation
	 * w.r.t. to character offsets (begin and end).
	 * 
	 */
	public static boolean contains(Annotation bigAnnotation, Annotation smallAnnotation) {
		if (bigAnnotation instanceof SplitAnnotation) {
			FSArray splits = ((SplitAnnotation) bigAnnotation).getAnnotations();

			for (int i = 0; i < splits.size(); i++) {
				ContiguousAnnotation split = (ContiguousAnnotation) splits.get(i);
				if (contains(split, smallAnnotation)) return true;
			}
			return false;
		}

		if (bigAnnotation == null || smallAnnotation == null) return false;
		if (bigAnnotation.getBegin() <= smallAnnotation.getBegin()
				&& bigAnnotation.getEnd() >= smallAnnotation.getEnd()) return true;
		else return false;
	}

	public static boolean overlaps(Annotation annotation1, Annotation annotation2) {
		Annotation firstAnnotation, secondAnnotation;

		if (annotation1.getBegin() == annotation2.getBegin()) return true;

		if (annotation1.getBegin() < annotation2.getBegin()) {
			firstAnnotation = annotation1;
			secondAnnotation = annotation2;
		}
		else {
			firstAnnotation = annotation2;
			secondAnnotation = annotation1;
		}

		if (firstAnnotation.getEnd() > secondAnnotation.getBegin()) return true;
		return false;

	}

	public static int size(Annotation annotation) {
		try {
			return annotation.getEnd() - annotation.getBegin();
		}
		catch (Exception e) {
			return 0;
		}
	}

	public static Span getAnnotationsExtent(List<? extends Annotation> annotations) {
		int start = Integer.MAX_VALUE;
		int end = 0;

		for (Annotation annotation : annotations) {
			if (annotation.getBegin() < start) start = annotation.getBegin();
			if (annotation.getEnd() > end) end = annotation.getEnd();
		}

		return new Span(start, end);
	}

	/**
	 * This method provides a way of getting some text before or after an
	 * annotation specified by some "window" of "tokens". For example, you could
	 * retrieve the text corresponding to 5 tokens to the right of a named
	 * entity. This may be useful for e.g. creating a report that shows the
	 * output of an annotator such that surrounding text is included.
	 * 
	 * @param <TOKEN_TYPE>
	 * @param jCas
	 * @param annotation
	 *            an annotation to get surrounding/nearby text from. This can be
	 *            any kind of annotation.
	 * @param tokenClass
	 *            the kind of "tokens" to use. This could be any kind of
	 *            annotation type as well.
	 * @param numberOfTokens
	 *            the number of tokens to consider when creating a span of text
	 * @param before
	 *            determines whether to return text occuring before the
	 *            annotation or after
	 * @return a span of text that occurs before or after the annotation which
	 *         will either end at the beginning of the annotation or begin at
	 *         the end of the annotation. The other edge of the span is
	 *         determined by the start/end location of the "token" found on<commit_msg>moved methods to AnnotationUtil2<commit_after>/** 
 * Copyright (c) 2007-2008, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */
package org.cleartk.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.FSArray;
import org.apache.uima.jcas.tcas.Annotation;


/**
 * <br>
 * Copyright (c) 2007-2008, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * 
 * @author <NAME>
 * 
 */
public class AnnotationUtil {

	public static boolean overlaps(Annotation annotation1, Annotation annotation2) {
		Annotation firstAnnotation, secondAnnotation;

		if (annotation1.getBegin() == annotation2.getBegin()) return true;

		if (annotation1.getBegin() < annotation2.getBegin()) {
			firstAnnotation = annotation1;
			secondAnnotation = annotation2;
		}
		else {
			firstAnnotation = annotation2;
			secondAnnotation = annotation1;
		}

		if (firstAnnotation.getEnd() > secondAnnotation.getBegin()) return true;
		return false;

	}

	public static int size(Annotation annotation) {
		try {
			return annotation.getEnd() - annotation.getBegin();
		}
		catch (Exception e) {
			return 0;
		}
	}

	/**
	 * This method provides a way of getting some text before or after an
	 * annotation specified by some "window" of "tokens". For example, you could
	 * retrieve the text corresponding to 5 tokens to the right of a named
	 * entity. This may be useful for e.g. creating a report that shows the
	 * output of an annotator such that surrounding text is included.
	 * 
	 * @param <TOKEN_TYPE>
	 * @param jCas
	 * @param annotation
	 *            an annotation to get surrounding/nearby text from. This can be
	 *            any kind of annotation.
	 * @param tokenClass
	 *            the kind of "tokens" to use. This could be any kind of
	 *            annotation type as well.
	 * @param numberOfTokens
	 *            the number of tokens to consider when creating a span of text
	 * @param before
	 *            determines whether to return text occuring before the
	 *            annotation or after
	 * @return a span of text that occurs before or after the annotation which
	 *         will either end at the beginning of the annotation or begin at
	 *         the end of the annotation. The other edge of the span is
	 *         determined by the start/end location of the "token" found on
{'fraction_non_alphanumeric': 0.0708209037675395, 'fraction_numerical': 0.006518616727433433, 'mean_word_length': 3.6136595310907236, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5281573', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2239, 'n_words': 1069}