You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by "Dr. Armin Wegner" <ar...@googlemail.com> on 2013/10/16 17:55:55 UTC

HashMap as type feature

Hi,

I'd like to have a type feature that is a list of key-value pairs. The
number of pairs is unknown. What's best for this? Is it even possible?

Thanks,
Armin

Re: HashMap as type feature

Posted by Thomas Ginter <th...@utah.edu>.
Armin,

Yes.  Extracting the key set results in an array wherein the n-th element of the key array corresponds to the n-th element of the values array.  That is part of how the hash map is handled in Java.  Even if you implemented your own sorting algorithm for insertion the value would get inserted with the key and the corresponding key and values arrays would still match.  The only caveat would be if you decided to manipulate the keys array independently after getting it from the HashMap.

Thanks,

Thomas Ginter
801-448-7676
thomas.ginter@utah.edu




On Oct 17, 2013, at 8:43 AM, Armin.Wegner@bka.bund.de wrote:

> Hi Thomas,
> 
> thanks for your answer. Using HashMap, does the n-th element of keySet() always corresponds to the n-th element of values()? Is this a defined behavior in Java?
> 
> Cheers,
> Armin
> 
> -----Ursprüngliche Nachricht-----
> Von: Thomas Ginter [mailto:thomas.ginter@utah.edu] 
> Gesendet: Mittwoch, 16. Oktober 2013 18:53
> An: <us...@uima.apache.org>
> Betreff: Re: HashMap as type feature
> 
> Armin,
> 
> Our team does this with an annotation type designed to store feature vectors for Machine Learning applications.  In this case we use a StringArray feature for the keys and a StringArray feature for the values.  The StringArrays are pulled from a HashMap<String, String> vector variable and inserted into the features with the following code:
> 
> int size = vector.size();
> StringArray keys = new StringArray(jcas, size); StringArray values = new StringArray(jcas, size); keys.copyFromArray(vector.keySet().toArray(new String[size]), 0, 0, size); values.copyFromArray(vector.values().toArray(new String[size]), 0, 0, size);
> 
> Retrieving the values is fairly straightforward.  If you are using a static annotation type it can be as simple as:
> 
> StringArray keys = vector.getKeysArray();
> 
> If you parameterize our annotation type in the annotator you can use the name of the feature to get a Feature object reference then pull the StringArrays like so:
> 
> Type annotationTypeObj = aJCas.getRequiredType("com.my.Annotation"); //parameter is the canonized name of the Annotation type Feature keyFeature = annotationTypeObj.getFeatureByBaseName("keyFeatureName"); //the actual name of the feature storing the key StringArray reference Feature valuesFeature = annotationTypeObj.getFeatureByBaseName("valuesFeatureName"); //the name of the values feature
> 
> //Get a list of the annotation objects in the CAS then iterate through the list, for each annotation 'a' do the following to retrieve the keys and values
> 
> StringArray keys = (StringArray) vector.getFeatureValue(keysFeature);
> StringArray values = (StringArray) vector.getFeatureValue(valuesFeature);
> 
> If necessary you can retrieve a String[] from the StringArray FeatureStructure by calling the .toArray() method such as:
> 
> String[] keysArray = keys.toArray();
> 
> Let me know if you have any questions.
> 
> Thanks,
> 
> Thomas Ginter
> 801-448-7676
> thomas.ginter@utah.edu<ma...@utah.edu>
> 
> 
> 
> 
> On Oct 16, 2013, at 9:55 AM, Dr. Armin Wegner <ar...@googlemail.com>> wrote:
> 
> Hi,
> 
> I'd like to have a type feature that is a list of key-value pairs. The number of pairs is unknown. What's best for this? Is it even possible?
> 
> Thanks,
> Armin
> 


Re: HashMap as type feature

Posted by "Dr. Armin Wegner" <ar...@googlemail.com>.
Looks good, I will try it.

Thank you,
Armin

On 10/17/13, Richard Eckart de Castilho <re...@apache.org> wrote:
> You could also use the entrySet which gives you all the key/value pairs.
>
> -- Richard
>
> On 17.10.2013, at 16:43, <Ar...@bka.bund.de> wrote:
>
>> Hi Thomas,
>>
>> thanks for your answer. Using HashMap, does the n-th element of keySet()
>> always corresponds to the n-th element of values()? Is this a defined
>> behavior in Java?
>>
>> Cheers,
>> Armin
>
>

Re: HashMap as type feature

Posted by Richard Eckart de Castilho <re...@apache.org>.
You could also use the entrySet which gives you all the key/value pairs.

-- Richard

On 17.10.2013, at 16:43, <Ar...@bka.bund.de> wrote:

> Hi Thomas,
> 
> thanks for your answer. Using HashMap, does the n-th element of keySet() always corresponds to the n-th element of values()? Is this a defined behavior in Java?
> 
> Cheers,
> Armin


AW: HashMap as type feature

Posted by Ar...@bka.bund.de.
Hi Thomas,

thanks for your answer. Using HashMap, does the n-th element of keySet() always corresponds to the n-th element of values()? Is this a defined behavior in Java?

Cheers,
Armin

-----Ursprüngliche Nachricht-----
Von: Thomas Ginter [mailto:thomas.ginter@utah.edu] 
Gesendet: Mittwoch, 16. Oktober 2013 18:53
An: <us...@uima.apache.org>
Betreff: Re: HashMap as type feature

Armin,

Our team does this with an annotation type designed to store feature vectors for Machine Learning applications.  In this case we use a StringArray feature for the keys and a StringArray feature for the values.  The StringArrays are pulled from a HashMap<String, String> vector variable and inserted into the features with the following code:

int size = vector.size();
StringArray keys = new StringArray(jcas, size); StringArray values = new StringArray(jcas, size); keys.copyFromArray(vector.keySet().toArray(new String[size]), 0, 0, size); values.copyFromArray(vector.values().toArray(new String[size]), 0, 0, size);

Retrieving the values is fairly straightforward.  If you are using a static annotation type it can be as simple as:

StringArray keys = vector.getKeysArray();

If you parameterize our annotation type in the annotator you can use the name of the feature to get a Feature object reference then pull the StringArrays like so:

Type annotationTypeObj = aJCas.getRequiredType("com.my.Annotation"); //parameter is the canonized name of the Annotation type Feature keyFeature = annotationTypeObj.getFeatureByBaseName("keyFeatureName"); //the actual name of the feature storing the key StringArray reference Feature valuesFeature = annotationTypeObj.getFeatureByBaseName("valuesFeatureName"); //the name of the values feature

//Get a list of the annotation objects in the CAS then iterate through the list, for each annotation 'a' do the following to retrieve the keys and values

StringArray keys = (StringArray) vector.getFeatureValue(keysFeature);
StringArray values = (StringArray) vector.getFeatureValue(valuesFeature);

If necessary you can retrieve a String[] from the StringArray FeatureStructure by calling the .toArray() method such as:

String[] keysArray = keys.toArray();

Let me know if you have any questions.

Thanks,

Thomas Ginter
801-448-7676
thomas.ginter@utah.edu<ma...@utah.edu>




On Oct 16, 2013, at 9:55 AM, Dr. Armin Wegner <ar...@googlemail.com>> wrote:

Hi,

I'd like to have a type feature that is a list of key-value pairs. The number of pairs is unknown. What's best for this? Is it even possible?

Thanks,
Armin


Re: HashMap as type feature

Posted by Thomas Ginter <th...@utah.edu>.
Armin,

Our team does this with an annotation type designed to store feature vectors for Machine Learning applications.  In this case we use a StringArray feature for the keys and a StringArray feature for the values.  The StringArrays are pulled from a HashMap<String, String> vector variable and inserted into the features with the following code:

int size = vector.size();
StringArray keys = new StringArray(jcas, size);
StringArray values = new StringArray(jcas, size);
keys.copyFromArray(vector.keySet().toArray(new String[size]), 0, 0, size);
values.copyFromArray(vector.values().toArray(new String[size]), 0, 0, size);

Retrieving the values is fairly straightforward.  If you are using a static annotation type it can be as simple as:

StringArray keys = vector.getKeysArray();

If you parameterize our annotation type in the annotator you can use the name of the feature to get a Feature object reference then pull the StringArrays like so:

Type annotationTypeObj = aJCas.getRequiredType("com.my.Annotation"); //parameter is the canonized name of the Annotation type
Feature keyFeature = annotationTypeObj.getFeatureByBaseName("keyFeatureName"); //the actual name of the feature storing the key StringArray reference
Feature valuesFeature = annotationTypeObj.getFeatureByBaseName("valuesFeatureName"); //the name of the values feature

//Get a list of the annotation objects in the CAS then iterate through the list, for each annotation 'a' do the following to retrieve the keys and values

StringArray keys = (StringArray) vector.getFeatureValue(keysFeature);
StringArray values = (StringArray) vector.getFeatureValue(valuesFeature);

If necessary you can retrieve a String[] from the StringArray FeatureStructure by calling the .toArray() method such as:

String[] keysArray = keys.toArray();

Let me know if you have any questions.

Thanks,

Thomas Ginter
801-448-7676
thomas.ginter@utah.edu<ma...@utah.edu>




On Oct 16, 2013, at 9:55 AM, Dr. Armin Wegner <ar...@googlemail.com>> wrote:

Hi,

I'd like to have a type feature that is a list of key-value pairs. The
number of pairs is unknown. What's best for this? Is it even possible?

Thanks,
Armin


Re: HashMap as type feature

Posted by "Dr. Armin Wegner" <ar...@googlemail.com>.
I've done it this way. First, I defined a new type KeyValuePair of
type uima.cas.TOP. It has a feature named key of type uima.cas.String
and a second feature named value of type uima.cas.Integer. Second, I
added a uima.cas.FSArray of KeyValuePair to the annotation type. It
used the Component Description Editor for doing so which was quit
easy. FSArray is better to read in CAS Editor than FSList.

Cheers,
Armin

On 10/21/13, Marshall Schor <ms...@schor.com> wrote:
> The CAS doesn't natively support hash maps.  One possible motivation is that
> the
> design was aimed at allowing multiple independent systems to process the
> CAS
> data, including Annotators written in C++.  To support this, it would be
> necessary to implement a common hash map in multiple languages (Java, C++,
> Python, Perl, etc.),  which wasn't done.
>
> Richard's suggestion is a possibility, though.  The other thing that people
> sometimes do is to store map data in external resources, which can be
> shared
> among co-located Annotators (running in the same JVM).
>
> -Marshall
> On 10/16/2013 11:55 AM, Dr. Armin Wegner wrote:
>> Hi,
>>
>> I'd like to have a type feature that is a list of key-value pairs. The
>> number of pairs is unknown. What's best for this? Is it even possible?
>>
>> Thanks,
>> Armin
>>
>
>

Re: HashMap as type feature

Posted by Marshall Schor <ms...@schor.com>.
The CAS doesn't natively support hash maps.  One possible motivation is that the
design was aimed at allowing multiple independent systems to process the CAS
data, including Annotators written in C++.  To support this, it would be
necessary to implement a common hash map in multiple languages (Java, C++,
Python, Perl, etc.),  which wasn't done.

Richard's suggestion is a possibility, though.  The other thing that people
sometimes do is to store map data in external resources, which can be shared
among co-located Annotators (running in the same JVM).

-Marshall
On 10/16/2013 11:55 AM, Dr. Armin Wegner wrote:
> Hi,
>
> I'd like to have a type feature that is a list of key-value pairs. The
> number of pairs is unknown. What's best for this? Is it even possible?
>
> Thanks,
> Armin
>


Re: HashMap as type feature

Posted by "Dr. Armin Wegner" <ar...@googlemail.com>.
Hi Richard,

thanks for the quick reply. But the answer is a little too short for
me. Can you point me to an example or a documentation, please? I
couldn't find it in the UIMA Tutorial and Developers' Guides. Did I
miss it?

Best,
Armin

On 10/16/13, Richard Eckart de Castilho <re...@apache.org> wrote:
> Hi,
>
> you could define a feature structure e.g.
>
> StringStringMapEntry {
>   String key
>   String value
> }
>
> and store these in an FSList. Then, write additional convenience code around
> that
> which transforms this to/from a Map<String, String>.
>
> -- Richard
>
> On 16.10.2013, at 17:55, Dr. Armin Wegner <ar...@googlemail.com>
> wrote:
>
>> Hi,
>>
>> I'd like to have a type feature that is a list of key-value pairs. The
>> number of pairs is unknown. What's best for this? Is it even possible?
>>
>> Thanks,
>> Armin
>
>

Re: HashMap as type feature

Posted by Richard Eckart de Castilho <re...@apache.org>.
TOP or AnnotationBase (which contains view-related code) would be the appropriate types, I believe.

It is not possible to store arbitrary objects in the CAS.

-- Richard

On 17.10.2013, at 16:33, <Ar...@bka.bund.de> wrote:

> Dear Richard,
> 
> to use StringStringMapEntry, needn't it subclass TOP or FeatureStructure? Is it possible to store an arbitray object into a CAS?
> 
> Cheers,
> Armin
> 
> -----Ursprüngliche Nachricht-----
> Von: Richard Eckart de Castilho [mailto:rec@apache.org] 
> Gesendet: Mittwoch, 16. Oktober 2013 18:02
> An: user@uima.apache.org
> Betreff: Re: HashMap as type feature
> 
> Hi,
> 
> you could define a feature structure e.g.
> 
> StringStringMapEntry {
>  String key
>  String value
> }
> 
> and store these in an FSList. Then, write additional convenience code around that which transforms this to/from a Map<String, String>.
> 
> -- Richard
> 
> On 16.10.2013, at 17:55, Dr. Armin Wegner <ar...@googlemail.com> wrote:
> 
>> Hi,
>> 
>> I'd like to have a type feature that is a list of key-value pairs. The 
>> number of pairs is unknown. What's best for this? Is it even possible?
>> 
>> Thanks,
>> Armin


AW: HashMap as type feature

Posted by Ar...@bka.bund.de.
Dear Richard,

to use StringStringMapEntry, needn't it subclass TOP or FeatureStructure? Is it possible to store an arbitray object into a CAS?

Cheers,
Armin

-----Ursprüngliche Nachricht-----
Von: Richard Eckart de Castilho [mailto:rec@apache.org] 
Gesendet: Mittwoch, 16. Oktober 2013 18:02
An: user@uima.apache.org
Betreff: Re: HashMap as type feature

Hi,

you could define a feature structure e.g.

StringStringMapEntry {
  String key
  String value
}

and store these in an FSList. Then, write additional convenience code around that which transforms this to/from a Map<String, String>.

-- Richard

On 16.10.2013, at 17:55, Dr. Armin Wegner <ar...@googlemail.com> wrote:

> Hi,
> 
> I'd like to have a type feature that is a list of key-value pairs. The 
> number of pairs is unknown. What's best for this? Is it even possible?
> 
> Thanks,
> Armin


Re: HashMap as type feature

Posted by Richard Eckart de Castilho <re...@apache.org>.
Hi,

you could define a feature structure e.g.

StringStringMapEntry {
  String key
  String value
}

and store these in an FSList. Then, write additional convenience code around that
which transforms this to/from a Map<String, String>.

-- Richard

On 16.10.2013, at 17:55, Dr. Armin Wegner <ar...@googlemail.com> wrote:

> Hi,
> 
> I'd like to have a type feature that is a list of key-value pairs. The
> number of pairs is unknown. What's best for this? Is it even possible?
> 
> Thanks,
> Armin