google-code-export / uimafit

Automatically exported from code.google.com/p/uimafit
2 stars 1 forks source link

Additional method selectRelative() for JCasUtil #120

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Especially for feature extraction it often is very useful to select just the 
annotation which is 3 steps to the left or two steps to the right.
This can be done with selectPreceeding() selectFollowing() but those return a 
list not the element itself.

selectRelative would allow for sr(-3) or sr(2) to get exactly the element one 
is looking for.

Possible (lazily coded) implementation:
    public static <T extends Annotation> T selectRelative(JCas cas, Class<T> type, Annotation ann, int index) {
        if (index > 0) {
            List<T> xs = JCasUtil.selectFollowing(cas, type, ann, index);
            return (xs.size() >= index) ? xs.get(index-1) : null;
        } else if (index < 0) {
            List<T> xs = JCasUtil.selectPreceding(cas, type, ann, -index);
            return (xs.size() >= -index) ? xs.get(-index-1) : null;
        } else {
            return null;
        }
    }

Original issue reported on code.google.com by torsten....@gmail.com on 30 Apr 2012 at 12:33

GoogleCodeExporter commented 9 years ago
I do not understand why "null" is returned when the index is 0. Imho it should 
return ann then.

Original comment by richard.eckart on 5 Jul 2012 at 9:49

GoogleCodeExporter commented 9 years ago
I agree.

Additionally, I think the other select*() implementations returning only one 
element, throw IllegalArgumentException if no element was found or something is 
wrong.
I think this should be return in the branches for index<0 and index>0 instead 
of null.

Original comment by torsten....@gmail.com on 6 Jul 2012 at 7:21

GoogleCodeExporter commented 9 years ago
Added the selectRelative methods.

Actually, selectSingle does throw an exception. I've left the "return null" for 
cases where the index is out of bounds though.

Which other methods do you refer to that should throw an exception? 
selectSingle does in fact throw one.

Original comment by richard.eckart on 15 Jul 2012 at 8:38

GoogleCodeExporter commented 9 years ago
I meant that selectRelative() should throw an IllegalArgumentException instead 
of returning null.

Original comment by torsten....@gmail.com on 15 Jul 2012 at 9:41

GoogleCodeExporter commented 9 years ago
I'm throwing a IndexOutOfBoundsException now. That seems more reasonable to me 
than an IllegalArgumentException.

Original comment by richard.eckart on 15 Jul 2012 at 9:54