You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@uima.apache.org by Jörn Kottmann <ko...@gmail.com> on 2012/02/23 13:33:01 UTC

AnnotationFS.getCoveredText returns null?

Hi all,

the CasEditor code assumes that getCoveredText never returns
null.

Should that always be the case?

I have here a CAS with two DocumentAnnotations:

1. The one created by UIMA
2. An ArticleDocumentAnnotation (extends DocumentAnnotation)
     where the begin and end indexes are 0

The second one returns null when getCoveredText is called
(I can do more debugging when needed).

Jörn

Re: AnnotationFS.getCoveredText returns null?

Posted by Jörn Kottmann <ko...@gmail.com>.

On 02/23/2012 03:05 PM, Thilo Goetz wrote:
> This is the source code:
>
>    public String getCoveredText() {
>      final CAS casView = this.getView();
>      final String text = casView.getDocumentText();
>      if (text == null) {
>        return null;
>      }
>      return text.substring(getBegin(), getEnd());
>    }
>
> Your ArticleDocumentAnnotation probably belongs to a different
> view or sofa or whatever the right terminology is, which doesn't
> have a document text.
>


Should we update the javadoc and say that it returns null in case
there is no document text?

I will update the Cas Editor to handle this null case correctly.

Jörn

Re: AnnotationFS.getCoveredText returns null?

Posted by Marshall Schor <ms...@schor.com>.

On 2/23/2012 10:07 AM, Jörn Kottmann wrote:
> On 02/23/2012 03:38 PM, Thilo Goetz wrote:
>> Indeed.  I sometimes use negative values for offsets when I want
>> to create an annotation but don't know what the final offsets will
>> be.  Of course it is my responsibility to later set proper values.
>
> What do you think about returning just null when the annotation
> span is invalid?

My feeling is that is less useful than signalling an error.  I think it's 
probably fine to have the error be the index out of range from Java, with its 
stack trace.

-Marshall
>
> Jörn
>

Re: AnnotationFS.getCoveredText returns null?

Posted by Tommaso Teofili <to...@gmail.com>.
2012/2/28 Jörn Kottmann <ko...@gmail.com>

> On 02/27/2012 04:25 PM, Tommaso Teofili wrote:
>
>> 2012/2/27 Thilo Goetz<tw...@gmx.de>
>>
>>  On 24/02/12 18:15, Tommaso Teofili wrote:
>>> ...
>>>
>>>> However I don't like to just let the IndexOutOfBoundException raise as
>>>>
>>> that
>>>
>>>> may sound like a bug or unhandled situation while the API should
>>>>
>>> explicitly
>>>
>>>> declare a dedicated exception for when the begin and end offset don't
>>>>
>>> cover
>>>
>>>> a fully existing span of text.
>>>>
>>>> Tommaso
>>>>
>>> That would be a non-backward compatible change on one of our
>>> oldest and most widely used APIs.
>>>
>>>  Yes I know and that's not so good but I think that, if we happen to
>> agree
>> the current behavior could be improved, at some point in time (next major
>> version perhaps) we may choose to  break that in order to have a smarter
>> behavior.
>> So not in 2.4.x but maybe for 2.5+ or, better, 3.x versions that would be
>> (IMHO) a nice API improvement.
>> Tommaso
>>
>
> Even then you might just want to create a new method to retrieve the
> covered text and deprecate our current one.


right, that'd be very good.
Tommaso


>
>
> Jörn
>

Re: AnnotationFS.getCoveredText returns null?

Posted by Jörn Kottmann <ko...@gmail.com>.
On 02/27/2012 04:25 PM, Tommaso Teofili wrote:
> 2012/2/27 Thilo Goetz<tw...@gmx.de>
>
>> On 24/02/12 18:15, Tommaso Teofili wrote:
>> ...
>>> However I don't like to just let the IndexOutOfBoundException raise as
>> that
>>> may sound like a bug or unhandled situation while the API should
>> explicitly
>>> declare a dedicated exception for when the begin and end offset don't
>> cover
>>> a fully existing span of text.
>>>
>>> Tommaso
>> That would be a non-backward compatible change on one of our
>> oldest and most widely used APIs.
>>
> Yes I know and that's not so good but I think that, if we happen to agree
> the current behavior could be improved, at some point in time (next major
> version perhaps) we may choose to  break that in order to have a smarter
> behavior.
> So not in 2.4.x but maybe for 2.5+ or, better, 3.x versions that would be
> (IMHO) a nice API improvement.
> Tommaso

Even then you might just want to create a new method to retrieve the
covered text and deprecate our current one.

Jörn

Re: AnnotationFS.getCoveredText returns null?

Posted by Tommaso Teofili <to...@gmail.com>.
2012/2/27 Thilo Goetz <tw...@gmx.de>

> On 24/02/12 18:15, Tommaso Teofili wrote:
> ...
> > However I don't like to just let the IndexOutOfBoundException raise as
> that
> > may sound like a bug or unhandled situation while the API should
> explicitly
> > declare a dedicated exception for when the begin and end offset don't
> cover
> > a fully existing span of text.
> >
> > Tommaso
>
> That would be a non-backward compatible change on one of our
> oldest and most widely used APIs.
>

Yes I know and that's not so good but I think that, if we happen to agree
the current behavior could be improved, at some point in time (next major
version perhaps) we may choose to  break that in order to have a smarter
behavior.
So not in 2.4.x but maybe for 2.5+ or, better, 3.x versions that would be
(IMHO) a nice API improvement.
Tommaso



>
> --Thilo
>
>
>

Re: AnnotationFS.getCoveredText returns null?

Posted by Thilo Goetz <tw...@gmx.de>.
On 24/02/12 18:15, Tommaso Teofili wrote:
...
> However I don't like to just let the IndexOutOfBoundException raise as that
> may sound like a bug or unhandled situation while the API should explicitly
> declare a dedicated exception for when the begin and end offset don't cover
> a fully existing span of text.
> 
> Tommaso

That would be a non-backward compatible change on one of our
oldest and most widely used APIs.

--Thilo



Re: AnnotationFS.getCoveredText returns null?

Posted by Tommaso Teofili <to...@gmail.com>.
2012/2/23 Jörn Kottmann <ko...@gmail.com>

> On 02/23/2012 04:57 PM, Thilo Goetz wrote:
>
>> So if you ask me, the current behavior of getCoveredText() is
>> fine, it just needs to be documented.
>>
>
> +1 to add documentation.
>
> Shouldn't we then throw an exception as well when
> the sofa is null?
>
> You want to get the text, that is either possible or not.
> It should be easy for user code to detect that.
>
> With the current implementation you have to check
> the range and for a null value.


I agree here that also explicitly handle when the sofa is null.

However I don't like to just let the IndexOutOfBoundException raise as that
may sound like a bug or unhandled situation while the API should explicitly
declare a dedicated exception for when the begin and end offset don't cover
a fully existing span of text.

Tommaso


>
>
> Jörn
>

Re: AnnotationFS.getCoveredText returns null?

Posted by Jörn Kottmann <ko...@gmail.com>.
On 02/23/2012 04:57 PM, Thilo Goetz wrote:
> So if you ask me, the current behavior of getCoveredText() is
> fine, it just needs to be documented.

+1 to add documentation.

Shouldn't we then throw an exception as well when
the sofa is null?

You want to get the text, that is either possible or not.
It should be easy for user code to detect that.

With the current implementation you have to check
the range and for a null value.

Jörn

Re: AnnotationFS.getCoveredText returns null?

Posted by Thilo Goetz <tw...@gmx.de>.
On 23/02/12 16:07, Jörn Kottmann wrote:
> On 02/23/2012 03:38 PM, Thilo Goetz wrote:
>> Indeed.  I sometimes use negative values for offsets when I want
>> to create an annotation but don't know what the final offsets will
>> be.  Of course it is my responsibility to later set proper values.
> 
> What do you think about returning just null when the annotation
> span is invalid?
> 
> Jörn

I assume you are talking about getCoveredText() now?  The current
code, as Tommaso observes, will throw an exception when the offsets
are invalid.  To my taste, that is the correct behavior, although
it could be argued that we should throw our own exception there.
I also agree that the cases where it returns null should be
documented.

I was arguing against checking for validity of offsets when
annotations are created.  That might break existing code, as you
observed.

So if you ask me, the current behavior of getCoveredText() is
fine, it just needs to be documented.

--Thilo

Re: AnnotationFS.getCoveredText returns null?

Posted by Jörn Kottmann <ko...@gmail.com>.
On 02/23/2012 03:38 PM, Thilo Goetz wrote:
> Indeed.  I sometimes use negative values for offsets when I want
> to create an annotation but don't know what the final offsets will
> be.  Of course it is my responsibility to later set proper values.

What do you think about returning just null when the annotation
span is invalid?

Jörn

Re: AnnotationFS.getCoveredText returns null?

Posted by Thilo Goetz <tw...@gmx.de>.
On 23/02/12 15:28, Jörn Kottmann wrote:
> On 02/23/2012 03:14 PM, Tommaso Teofili wrote:
>> BTW it looks like this piece of code is prone to error due to incorrect
>> setting of begin and end.
>> If I have a text 'abc' and set begin to 0 and end to 5 then I'll have an
>> error only when I call the getCoveredText() method so I think we should
>> either check if the input is correct in setters or inside
>> getCoveredText()
>> to avoid IndexOutOfBoundExceptions generated from the substring method.
>> Am I right or did I miss something that already prevents that situation?
> 
> Well, the one who writes to the CAS needs to get this right. To make it
> fail fast you have to throw an exception when an invalid range is set.
> 
> We could throw an IllegalArgumentException when an AnnotationFS is
> created, but
> might break existing working code. 

Indeed.  I sometimes use negative values for offsets when I want
to create an annotation but don't know what the final offsets will
be.  Of course it is my responsibility to later set proper values.

--Thilo

Or add options to the serializer and
> deserializer to throw an exceptions if ranges are incorrect (and other
> things).
> 
> The Cas Editor cannot really handle this currently. I guess we should
> get it fixed
> and then clearly display to the user that something is wrong..
> 
> Jörn
> 
> 
> 


Re: AnnotationFS.getCoveredText returns null?

Posted by Jörn Kottmann <ko...@gmail.com>.
On 02/23/2012 03:14 PM, Tommaso Teofili wrote:
> BTW it looks like this piece of code is prone to error due to incorrect
> setting of begin and end.
> If I have a text 'abc' and set begin to 0 and end to 5 then I'll have an
> error only when I call the getCoveredText() method so I think we should
> either check if the input is correct in setters or inside getCoveredText()
> to avoid IndexOutOfBoundExceptions generated from the substring method.
> Am I right or did I miss something that already prevents that situation?

Well, the one who writes to the CAS needs to get this right. To make it
fail fast you have to throw an exception when an invalid range is set.

We could throw an IllegalArgumentException when an AnnotationFS is 
created, but
might break existing working code. Or add options to the serializer and
deserializer to throw an exceptions if ranges are incorrect (and other 
things).

The Cas Editor cannot really handle this currently. I guess we should 
get it fixed
and then clearly display to the user that something is wrong..

Jörn




Re: AnnotationFS.getCoveredText returns null?

Posted by Tommaso Teofili <to...@gmail.com>.
BTW it looks like this piece of code is prone to error due to incorrect
setting of begin and end.
If I have a text 'abc' and set begin to 0 and end to 5 then I'll have an
error only when I call the getCoveredText() method so I think we should
either check if the input is correct in setters or inside getCoveredText()
to avoid IndexOutOfBoundExceptions generated from the substring method.
Am I right or did I miss something that already prevents that situation?
Tommaso

2012/2/23 Thilo Goetz <tw...@gmx.de>

> On 23/02/12 13:33, Jörn Kottmann wrote:
> > Hi all,
> >
> > the CasEditor code assumes that getCoveredText never returns
> > null.
> >
> > Should that always be the case?
> >
> > I have here a CAS with two DocumentAnnotations:
> >
> > 1. The one created by UIMA
> > 2. An ArticleDocumentAnnotation (extends DocumentAnnotation)
> >     where the begin and end indexes are 0
> >
> > The second one returns null when getCoveredText is called
> > (I can do more debugging when needed).
> >
> > Jörn
>
> This is the source code:
>
>  public String getCoveredText() {
>    final CAS casView = this.getView();
>    final String text = casView.getDocumentText();
>    if (text == null) {
>      return null;
>    }
>    return text.substring(getBegin(), getEnd());
>  }
>
> Your ArticleDocumentAnnotation probably belongs to a different
> view or sofa or whatever the right terminology is, which doesn't
> have a document text.
>
> --Thilo
>

Re: AnnotationFS.getCoveredText returns null?

Posted by Thilo Goetz <tw...@gmx.de>.
On 23/02/12 13:33, Jörn Kottmann wrote:
> Hi all,
> 
> the CasEditor code assumes that getCoveredText never returns
> null.
> 
> Should that always be the case?
> 
> I have here a CAS with two DocumentAnnotations:
> 
> 1. The one created by UIMA
> 2. An ArticleDocumentAnnotation (extends DocumentAnnotation)
>     where the begin and end indexes are 0
> 
> The second one returns null when getCoveredText is called
> (I can do more debugging when needed).
> 
> Jörn

This is the source code:

  public String getCoveredText() {
    final CAS casView = this.getView();
    final String text = casView.getDocumentText();
    if (text == null) {
      return null;
    }
    return text.substring(getBegin(), getEnd());
  }

Your ArticleDocumentAnnotation probably belongs to a different
view or sofa or whatever the right terminology is, which doesn't
have a document text.

--Thilo