use of net.didion.jwnl.data.Synset in project lucida by claritylab.
the class WordNet method getHyponyms.
/**
* Looks up hyponyms of the given word, assuming that it is used in its most
* common sense.
*
* @param word a word
* @param pos its part of speech
* @return hyponyms or <code>null</code> if lookup failed
*/
public static String[] getHyponyms(String word, POS pos) {
Synset synset = getCommonSynset(word, pos);
if (synset == null)
return null;
Synset[] hyponyms = getHyponymSynsets(synset);
if (hyponyms == null)
return null;
return getLemmas(hyponyms);
}
use of net.didion.jwnl.data.Synset in project lucida by claritylab.
the class WordNet method getHaveSubstance.
/**
* Looks up substance meronyms of the given noun, assuming that it is used in
* its most common sense.
*
* @param noun a noun
* @return substance meronyms or <code>null</code> if lookup failed
*/
public static String[] getHaveSubstance(String noun) {
Synset synset = getCommonSynset(noun, NOUN);
if (synset == null)
return null;
Synset[] haveSubstance = getHasSubstanceSynsets(synset);
if (haveSubstance == null)
return null;
return getLemmas(haveSubstance);
}
use of net.didion.jwnl.data.Synset in project lucida by claritylab.
the class WordNet method expandEntity.
/**
* Expands an entity by looking up related entities.
*
* @param entity an entity
* @return related entities and their weights
*/
public Map<String, Double> expandEntity(String entity) {
if (!isCompoundNoun(entity))
return new Hashtable<String, Double>();
// synsets of related concepts
Map<Synset, Double> synsets = new Hashtable<Synset, Double>();
// synsets that have already been expanded
Map<Synset, Double> expanded = new Hashtable<Synset, Double>();
// get most common synset
double hurdle = TermExpander.MIN_EXPANSION_WEIGHT;
if (SYNONYM_WEIGHT >= hurdle) {
Synset synset = getCommonSynset(entity, NOUN);
if (synset != null)
synsets.put(synset, 1d);
}
// expand synsets
int pathLength = 0;
while (pathLength++ < MAX_PATH_LENGTH && synsets.size() > 0) {
// get synsets and their weights
Synset[] currSynsets = synsets.keySet().toArray(new Synset[synsets.size()]);
double[] currWeights = new double[synsets.size()];
for (int i = 0; i < synsets.size(); i++) currWeights[i] = synsets.get(currSynsets[i]);
for (int i = 0; i < currSynsets.length; i++) {
Synset synset = currSynsets[i];
double weight = currWeights[i];
// move to expanded synsets
if (synsets.get(synset) == weight)
synsets.remove(synset);
if (!expanded.containsKey(synset) || expanded.get(synset) < weight) {
expanded.put(synset, weight);
} else
continue;
// 'hypernym' relation
double hypernymWeight = weight * HYPERNYM_WEIGHT;
if (hypernymWeight >= hurdle) {
Synset[] hypernyms = getHypernymSynsets(synset);
for (Synset hypernym : hypernyms) if (!synsets.containsKey(hypernym) || synsets.get(hypernym) < hypernymWeight)
synsets.put(hypernym, hypernymWeight);
}
// 'hyponym' relation
double hyponymWeight = weight * HYPONYM_WEIGHT;
if (hyponymWeight >= hurdle) {
Synset[] hyponyms = getHyponymSynsets(synset);
for (Synset hyponym : hyponyms) if (!synsets.containsKey(hyponym) || synsets.get(hyponym) < hyponymWeight)
synsets.put(hyponym, hyponymWeight);
}
// 'member-of' relation
double memberOfWeight = weight * MEMBER_OF_WEIGHT;
if (memberOfWeight >= hurdle) {
Synset[] membersOf = getMemberOfSynsets(synset);
for (Synset memberOf : membersOf) if (!synsets.containsKey(memberOf) || synsets.get(memberOf) < memberOfWeight)
synsets.put(memberOf, memberOfWeight);
}
// 'substance-of' relation
double substanceOfWeight = weight * SUBSTANCE_OF_WEIGHT;
if (substanceOfWeight >= hurdle) {
Synset[] substancesOf = getSubstanceOfSynsets(synset);
for (Synset substanceOf : substancesOf) if (!synsets.containsKey(substanceOf) || synsets.get(substanceOf) < substanceOfWeight)
synsets.put(substanceOf, substanceOfWeight);
}
// 'part-of' relation
double partOfWeight = weight * PART_OF_WEIGHT;
if (partOfWeight >= hurdle) {
Synset[] partsOf = getPartOfSynsets(synset);
for (Synset partOf : partsOf) if (!synsets.containsKey(partOf) || synsets.get(partOf) < partOfWeight)
synsets.put(partOf, partOfWeight);
}
// 'has-member' relation
double hasMemberWeight = weight * HAS_MEMBER_WEIGHT;
if (hasMemberWeight >= hurdle) {
Synset[] haveMember = getHasMemberSynsets(synset);
for (Synset hasMember : haveMember) if (!synsets.containsKey(hasMember) || synsets.get(hasMember) < hasMemberWeight)
synsets.put(hasMember, hasMemberWeight);
}
// 'has-substance' relation
double hasSubstanceWeight = weight * HAS_SUBSTANCE_WEIGHT;
if (hasSubstanceWeight >= hurdle) {
Synset[] haveSubstance = getHasSubstanceSynsets(synset);
for (Synset hasSubstance : haveSubstance) if (!synsets.containsKey(hasSubstance) || synsets.get(hasSubstance) < hasSubstanceWeight)
synsets.put(hasSubstance, hasSubstanceWeight);
}
// 'has-part' relation
double hasPartWeight = weight * HAS_PART_WEIGHT;
if (hasPartWeight >= hurdle) {
Synset[] havePart = getHasPartSynsets(synset);
for (Synset hasPart : havePart) if (!synsets.containsKey(hasPart) || synsets.get(hasPart) < hasPartWeight)
synsets.put(hasPart, hasPartWeight);
}
}
}
for (Synset synset : synsets.keySet()) {
double weight = synsets.get(synset);
if (!expanded.containsKey(synset) || expanded.get(synset) < weight)
expanded.put(synset, weight);
}
// get concepts in synsets
Map<String, Double> expansions = new Hashtable<String, Double>();
for (Synset synset : expanded.keySet()) {
double weight = expanded.get(synset);
// direct synonyms
if (weight == 1)
weight = SYNONYM_WEIGHT;
for (String expansion : getLemmas(synset)) if (!expansions.containsKey(expansion) || expansions.get(expansion) < weight)
expansions.put(expansion, weight);
}
List<String> dropped = new ArrayList<String>();
for (String expansion : expansions.keySet()) if (expansion.equalsIgnoreCase(entity))
dropped.add(expansion);
for (String expansion : dropped) expansions.remove(expansion);
return expansions;
}
use of net.didion.jwnl.data.Synset in project lucida by claritylab.
the class WordNet method expandModifier.
/**
* Expands a modifier by looking up related modifiers.
*
* @param modifier a modifier
* @param pos its part of speech: <code>POS.ADJECTIVE</code> or
* <code>POS.ADVERB</code>
* @return related modifiers and their weights
*/
public Map<String, Double> expandModifier(String modifier, POS pos) {
if ((pos.equals(ADJECTIVE) && !isAdjective(modifier)) || (pos.equals(ADVERB) && !isAdverb(modifier)))
return new Hashtable<String, Double>();
// synsets of related concepts
Map<Synset, Double> synsets = new Hashtable<Synset, Double>();
// synsets that have already been expanded
Map<Synset, Double> expanded = new Hashtable<Synset, Double>();
// get most common synset
double hurdle = TermExpander.MIN_EXPANSION_WEIGHT;
if (SYNONYM_WEIGHT >= hurdle) {
Synset synset = getCommonSynset(modifier, pos);
if (synset != null)
synsets.put(synset, 1d);
}
// expand synsets
int pathLength = 0;
while (pathLength++ < MAX_PATH_LENGTH && synsets.size() > 0) {
// get synsets and their weights
Synset[] currSynsets = synsets.keySet().toArray(new Synset[synsets.size()]);
double[] currWeights = new double[synsets.size()];
for (int i = 0; i < synsets.size(); i++) currWeights[i] = synsets.get(currSynsets[i]);
for (int i = 0; i < currSynsets.length; i++) {
Synset synset = currSynsets[i];
double weight = currWeights[i];
// move to expanded synsets
if (synsets.get(synset) == weight)
synsets.remove(synset);
if (!expanded.containsKey(synset) || expanded.get(synset) < weight) {
expanded.put(synset, weight);
} else
continue;
// currently no relations other than synonyms
}
}
for (Synset synset : synsets.keySet()) {
double weight = synsets.get(synset);
if (!expanded.containsKey(synset) || expanded.get(synset) < weight)
expanded.put(synset, weight);
}
// get concepts in synsets
Map<String, Double> expansions = new Hashtable<String, Double>();
for (Synset synset : expanded.keySet()) {
double weight = expanded.get(synset);
// direct synonyms
if (weight == 1)
weight = SYNONYM_WEIGHT;
for (String expansion : getLemmas(synset)) if (!expansions.containsKey(expansion) || expansions.get(expansion) < weight)
expansions.put(expansion, weight);
}
List<String> dropped = new ArrayList<String>();
for (String expansion : expansions.keySet()) if (expansion.equalsIgnoreCase(modifier))
dropped.add(expansion);
for (String expansion : dropped) expansions.remove(expansion);
return expansions;
}
use of net.didion.jwnl.data.Synset in project lucida by claritylab.
the class WordNet method getHypernyms.
/**
* Looks up hypernyms of the given word, assuming that it is used in its
* most common sense.
*
* @param word a word
* @param pos its part of speech
* @return hypernyms or <code>null</code> if lookup failed
*/
public static String[] getHypernyms(String word, POS pos) {
Synset synset = getCommonSynset(word, pos);
if (synset == null)
return null;
Synset[] hypernyms = getHypernymSynsets(synset);
if (hypernyms == null)
return null;
return getLemmas(hypernyms);
}
Aggregations