public static int isWNHypernym(Synset child, Synset parent, int depth) { initializeWordNet(); PointerTargetNodeList dp; if (child.equals(parent)) return 0; Synset cur = child; int curDepth = 0; try { while ((dp = PointerUtils.getInstance().getDirectHypernyms(cur)) != null && !dp.isEmpty() && curDepth < depth) { curDepth++; // if (dp.size() > 1) // throw new RuntimeException("More than 1 hypernym"); Object pt = dp.get(0); PointerTargetNode ptn = (PointerTargetNode) pt; cur = ptn.getSynset(); if (cur.equals(parent)) return curDepth; } } catch (Exception e) { throw new RuntimeException(e); } return -1; }
/** * @param v The synsets * @param t The Topic * @return The number of words in the synset also found in the topic */ public static int checkAmbig(Vector<Synset> v, Topic t) { int count = 0; boolean result = false; if (v == null) return count; for (Synset set : v) { Word[] w = set.getWords(); for (Word word : w) { String rec = word.getLemma(); String toLookup = WNLookup.getStaticStem(rec); result = t.containsKey(toLookup); if (result || t.containsKey(rec)) { count++; Log.logger.debug( "[found a related word on my mind: '" + toLookup + "', recent count for this topic '" + t.getName() + "' is: " + count + "]"); } } } return count; }
public static int isWNHypernym(Synset child, String parent) { initializeWordNet(); PointerTargetNodeList dp; if (child.getWord(0).getLemma().equalsIgnoreCase(parent)) return 0; Synset cur = child; int curDepth = 0; try { while ((dp = PointerUtils.getInstance().getDirectHypernyms(cur)) != null && !dp.isEmpty()) { curDepth++; // System.out.println("*****"); Object pt = dp.get(0); PointerTargetNode ptn = (PointerTargetNode) pt; // System.out.println(ptn.getSynset().getWords()[0].getLemma()); cur = ptn.getSynset(); // System.out.println(cur.getWord(0).getLemma()); if (cur.getWord(0).getLemma().equalsIgnoreCase(parent)) return curDepth; // if (dp.size() > 1) // throw new RuntimeException("More than 1 hypernym"); } } catch (Exception e) { throw new RuntimeException(e); } return -1; }
protected boolean isTermContainedInWords(Synset synset, String term) { for (int i = 0; i < synset.getWords().length; i++) { if (synset .getWord(i) .getLemma() .equalsIgnoreCase(term) /*|| synset.getWord(i).getLemma().contains(term)*/) return true; } return false; }
public static int getDistance(Synset s1, Synset s2, int max) { initializeWordNet(); PointerTargetNodeList dp; if (s1.equals(s2)) return 0; Synset cur = s2; int depth = 0, depth2 = isWNHypernym(s1, cur, max); if (depth2 >= 0) return depth2; try { while ((dp = PointerUtils.getInstance().getDirectHypernyms(cur)) != null && !dp.isEmpty() && depth < max) { depth++; Object pt = dp.get(0); PointerTargetNode ptn = (PointerTargetNode) pt; cur = ptn.getSynset(); // System.out.println(cur); if ((depth2 = isWNHypernym(s1, cur, max)) >= 0) return depth + depth2 < max ? depth + depth2 : max; } } catch (Exception e) { throw new RuntimeException(e); } return max; }
protected boolean isSynsetOfPOS(Synset synset, POS[] posES) { for (int i = 0; i < posES.length; i++) if (synset.getPOS() == posES[i]) return true; return false; }
public Map<RelationType, Set<String>> getAllRelatedWords(String word) { Map<RelationType, Set<String>> ret = createContainer(); identity.add(word); relatedWord.add(word); getAllSenses(word); if (allSenses.size() == 0) { relatedWordWithPOS.add(new String[] {word}); return ret; } Iterator<Synset> senseItr = allSenses.iterator(); Synset sense = null; Word[] words = null; Word w = null; POS pos = null; String lemma = null; while (senseItr.hasNext()) { sense = senseItr.next(); words = sense.getWords(); /** Synonyms */ String syn = null; for (int i = 0; i < words.length; i++) { w = words[i]; syn = w.getLemma(); pos = w.getPOS(); // System.out.println("Synonym:"+syn); synonyms.add(syn); // relatedWord.add(word); checkWordWithPOS(syn, pos); } /** Morphological variation */ POS sensePOS = sense.getPOS(); if (sensePOS == POS.NOUN) { addMorph(word + "s", sensePOS); addMorph(word + "es", sensePOS); addMorph(word.substring(0, word.length() - 1) + "ies", sensePOS); } else if (sensePOS == POS.VERB) { if (word.endsWith("e")) { addMorph(word + "r", sensePOS); addMorph(word.substring(0, word.length() - 1) + "ing", sensePOS); addMorph(word + "d", sensePOS); } else { addMorph(word + "er", sensePOS); addMorph(word + "ing", sensePOS); addMorph(word + "ed", sensePOS); } } /** Hypernyms, etc */ for (RelationType relation : RelationType.values()) { PointerTargetNodeList nodeList = null; Set<String> listToStore = null; // RelationType relation = relations[i]; try { switch (relation) { case hype: nodeList = pUtils.getDirectHypernyms(sense); break; case hypo: nodeList = pUtils.getDirectHyponyms(sense); break; case derv: nodeList = pUtils.getDerived(sense); break; case vgrp: nodeList = pUtils.getVerbGroup(sense); break; case cause: nodeList = pUtils.getCauses(sense); break; case entl: nodeList = pUtils.getEntailments(sense); break; case entlby: nodeList = pUtils.getEntailedBy(sense); break; case antm: nodeList = pUtils.getAntonyms(sense); break; case syn2: nodeList = pUtils.getSynonyms(sense); break; case alsoc: nodeList = pUtils.getAlsoSees(sense); break; case extd: // pUtils.getExtendedAntonyms(sense).print(); nodeList = (PointerTargetNodeList) pUtils.getExtendedAntonyms(sense).toList(); break; case indi: // pUtils.getIndirectAntonyms(sense).print(); nodeList = (PointerTargetNodeList) pUtils.getIndirectAntonyms(sense).toList(); break; } } catch (Exception e) { } if (nodeList != null) { listToStore = ret.get(relation); Iterator targetItr = nodeList.iterator(); PointerTargetNode pTargetNode = null; while (targetItr.hasNext()) { pTargetNode = (PointerTargetNode) targetItr.next(); if (!pTargetNode.isLexical()) { words = pTargetNode.getSynset().getWords(); for (int j = 0; j < words.length; j++) { w = words[j]; lemma = w.getLemma(); pos = w.getPOS(); if (lemma.contains("_")) { String[] parts = lemma.split("_"); if (parts.length == 2) { multiword.add(lemma.toLowerCase()); } } else { listToStore.add(lemma); checkWordWithPOS(lemma, pos); } } } else { w = pTargetNode.getWord(); lemma = w.getLemma(); pos = w.getPOS(); if (lemma.contains("_")) { String[] parts = lemma.split("_"); if (parts.length == 2) { multiword.add(lemma.toLowerCase()); } } else { listToStore.add(lemma); checkWordWithPOS(lemma, pos); } } } } nodeList = null; } } // end for all senses // System.out.println("Synonyms:"); // for(String w: synonyms) // System.out.println("\t"+w); // System.out.println("\nSynonym2:"); // for(String w: synonyms2) // System.out.println("\t"+w); // System.out.println("\nHypernyms:"); // for(String w: hypernyms) // System.out.println("\t"+w); // System.out.println("\nHyponyms:"); // for(String w: hyponyms) // System.out.println("\t"+w); // System.out.println("\nCoordinates:"); // for(String w: coordinates) // System.out.println("\t"+w); // System.out.println("\nDerived:"); // for(String w: derived) // System.out.println("\t"+w); // System.out.println("\nVerbGroup:"); // for(String w: verbGroup) // System.out.println("\t"+w); // System.out.println("\nCauses:"); // for(String w: causes) // System.out.println("\t"+w); // System.out.println("\nEntailments:"); // for(String w: entailments) // System.out.println("\t"+w); // System.out.println("\nEntailedBys:"); // for(String w: entailedBys) // System.out.println("\t"+w); // System.out.println("\nAntonym:"); // for(String w: antonyms) // System.out.println("\t"+w); // System.out.println("\nExtendedAntonym:"); // for(String w: extendedAntonyms) // System.out.println("\t"+w); // System.out.println("\nIndirectAntonym:"); // for(String w: indirectAntonyms) // System.out.println("\t"+w); // System.out.println("\nAlso See:"); // for(String w: alsosees) // System.out.println("\t"+w); return ret; }