package OntologyEntityFinder;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Class describing an ontology entity
 * @author Simeon Polfliet
 * (simeon.polfliet@ensimag.imag.fr)
 */
public class Entity {
	
	/** Ontology entity name */
	private String name;
	/** Set of words associated with the entity */
	private HashSet<String> words;
	
	/**
	 * Standard constructor
	 * @param n the entity name
	 */
	public Entity(String n){
		name = n;
		words = new HashSet<String>();
	}

	/**
	 * 
	 * @return the entity name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Add a word to this entity
	 * @param word The word to add
	 */
	public void addWord(String word){
		words.add(word);
	}
	
	/**
	 * Add all the words from the given list
	 * @param list the list of words to add
	 */
	public void addWord(LinkedList<String> list){
		words.addAll(list);
	}
	
	/**
	 * Remove a word from this entity
	 * @param word the word to remove
	 */
	public void delWord(String word){
		words.remove(word);
	}
	
	/**
	 * @param word the word to look for
	 * @return true if present, false if absent
	 */
	public boolean containsWord(String word){
		return words.contains(word);
	}
	
	/**
	 * Delete all the entity words who appears in the given list
	 * @param list words to remove
	 */
	public void delWord(LinkedList<String> list){
		words.removeAll(list);
	}	
	
	/**
	 * Compare a word with all the words of the entity
	 * and return the highest result
	 * @param str the word to compare with the entity
	 * @return the similarity between the word and the entity
	 */
	public double compareWord(String str){
		Iterator<String> it = words.iterator();
		double res = 0.0;
		double tmp = 0.0;
		while (it.hasNext()){
			tmp = StringOperation.compareWord(it.next(), str);
			if (tmp > res){
				res = tmp;
			}
		}
		return res;
	}
	
	/**
	 * Compare all the words of the list with all the words of the entity
	 * and return the mean of the highest value for each word
	 * @param listStr the list of words to compare with the entity
	 * @return the similarity between the set and the entity
	 */
	public double compareWord(LinkedList<String> listStr){
		Iterator<String> it = listStr.iterator();
		double res = 0.0;
		while (it.hasNext()){
			res = res + compareWord(it.next());
		}
		//Heuristic: Penalize the entities name with more words than the given list of words
		return res / listStr.size() - Math.max(0,(words.size() - listStr.size()) / 20.0);	
	}

	public String toString() {
		return "\nname=" + name + ", words=" + words;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Entity other = (Entity) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
