import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;
import edu.smu.tspell.wordnet.NounSynset;
import edu.smu.tspell.wordnet.Synset;
import edu.smu.tspell.wordnet.SynsetType;
import edu.smu.tspell.wordnet.VerbSynset;
/**
*
*/
/**
* @author Abhijeet and Brian Magerko
* this class implements a BFS from a source node to the root node
* it returns a path from the source to the root node
*/
public class DistanceToRoot {
private ArrayList<Node> path;
private ArrayList<Node> expand;
private Map<Integer,Node> created;
public ArrayList<Node> getDistanceToRoot(Synset source)
{
/* initialize the path and expand arraylist and the map */
path = new ArrayList<Node>();
expand = new ArrayList<Node>();
created = new Hashtable<Integer,Node>();
/* create a node for the source */
Node node = new Node();
node.synset = source;
// node.marked = true;
/* add it to expand */
expand.add(node);
/* add it to created */
created.put(node.synset.hashCode(), node);
while(!expand.isEmpty())
{
Synset [] hypernyms = null;
/* remove the first node from the expand list */
Node u = expand.remove(0);
/* get the list of its parents */
if(source.getType() == SynsetType.NOUN)
{
NounSynset nounsyn = (NounSynset) (u.synset);
hypernyms = nounsyn.getHypernyms();
}
else if(source.getType() == SynsetType.VERB)
{
VerbSynset nounsyn = (VerbSynset) (u.synset);
hypernyms = nounsyn.getHypernyms();
}
/* if u has no parents then its a root */
if(hypernyms.length == 0)
{
/* generate the path
* u points to the root
* traverse using the previous of each root till source is reached
*/
Node current = u;
while(!current.synset.equals(source))
{
path.add(0, current);
current = current.previous;
}
/* this adds the source */
path.add(0,current);
return path;
}
/* go through all its parents */
for(Synset v : hypernyms)
{
/* check if a node is already created for this synset
* if YES then continue
* if NO create a node
*/
if(created.containsKey(v.hashCode()))
continue;
/* create a node for this parent */
Node n = new Node();
n.synset = v;
n.previous = u;
// n.marked = true;
expand.add(n);
created.put(n.synset.hashCode(), n);
}
}// while ends
return null;
}
class Node
{
Synset synset;
Node previous = null;
boolean marked = false;
}
}