You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Diman Karagiozov <di...@tetracom.com> on 2010/09/17 08:49:37 UTC

How to work with ByteArray feature value

  Hi,

I am developing an NLP tool based on latest UIMA version (2.3.0 
incubating). Basically, the tool utilizes an aggregate engine with 
several primitive engines.

Currently, I am having a problem with the handling of ByteArray values 
of one of the annotation features.
Here is how am I setting the value of the feature:

                                 final ByteArray ba = new ByteArray( 
cas, wordHypernymsAsBytes.length );
                                 ba.copyFromArray( wordHypernymsAsBytes, 
0, 0, wordHypernymsAsBytes.length );
                                 token.setHypernyms( ba );

And here is how am I reading the ByteArray features (there will be more, 
so I need a generic way of reading them):
             List< Feature > annoFeatures = 
anno.getCAS().getJCas().getTypeSystem().getType( 
anno.getType().getName() ).getFeatures();
             Iterator< Feature > featIter = annoFeatures.iterator();
             ....
             while ( featIter.hasNext() ) {

                 Feature feature = featIter.next();
                 ...
                 if ( feature.getRange().getName().equalsIgnoreCase( 
"uima.cas.ByteArray" ) ) {

                     FeatureStructure fs = anno.getFeatureValue( feature );

                     if ( fs != null ) {
                         byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();

----> here is the problem actually. The bytes array contains a single 
byte (length of the array is 1). The "setter" above is setting something 
much longer.

                     }
                 }
             }

My question is how to read&write the ByteArray features?

Attached you can find the serialized CAS file as well...

greetings
Diman

Re: How to work with ByteArray feature value

Posted by Eddie Epstein <ea...@gmail.com>.
Hi Diman,

Yes, there is a bug in TypeSystemUtil.typeSystem2TypeSystemDescription().
The fix is already checked in against
https://issues.apache.org/jira/browse/UIMA-1877

Thanks for finding and helping to isolate this problem.
Eddie

On Thu, Sep 23, 2010 at 4:37 AM, Diman Karagiozov <di...@tetracom.com> wrote:
>  Hi Eddie,
>
> Indeed, the type system used for the de-serialization differs from the one
> used for serialization.
> In the process of serialization I am doing the following:
>
> // serialize the cas
>            final ByteArrayOutputStream osCas = new ByteArrayOutputStream();
>            XmiCasSerializer.serialize( cas.getCas(), osCas );
>
> // serialize the type system
>            final ByteArrayOutputStream osTS = new ByteArrayOutputStream();
>            TypeSystemUtil.typeSystem2TypeSystemDescription(
> cas.getTypeSystem() ).toXML( osTS );
>
> In the type system, before serialization, I have the feature feature:
> <featureDescription>
> <name>hypernyms</name>
> <description>List of hypernyms</description>
> <rangeTypeName>uima.cas.ByteArray</rangeTypeName>
> <multipleReferencesAllowed>true</multipleReferencesAllowed>
> </featureDescription>
>
> However, in the serialized version of the type system, the
>  "<multipleReferencesAllowed>true</multipleReferencesAllowed>" is missing
> and this causes the problem...
>
> <featureDescription>
> <name>hypernyms</name>
> <description/>
> <rangeTypeName>uima.cas.ByteArray</rangeTypeName>
> </featureDescription>
>
>
> I have also other ByteArray features and the "multipleReferencesAllowed" is
> missing for all of them in the serialized version.
>
> Is this a bug or there is another way to serialize the type system, so that
> the "multipleReferencesAllowed" is handled correctly?
>
> greetings
> Diman
>
> On 09/21/2010 12:13 AM, Eddie Epstein wrote:
>>
>> On Mon, Sep 20, 2010 at 9:50 AM, Eddie Epstein<ea...@gmail.com>
>>  wrote:
>>>
>>> Hi Diman,
>>>
>>> Indeed the problem is Xmi deserialization. It looks like ByteArray
>>> features must be defined with
>>>              <multipleReferencesAllowed>true</multipleReferencesAllowed>
>>>
>>> At first glance this is missing from our documentation. Will double check
>>> that.
>>>
>> In fact UIMA is working as designed. It appears that your XmiCas data
>> was serialized out with multipleReferencesAllowed=true for the feature
>> hypernyms, but then deserialized with a different typesystem where
>> multipleReferencesAllowed=false.
>>
>> Eddie
>
>

Re: How to work with ByteArray feature value

Posted by Diman Karagiozov <di...@tetracom.com>.
  Hi Eddie,

Indeed, the type system used for the de-serialization differs from the 
one used for serialization.
In the process of serialization I am doing the following:

// serialize the cas
             final ByteArrayOutputStream osCas = new 
ByteArrayOutputStream();
             XmiCasSerializer.serialize( cas.getCas(), osCas );

// serialize the type system
             final ByteArrayOutputStream osTS = new ByteArrayOutputStream();
             TypeSystemUtil.typeSystem2TypeSystemDescription( 
cas.getTypeSystem() ).toXML( osTS );

In the type system, before serialization, I have the feature feature:
<featureDescription>
<name>hypernyms</name>
<description>List of hypernyms</description>
<rangeTypeName>uima.cas.ByteArray</rangeTypeName>
<multipleReferencesAllowed>true</multipleReferencesAllowed>
</featureDescription>

However, in the serialized version of the type system, the  
"<multipleReferencesAllowed>true</multipleReferencesAllowed>" is missing 
and this causes the problem...

<featureDescription>
<name>hypernyms</name>
<description/>
<rangeTypeName>uima.cas.ByteArray</rangeTypeName>
</featureDescription>


I have also other ByteArray features and the "multipleReferencesAllowed" 
is missing for all of them in the serialized version.

Is this a bug or there is another way to serialize the type system, so 
that the "multipleReferencesAllowed" is handled correctly?

greetings
Diman

On 09/21/2010 12:13 AM, Eddie Epstein wrote:
> On Mon, Sep 20, 2010 at 9:50 AM, Eddie Epstein<ea...@gmail.com>  wrote:
>> Hi Diman,
>>
>> Indeed the problem is Xmi deserialization. It looks like ByteArray
>> features must be defined with
>>               <multipleReferencesAllowed>true</multipleReferencesAllowed>
>>
>> At first glance this is missing from our documentation. Will double check that.
>>
> In fact UIMA is working as designed. It appears that your XmiCas data
> was serialized out with multipleReferencesAllowed=true for the feature
> hypernyms, but then deserialized with a different typesystem where
> multipleReferencesAllowed=false.
>
> Eddie


Re: How to work with ByteArray feature value

Posted by Eddie Epstein <ea...@gmail.com>.
It seems possible to detect this kind of error in most cases. The
XmiCas only contains feature structures that are indexed or are
referenced by other feature structures. For the problem described in
this thread, after deserialization there are ByteArray FS that are not
indexed and are left unreferenced.

A problem is with out of typesystem data. Imagine that a primitive
Array type is referenced by an OOTS feature structure. The array FS
will be deserialized, but there is no way to know if it is referenced
by something OOTS.

Eddie

On Mon, Sep 20, 2010 at 6:00 PM, Marshall Schor <ms...@schor.com> wrote:
>
>
> On 9/20/2010 5:13 PM, Eddie Epstein wrote:
>> On Mon, Sep 20, 2010 at 9:50 AM, Eddie Epstein <ea...@gmail.com> wrote:
>>> Hi Diman,
>>>
>>> Indeed the problem is Xmi deserialization. It looks like ByteArray
>>> features must be defined with
>>>              <multipleReferencesAllowed>true</multipleReferencesAllowed>
>>>
>>> At first glance this is missing from our documentation. Will double check that.
>>>
>> In fact UIMA is working as designed.
>
> Do you think the design could be improved with some kind of error message that
> could help the user figure out what to change  :-)  ?  (Perhaps that's not
> possible in this case?)
>
> -Marshall
>> It appears that your XmiCas data
>> was serialized out with multipleReferencesAllowed=true for the feature
>> hypernyms, but then deserialized with a different typesystem where
>> multipleReferencesAllowed=false.
>>
>> Eddie
>>
>>
>

Re: How to work with ByteArray feature value

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

On 9/20/2010 5:13 PM, Eddie Epstein wrote:
> On Mon, Sep 20, 2010 at 9:50 AM, Eddie Epstein <ea...@gmail.com> wrote:
>> Hi Diman,
>>
>> Indeed the problem is Xmi deserialization. It looks like ByteArray
>> features must be defined with
>>              <multipleReferencesAllowed>true</multipleReferencesAllowed>
>>
>> At first glance this is missing from our documentation. Will double check that.
>>
> In fact UIMA is working as designed. 

Do you think the design could be improved with some kind of error message that
could help the user figure out what to change  :-)  ?  (Perhaps that's not
possible in this case?)

-Marshall
> It appears that your XmiCas data
> was serialized out with multipleReferencesAllowed=true for the feature
> hypernyms, but then deserialized with a different typesystem where
> multipleReferencesAllowed=false.
>
> Eddie
>
>

Re: How to work with ByteArray feature value

Posted by Eddie Epstein <ea...@gmail.com>.
On Mon, Sep 20, 2010 at 9:50 AM, Eddie Epstein <ea...@gmail.com> wrote:
> Hi Diman,
>
> Indeed the problem is Xmi deserialization. It looks like ByteArray
> features must be defined with
>              <multipleReferencesAllowed>true</multipleReferencesAllowed>
>
> At first glance this is missing from our documentation. Will double check that.
>

In fact UIMA is working as designed. It appears that your XmiCas data
was serialized out with multipleReferencesAllowed=true for the feature
hypernyms, but then deserialized with a different typesystem where
multipleReferencesAllowed=false.

Eddie

Re: How to work with ByteArray feature value

Posted by Eddie Epstein <ea...@gmail.com>.
Hi Diman,

Indeed the problem is Xmi deserialization. It looks like ByteArray
features must be defined with
              <multipleReferencesAllowed>true</multipleReferencesAllowed>

At first glance this is missing from our documentation. Will double check that.

Eddie

On Fri, Sep 17, 2010 at 10:06 AM, Diman Karagiozov <di...@tetracom.com> wrote:
>  Hi Eddie,
>
> thanks for the fast reply!
>
> What I've tried is:
>
>                        byte[] bytes = new byte[ ( ( ByteArrayFS ) fs
> ).size() ];
>                        for ( int i = 0; i < bytes.length; i++ ) {
>                            bytes[i] = ( ( ByteArrayFS ) fs ).get( i );
>                        }
>                        // byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();
>
>                        System.out.println( fs + "; " + bytes.length + "; " +
> Arrays.toString( bytes ) );
>
> This is the output of on the stdout:
> ByteArray
>   Array length: 2
>   Array elements: [16, 41]
> ; 2; [16, 41]
>
> Unfortunately, the bytes length is only 2...
>
> Is it possible that there is something wrong with the deserialization of the
> ByteArray feature? I've check in the UIMA source code and it appears that
> there is different handling of the ByteArray than other array features...
>
> greetings
> Diman
>
> On 09/17/2010 04:52 PM, Eddie Epstein wrote:
>>
>> Hi,
>>
>> The implementation of ByteArrayFS.toArray() doesn't look so good. As a
>> workaround, please try accessing the byte values one at a time using
>> the get(int i) method.
>>
>> By the way, an easier way to get the feature should be
>>    Feature feature = anno.getType().getFeatureByBaseName("hypernyms");
>>
>> Eddie
>>
>> On Fri, Sep 17, 2010 at 2:49 AM, Diman Karagiozov<di...@tetracom.com>
>>  wrote:
>>>
>>>  Hi,
>>>
>>> I am developing an NLP tool based on latest UIMA version (2.3.0
>>> incubating).
>>> Basically, the tool utilizes an aggregate engine with several primitive
>>> engines.
>>>
>>> Currently, I am having a problem with the handling of ByteArray values of
>>> one of the annotation features.
>>> Here is how am I setting the value of the feature:
>>>
>>>                                final ByteArray ba = new ByteArray( cas,
>>> wordHypernymsAsBytes.length );
>>>                                ba.copyFromArray( wordHypernymsAsBytes, 0,
>>> 0,
>>> wordHypernymsAsBytes.length );
>>>                                token.setHypernyms( ba );
>>>
>>> And here is how am I reading the ByteArray features (there will be more,
>>> so
>>> I need a generic way of reading them):
>>>            List<  Feature>  annoFeatures =
>>> anno.getCAS().getJCas().getTypeSystem().getType( anno.getType().getName()
>>> ).getFeatures();
>>>            Iterator<  Feature>  featIter = annoFeatures.iterator();
>>>            ....
>>>            while ( featIter.hasNext() ) {
>>>
>>>                Feature feature = featIter.next();
>>>                ...
>>>                if ( feature.getRange().getName().equalsIgnoreCase(
>>> "uima.cas.ByteArray" ) ) {
>>>
>>>                    FeatureStructure fs = anno.getFeatureValue( feature );
>>>
>>>                    if ( fs != null ) {
>>>                        byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();
>>>
>>> ---->  here is the problem actually. The bytes array contains a single
>>> byte
>>> (length of the array is 1). The "setter" above is setting something much
>>> longer.
>>>
>>>                    }
>>>                }
>>>            }
>>>
>>> My question is how to read&write the ByteArray features?
>>>
>>> Attached you can find the serialized CAS file as well...
>>>
>>> greetings
>>> Diman
>>>
>
>

Re: How to work with ByteArray feature value

Posted by Diman Karagiozov <di...@tetracom.com>.
  Hi Eddie,

thanks for the fast reply!

What I've tried is:

                         byte[] bytes = new byte[ ( ( ByteArrayFS ) fs 
).size() ];
                         for ( int i = 0; i < bytes.length; i++ ) {
                             bytes[i] = ( ( ByteArrayFS ) fs ).get( i );
                         }
                         // byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();

                         System.out.println( fs + "; " + bytes.length + 
"; " + Arrays.toString( bytes ) );

This is the output of on the stdout:
ByteArray
    Array length: 2
    Array elements: [16, 41]
; 2; [16, 41]

Unfortunately, the bytes length is only 2...

Is it possible that there is something wrong with the deserialization of 
the ByteArray feature? I've check in the UIMA source code and it appears 
that there is different handling of the ByteArray than other array 
features...

greetings
Diman

On 09/17/2010 04:52 PM, Eddie Epstein wrote:
> Hi,
>
> The implementation of ByteArrayFS.toArray() doesn't look so good. As a
> workaround, please try accessing the byte values one at a time using
> the get(int i) method.
>
> By the way, an easier way to get the feature should be
>     Feature feature = anno.getType().getFeatureByBaseName("hypernyms");
>
> Eddie
>
> On Fri, Sep 17, 2010 at 2:49 AM, Diman Karagiozov<di...@tetracom.com>  wrote:
>>   Hi,
>>
>> I am developing an NLP tool based on latest UIMA version (2.3.0 incubating).
>> Basically, the tool utilizes an aggregate engine with several primitive
>> engines.
>>
>> Currently, I am having a problem with the handling of ByteArray values of
>> one of the annotation features.
>> Here is how am I setting the value of the feature:
>>
>>                                 final ByteArray ba = new ByteArray( cas,
>> wordHypernymsAsBytes.length );
>>                                 ba.copyFromArray( wordHypernymsAsBytes, 0, 0,
>> wordHypernymsAsBytes.length );
>>                                 token.setHypernyms( ba );
>>
>> And here is how am I reading the ByteArray features (there will be more, so
>> I need a generic way of reading them):
>>             List<  Feature>  annoFeatures =
>> anno.getCAS().getJCas().getTypeSystem().getType( anno.getType().getName()
>> ).getFeatures();
>>             Iterator<  Feature>  featIter = annoFeatures.iterator();
>>             ....
>>             while ( featIter.hasNext() ) {
>>
>>                 Feature feature = featIter.next();
>>                 ...
>>                 if ( feature.getRange().getName().equalsIgnoreCase(
>> "uima.cas.ByteArray" ) ) {
>>
>>                     FeatureStructure fs = anno.getFeatureValue( feature );
>>
>>                     if ( fs != null ) {
>>                         byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();
>>
>> ---->  here is the problem actually. The bytes array contains a single byte
>> (length of the array is 1). The "setter" above is setting something much
>> longer.
>>
>>                     }
>>                 }
>>             }
>>
>> My question is how to read&write the ByteArray features?
>>
>> Attached you can find the serialized CAS file as well...
>>
>> greetings
>> Diman
>>


Re: How to work with ByteArray feature value

Posted by Eddie Epstein <ea...@gmail.com>.
Hi,

The implementation of ByteArrayFS.toArray() doesn't look so good. As a
workaround, please try accessing the byte values one at a time using
the get(int i) method.

By the way, an easier way to get the feature should be
   Feature feature = anno.getType().getFeatureByBaseName("hypernyms");

Eddie

On Fri, Sep 17, 2010 at 2:49 AM, Diman Karagiozov <di...@tetracom.com> wrote:
>  Hi,
>
> I am developing an NLP tool based on latest UIMA version (2.3.0 incubating).
> Basically, the tool utilizes an aggregate engine with several primitive
> engines.
>
> Currently, I am having a problem with the handling of ByteArray values of
> one of the annotation features.
> Here is how am I setting the value of the feature:
>
>                                final ByteArray ba = new ByteArray( cas,
> wordHypernymsAsBytes.length );
>                                ba.copyFromArray( wordHypernymsAsBytes, 0, 0,
> wordHypernymsAsBytes.length );
>                                token.setHypernyms( ba );
>
> And here is how am I reading the ByteArray features (there will be more, so
> I need a generic way of reading them):
>            List< Feature > annoFeatures =
> anno.getCAS().getJCas().getTypeSystem().getType( anno.getType().getName()
> ).getFeatures();
>            Iterator< Feature > featIter = annoFeatures.iterator();
>            ....
>            while ( featIter.hasNext() ) {
>
>                Feature feature = featIter.next();
>                ...
>                if ( feature.getRange().getName().equalsIgnoreCase(
> "uima.cas.ByteArray" ) ) {
>
>                    FeatureStructure fs = anno.getFeatureValue( feature );
>
>                    if ( fs != null ) {
>                        byte[] bytes = ( ( ByteArrayFS ) fs ).toArray();
>
> ----> here is the problem actually. The bytes array contains a single byte
> (length of the array is 1). The "setter" above is setting something much
> longer.
>
>                    }
>                }
>            }
>
> My question is how to read&write the ByteArray features?
>
> Attached you can find the serialized CAS file as well...
>
> greetings
> Diman
>