Hi all,

I often find myself wanting to attach meta-data to built-in Java classes and particularly to the 'String' class. In a text-mining and NLP context, it is very useful for example to have a dictionary of strings where each string 'knows' its synonyms...Of course one can get around that by having a giant map from entities to synonyms. However, this adds an extra preprocessing step and also Java programmers enjoy this sort of encapsulation. Being able to store the synonyms as meta-data in the String object itself, essentially means that the object can be queried individually with no fuss...I'm not advocating any universally correct approaches here... I'm just sharing my experiences and where they led me...:-)
Both approaches (map-based, metadata-based) worked just fine...

In case anyone else is struggling to attach meta-data to any built-in Java type, here is what I did for the String class (file attached). It is very straight forward...basically you need to wrap the object in question and implement IObj...

Voila! Now MString instances support meta-data...YAY!

Jim

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
package PAnnotator.java;

import clojure.lang.IPersistentMap;
import clojure.lang.IObj;

//a wrapper around the String class that supports Clojure maps as meta-data
public final class MString implements IObj {

	private final String value;
	private final IPersistentMap meta;

	public MString(IPersistentMap meta, String value) {
		this.meta = meta;
		this.value = value;
	}
	public MString(String value) {
		this(null, value);
	}
	
	public String getString(){
	        return value;
	}

	public IObj withMeta(IPersistentMap meta) {
		return new MString(meta, value);
	}

	public IPersistentMap meta() {
		return meta;
	}
}

Reply via email to