All Downloads are FREE. Search and download functionalities are using the official Maven repository.

justhalf.nlp.reader.acereader.WordLabel Maven / Gradle / Ivy

package justhalf.nlp.reader.acereader;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class WordLabel implements Comparable, Serializable{
	
	private static final long serialVersionUID = -2821034438335023157L;
	public static final Map LABELS = new HashMap();
	public static final Map LABELS_INDEX = new HashMap();
	
	public static WordLabel get(String form){
		if(!LABELS.containsKey(form)){
			WordLabel label = new WordLabel(form, LABELS.size());
			synchronized(LABELS){
				LABELS.put(form, label);
				LABELS_INDEX.put(label.id, label);
			}
		}
		return LABELS.get(form);
	}
	
	public static WordLabel get(int id){
		return LABELS_INDEX.get(id);
	}
	
	public String form;
	public int id;
	
	private WordLabel(String form, int id) {
		this.form = form;
		this.id = id;
	}

	@Override
	public int hashCode() {
		return form.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof WordLabel))
			return false;
		WordLabel other = (WordLabel) obj;
		if (form == null) {
			if (other.form != null)
				return false;
		} else if (!form.equals(other.form))
			return false;
		return true;
	}
	
	public String toString(){
		return String.format("%s(%d)", form, id);
	}

	@Override
	public int compareTo(WordLabel o) {
		return Integer.compare(id, o.id);
	}
	
	public static int compare(WordLabel o1, WordLabel o2){
		if(o1 == null){
			if(o2 == null) return 0;
			else return -1;
		} else {
			if(o2 == null) return 1;
			else return o1.compareTo(o2);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy