gate.relations.RelationsUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gate-core Show documentation
Show all versions of gate-core Show documentation
GATE - general achitecture for text engineering - is open source
software capable of solving almost any text processing problem. This
artifact enables you to embed the core GATE Embedded with its essential
dependencies. You will able to use the GATE Embedded API and load and
store GATE XML documents. This artifact is the perfect dependency for
CREOLE plugins or for applications that need to customize the GATE
dependencies due to confict with their own dependencies or for lower
footprint.
The newest version!
/*
* RelationsUtils.java
*
* Copyright (c) 1995-2012, The University of Sheffield. See the file
* COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
* software, licenced under the GNU Library General Public License,
* Version 2, June 1991 (in the distribution as file licence.html,
* and also available at http://gate.ac.uk/gate/licence.html).
*
* Valentin Tablan, 5 Mar 2012
*
* $Id: RelationsUtils.java 15540 2012-03-07 14:22:16Z valyt $
*/
package gate.relations;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Utilities for working with relations.
*/
public class RelationsUtils {
/**
* Computes the transitive closure for symmetric transitive relations (such as
* co-reference). Given a relation name and a seed annotation ID, this method
* returns the IDs for all annotations that are:
*
* - the seed annotation
* - in the given relation (regardless of position) with another
* annotation from this set
*
*
* @param relSet the {@link RelationSet} used as context.
* @param relationName the relation to be used for computing the closure.
* @param annotationId the ID for the seed annotation.
* @return an array containing the IDs of all the annotations in the closure.
*/
public static int[] transitiveClosure(RelationSet relSet,
String relationName, int annotationId) {
List relations = new ArrayList();
for(int pos = 0; pos < relSet.getMaximumArity(); pos++) {
int[] constraint = new int[pos + 1];
for(int i = 0; i < pos; i++) constraint[i] = RelationSet.ANY;
constraint[pos] = annotationId;
relations.addAll(relSet.getRelations(relationName, constraint));
}
SortedSet closure = new TreeSet();
closure.add(annotationId);
for(Relation rel : relations) {
for(int annId : rel.getMembers()) closure.add(annId);
}
int[] res = new int[closure.size()];
int i = 0;
for(int annId : closure) res[i++] = annId;
return res;
}
}