/** * @param relations relations of interest * @param annotation identified annotation of interest * @return all relations in the given relations where the second argument is the given annotation */ static public <T extends BinaryTextRelation> Collection<T> getRelationsAsSecond( final Collection<T> relations, final IdentifiedAnnotation annotation ) { return relations.stream() .filter( r -> r.getArg2().getArgument().equals( annotation ) ) .collect( Collectors.toList() ); }
/** * @param relations relations of interest * @param annotation identified annotation of interest * @return all relations in the given relations where the first argument is the given annotation */ static public <T extends BinaryTextRelation> Collection<T> getRelationsAsFirst( final Collection<T> relations, final IdentifiedAnnotation annotation ) { return relations.stream() .filter( r -> r.getArg1().getArgument().equals( annotation ) ) .collect( Collectors.toList() ); }
/** * @param relations relations of interest * @param annotation identified annotation of interest * @return all identified annotations in the given relations related to the given annotation as the first argument */ static public Collection<IdentifiedAnnotation> getFirstArguments( final Collection<? extends BinaryTextRelation> relations, final IdentifiedAnnotation annotation ) { return relations.stream() .filter( r -> r.getArg2().getArgument().equals( annotation ) ) .map( r -> r.getArg1().getArgument() ) .filter( IdentifiedAnnotation.class::isInstance ) .map( a -> (IdentifiedAnnotation) a ) .collect( Collectors.toList() ); }
/** * @param relations relations of interest * @param annotation identified annotation of interest * @return all identified annotations in the given relations related to the given annotation as the second argument */ static public Collection<IdentifiedAnnotation> getSecondArguments( final Collection<? extends BinaryTextRelation> relations, final IdentifiedAnnotation annotation ) { return relations.stream() .filter( r -> r.getArg1().getArgument().equals( annotation ) ) .map( r -> r.getArg2().getArgument() ) .filter( IdentifiedAnnotation.class::isInstance ) .map( a -> (IdentifiedAnnotation) a ) .collect( Collectors.toList() ); }
/** * Filter only the top level annotations (that is remove all the covered annotations). * * <p>This is not too efficient, but is should work with any ordering. * * @param <T> the generic type of the annotation * @param annotations the annotations to filter * @return a new list of containing just the top level (uncovered) annotations */ public static <T extends Annotation> List<T> filterToTopLevelAnnotations( final Collection<T> annotations) { final List<T> topLevel = new LinkedList<>(); for (final T a : annotations) { boolean covered = false; for (final T b : annotations) { if (!a.equals(b) && b.getBegin() <= a.getBegin() && a.getEnd() <= b.getEnd()) { covered = true; break; } } if (!covered) { topLevel.add(a); } } return topLevel; }
/** * Filter only the top level annotations (that is remove all the covered annotations). * * <p>This is not too efficient, but is should work with any ordering. * * @param <T> the generic type of the annotation * @param annotations the annotations to filter * @return a new list of containing just the top level (uncovered) annotations */ public static <T extends Annotation> List<T> filterToTopLevelAnnotations( final Collection<T> annotations) { final List<T> topLevel = new LinkedList<>(); for (final T a : annotations) { boolean covered = false; for (final T b : annotations) { if (!a.equals(b) && b.getBegin() <= a.getBegin() && a.getEnd() <= b.getEnd()) { covered = true; break; } } if (!covered) { topLevel.add(a); } } return topLevel; }
/** * @param relation relation of interest * @param annotation some annotation that might be in the relation * @return the target annotation in the relation if the first is present */ static private IdentifiedAnnotation getTarget( final BinaryTextRelation relation, final IdentifiedAnnotation annotation ) { if ( !relation.getArg1() .getArgument() .equals( annotation ) ) { return null; } final Annotation argument = relation.getArg2() .getArgument(); if ( argument != null && IdentifiedAnnotation.class.isInstance( argument ) ) { return (IdentifiedAnnotation) argument; } return null; }
/** * @param relation relation of interest * @param annotation some annotation that might be in the relation * @return the other annotation in the relation if the first is present */ static private IdentifiedAnnotation getRelated( final BinaryTextRelation relation, final IdentifiedAnnotation annotation ) { Annotation argument = null; if ( relation.getArg1().getArgument().equals( annotation ) ) { argument = relation.getArg2().getArgument(); } else if ( relation.getArg2().getArgument().equals( annotation ) ) { argument = relation.getArg1().getArgument(); } if ( argument != null && IdentifiedAnnotation.class.isInstance( argument ) ) { return (IdentifiedAnnotation) argument; } return null; }
static private String getRelationText( final IdentifiedAnnotation annotation, final BinaryTextRelation relation ) { if ( relation.getArg1().getArgument().equals( annotation ) ) { return SPACER + "[" + relation.getCategory() + "] " + getSafeText( relation.getArg2().getArgument() ) + NEWLINE; } else if ( relation.getArg2().getArgument().equals( annotation ) ) { return SPACER + getSafeText( relation.getArg1().getArgument() ) + " [" + relation.getCategory() + "]" + NEWLINE; } return ""; }
static private String getRelationText( final IdentifiedAnnotation annotation, final BinaryTextRelation relation ) { if ( relation.getArg1() .getArgument() .equals( annotation ) ) { return SPACER + "[" + relation.getCategory() + "] " + getSafeText( relation.getArg2() .getArgument() ) + NEWLINE; } else if ( relation.getArg2() .getArgument() .equals( annotation ) ) { return SPACER + getSafeText( relation.getArg1() .getArgument() ) + " [" + relation.getCategory() + "]" + NEWLINE; } return ""; }
public static boolean areAdjoining(Token t1, Token t2) { JCas jCas = getJCas(t1); FSIterator<Annotation> tokenIter = jCas.getAnnotationIndex(Token.typeIndexID).iterator(); tokenIter.moveTo(t1); assert (t1.equals(tokenIter.get())); tokenIter.moveToNext(); return tokenIter.isValid() && tokenIter.get().equals(t2); }