/**
* Java WordNet Library (JWNL)
* See the documentation for copyright information.
*/
package net.didion.jwnl.data;
import net.didion.jwnl.JWNLException;
import net.didion.jwnl.data.list.PointerTargetNode;
import net.didion.jwnl.data.list.PointerTargetNodeList;
import net.didion.jwnl.data.list.PointerTargetTree;
import net.didion.jwnl.data.list.PointerTargetTreeNode;
import net.didion.jwnl.data.list.PointerTargetTreeNodeList;
import java.util.Iterator;
/**
* This class constains static methods for performing various pointer operations. A pointer from one synset/word to
* another connotes a relationship between those words. The type of the relationship is specified by the type
* of pointer. See the WordNet documentation for information on pointer types. To avoid confusion with
* the <code>Relationship</code> class, these relationships will be referred to as links.
*/
public final class PointerUtils {
/**
* Representation of infinite depth. Used to tell the pointer operations to
* return all links to an infinite depth.
*/
public static final int INFINITY = Integer.MAX_VALUE;
private static final PointerUtils INSTANCE = new PointerUtils();
public static PointerUtils getInstance() {
return INSTANCE;
}
private PointerUtils() {
}
/** Get the immediate parents of <code>synset</code> */
public PointerTargetNodeList getDirectHypernyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.HYPERNYM);
}
/** Get all of the ancestors of <code>synset</code> */
public PointerTargetTree getHypernymTree(Synset synset) throws JWNLException {
return getHypernymTree(synset, INFINITY);
}
/** Get all of the ancestors of <code>synset</code> to depth <code>depth</code> */
public PointerTargetTree getHypernymTree(Synset synset, int depth) throws JWNLException {
return new PointerTargetTree(synset, makePointerTargetTreeList(synset, PointerType.HYPERNYM, depth));
}
/** Get the immediate children of <code>synset</code> */
public PointerTargetNodeList getDirectHyponyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.HYPONYM);
}
/** Get all of the children of <code>synset</code> */
public PointerTargetTree getHyponymTree(Synset synset) throws JWNLException {
return getHyponymTree(synset, INFINITY);
}
/** Get all of the children of <code>synset</code> to depth <code>depth</code> */
public PointerTargetTree getHyponymTree(Synset synset, int depth) throws JWNLException {
return new PointerTargetTree(synset, makePointerTargetTreeList(synset, PointerType.HYPONYM, depth));
}
//
// other general operations
//
/** Get <code>synset</code>'s siblings (the hyponyms of its hypernyms) */
public PointerTargetNodeList getCoordinateTerms(Synset synset) throws JWNLException {
PointerTargetNodeList list = new PointerTargetNodeList();
for (Iterator itr = getDirectHypernyms(synset).iterator(); itr.hasNext();) {
list.addAll(getPointerTargets(((PointerTargetNode) itr.next()).getSynset(), PointerType.HYPONYM));
}
return list;
}
/** Get the words that mean the opposite of <code>synset</code> */
public PointerTargetNodeList getAntonyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.ANTONYM);
}
/** Get the words that mean the opposite of <code>synset</code> and the immediate synonyms of those words */
public PointerTargetTree getExtendedAntonyms(Synset synset) throws JWNLException {
return getExtendedAntonyms(synset, 1);
}
/** Find all antonyms of <code>synset</code>, and all synonyms of those antonyms to depth <code>depth</code>. */
public PointerTargetTree getExtendedAntonyms(Synset synset, int depth) throws JWNLException {
PointerTargetTreeNodeList list = new PointerTargetTreeNodeList();
if (synset.getPOS() == POS.ADJECTIVE) {
PointerTargetNodeList antonyms = getAntonyms(synset);
list = makePointerTargetTreeList(antonyms, PointerType.SIMILAR_TO, PointerType.ANTONYM, depth, false);
}
return new PointerTargetTree(new PointerTargetTreeNode(synset, list, null));
}
/** Get the immediate antonyms of all words that mean the same as <code>synset</code>. */
public PointerTargetTree getIndirectAntonyms(Synset synset) throws JWNLException {
return getIndirectAntonyms(synset, 1);
}
/** Get the antonyms of all words that mean the same as <code>synset</code> to depth <code>depth</code>.*/
public PointerTargetTree getIndirectAntonyms(Synset synset, int depth) throws JWNLException {
PointerTargetTreeNodeList list = new PointerTargetTreeNodeList();
if (synset.getPOS() == POS.ADJECTIVE) {
PointerTargetNodeList synonyms = getSynonyms(synset);
list = makePointerTargetTreeList(synonyms, PointerType.ANTONYM, PointerType.ANTONYM, depth, false);
}
return new PointerTargetTree(new PointerTargetTreeNode(synset, list, null));
}
/** Get the attributes of <code>synset</code> */
public PointerTargetNodeList getAttributes(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.ATTRIBUTE);
}
/** Find what words are related to <code>synset</code> */
public PointerTargetNodeList getAlsoSees(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.SEE_ALSO);
}
/** Find all See Also relations to depth <code>depth</code>.*/
public PointerTargetTree getAlsoSeeTree(Synset synset, int depth) throws JWNLException {
return new PointerTargetTree(synset, makePointerTargetTreeList(synset, PointerType.SEE_ALSO, depth));
}
//
// noun operations
//
/** Get meronyms of <code>synset</code>. */
public PointerTargetNodeList getMeronyms(Synset synset) throws JWNLException {
PointerTargetNodeList list = new PointerTargetNodeList();
list.addAll(getPartMeronyms(synset));
list.addAll(getMemberMeronyms(synset));
list.addAll(getSubstanceMeronyms(synset));
return list;
}
/** Get part meronyms of <code>synset</code> */
public PointerTargetNodeList getPartMeronyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.PART_MERONYM);
}
/** Get member meronyms of <code>synset</code> */
public PointerTargetNodeList getMemberMeronyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.MEMBER_MERONYM);
}
/** Get substance meronyms of <code>synset</code> */
public PointerTargetNodeList getSubstanceMeronyms(Synset synset) throws JWNLException {
return getPointerTargets(synset, PointerType.SUBSTANCE_MERONYM);
}
/** Get meronyms of <code>synset</code> and of all its ancestors */
public PointerTargetTree getInteritedMeronyms(Synset synset) throws JWNLException {
return getInheritedMeronyms(synset, INFINITY, INFINITY);
}
/**
* Get meronyms of each synset, to depth <code>pointerDepth</code> starting at
* <code>synset</code> and going for all of <code>synset</code>'s ancestors to depth
* <code>ancestorDepth</code>.
*/
public PointerTargetTree getInheritedMeronyms(Synset synset, int pointerDepth, int ancestorDepth)
throws JWNLException {
PointerType[] types = new PointerType[3];
types[0] = PointerType.PART_MERONYM;
types[1] = PointerType.MEMBER_MERONYM;
types[2] = PointerType.SUBSTANCE_MERONYM;
return makeInheritedTree(synset, types, null, pointerDepth, ancestorDepth, false);
}
/** Get part meronyms of <code>synset</code> and of all its ancestors */
public PointerTargetTree getInheritedPartMeronyms(Synset synset) throws JWNLException {
return getInheritedPartMeronyms(synset, INFINITY, INFINITY);
}
/**
* Get part meronyms of each synset, to depth <code>pointerDepth</code>, starting at
* <code>synset</code> and going for all of <code>synset</code>'s ancestors to depth
* <code>ancestorDepth</code>.
*/
public PointerTargetTree getInheritedPartMeronyms(