public static void main(String args[]) { WordnetTester wordnetTester = new WordnetTester(); List hypernymList = wordnetTester.getHypernym("fauna"); for (int i = 0; i < hypernymList.size(); i++) { IWord word = (IWord) hypernymList.get(i); System.out.println( (new StringBuilder("Hypernym is:")).append(word.getLemma().toString()).toString()); } wordnetTester.getHypernym(hypernymList); }
public void getSynonym(List words) { for (int i = 0; i < words.size(); i++) { IWord word = (IWord) words.get(i); System.out.println((new StringBuilder("\n\nFor Word:")).append(word.getLemma()).toString()); List hypernymList = getHypernym(word.getLemma()); for (int j = 0; j < hypernymList.size(); j++) { IWord hypernym = (IWord) hypernymList.get(j); System.out.println((new StringBuilder("Hypernym:")).append(hypernym.getLemma()).toString()); } } }
List keepUniqueTerms(List hypernymsList) { ArrayList uniqueList = new ArrayList(); ArrayList uniqueHypernymList = new ArrayList(); for (int i = 0; i < hypernymsList.size(); i++) { IWord word = (IWord) hypernymsList.get(i); if (!uniqueList.contains(word.getLemma().toString())) { uniqueList.add(word.getLemma().toString()); uniqueHypernymList.add(word); } } return uniqueHypernymList; }
public List getSynonyms(String phrase) { List synonymsList = new ArrayList(); if (dict == null) { System.out.println("Dictionary is null"); System.exit(0); } IIndexWord idxWord = dict.getIndexWord(phrase, POS.NOUN); if (idxWord != null) { IWordID wordID = (IWordID) idxWord.getWordIDs().get(0); IWord word = dict.getWord(wordID); ISynset synset = word.getSynset(); List words = new ArrayList(); IWord w; for (Iterator iterator = synset.getWords().iterator(); iterator.hasNext(); words.add(w)) w = (IWord) iterator.next(); synonymsList = keepUniqueTerms(words); } return synonymsList; }
public boolean areSynoynyms(String word1, String word2) { boolean synonyms = false; List synoymList = getSynonyms(word1); List synoym1List = getSynonyms(word2); ArrayList word1List = new ArrayList(); ArrayList word2List = new ArrayList(); for (int i = 0; i < synoymList.size(); i++) { IWord word = (IWord) synoymList.get(i); word1List.add(word.getLemma()); } for (int i = 0; i < synoym1List.size(); i++) { IWord word = (IWord) synoym1List.get(i); word2List.add(word.getLemma()); } word1List.retainAll(word2List); if (word1List.size() > 0) synonyms = true; return synonyms; }
private Set<String> getHypernyms( String word, POS posTag, boolean firstSenseOnly, int maxIterations) { Set<String> hypernyms = new HashSet<String>(); IIndexWord iIndexWordRoot = dict.getIndexWord(word, posTag); if (iIndexWordRoot == null) { return hypernyms; // no senses found } List<IWordID> todo = iIndexWordRoot.getWordIDs(); int iterations = 0; while (iterations++ < maxIterations && !todo.isEmpty()) { List<IWordID> tmp = Lists.newArrayList(); // iterate over senses for (IWordID iWordId : todo) { IWord iWord1 = dict.getWord(iWordId); ISynset iSynset = iWord1.getSynset(); // multiple hypernym chains are possible for a synset for (ISynsetID iSynsetId : iSynset.getRelatedSynsets(Pointer.HYPERNYM)) { List<IWord> iWords = dict.getSynset(iSynsetId).getWords(); for (IWord iWord2 : iWords) { String lemma = iWord2.getLemma().replace('_', ' '); hypernyms.add(lemma); tmp.add(iWord2.getID()); } } if (firstSenseOnly) { break; } } todo = tmp; } return hypernyms; }
public List getHypernym(String phrase) { List hypernymsList = new ArrayList(); if (dict == null) { System.out.println("Dictionary is null"); System.exit(0); } IIndexWord idxWord = dict.getIndexWord(phrase, POS.NOUN); if (idxWord != null) { IWordID wordID = (IWordID) idxWord.getWordIDs().get(0); IWord word = dict.getWord(wordID); ISynset synset = word.getSynset(); List hypernyms = synset.getRelatedSynsets(Pointer.HYPERNYM); List words; for (Iterator iterator = hypernyms.iterator(); iterator.hasNext(); hypernymsList.addAll(words)) { ISynsetID sid = (ISynsetID) iterator.next(); words = dict.getSynset(sid).getWords(); } hypernymsList = keepUniqueTerms(hypernymsList); } return hypernymsList; }
public static void main(String[] args) throws IOException { String path = args[0]; String sentence = "The bank can guarantee deposits will eventually cover future tuition costs because it invests in adjustable-rate mortgage securities"; URL url = null; try { url = new URL("file", null, path); } catch (MalformedURLException e) { e.printStackTrace(); } if (url == null) return; // construct the dictionary object and open it IDictionary dict = new Dictionary(url); dict.open(); for (POS posVal : POS.values()) { IIndexWord idxWord = dict.getIndexWord("bank", posVal); if (dict.getIndexWord("bank", posVal) != null) { List<IWordID> wordSenses = idxWord.getWordIDs(); for (IWordID sense : wordSenses) { IWord word = dict.getWord(sense); String gloss = word.getSynset().getGloss(); calculateOverlap(gloss, sentence, posVal); } } } // printing the overlap map for (Entry<String, Integer> entry : overlapMap.entrySet()) { String key = entry.getKey().toString(); int value = entry.getValue(); System.out.println("------"); System.out.println("SENSE: " + key + "\t\tOVERLAP: " + value); List<String> displayOverlap = overlapWords.get(key); for (String str : displayOverlap) System.out.println(str); } // calculating the most likely sense int maxVal = -1; String sense = ""; for (Entry<String, Integer> entry : overlapMap.entrySet()) { if (entry.getValue() > maxVal) { maxVal = entry.getValue(); sense = entry.getKey(); } } System.out.println("------------------------"); System.out.println("MOST LIKELY SENSE IS: " + sense); System.out.println("VALUE OF ITS OVERLAP IS: " + maxVal); }
public Pair<List<String>, List<String>> cookLexicalItem( String text, ItemType textType, boolean discardStopwords) { try { List<String> cookedSentence = new ArrayList<String>(); Pair<List<String>, List<String>> out = new Pair<List<String>, List<String>>(null, null); for (String item : Arrays.asList(text.split(" "))) { if (item.trim().length() == 0) continue; switch (textType) { case SENSE_OFFSETS: cookedSentence.add(item); break; case SENSE_KEYS: IWord sense = WordNetUtils.getInstance().getSenseFromSenseKey(item); cookedSentence.add( GeneralUtils.fixOffset(sense.getSynset().getOffset(), sense.getPOS())); break; case WORD_SENSE: IWord snse = WordNetUtils.getInstance().mapWordSenseToIWord(item); cookedSentence.add(GeneralUtils.fixOffset(snse.getSynset().getOffset(), snse.getPOS())); break; case SURFACE_TAGGED: cookedSentence.add(item); break; } } if (textType.equals(ItemType.SURFACE)) { out = TextualSimilarity.getInstance().cookSentence(text); cookedSentence = out.first; } if (cookedSentence == null) cookedSentence = new ArrayList<String>(); List<String> newCS = new ArrayList<String>(); for (String s : cookedSentence) { // if it is a synset if (s.matches("[0-9]*\\-[anvr]")) { newCS.add(s); continue; } String comps[] = s.split("#"); String word = comps[0]; String ps = comps[1]; // otherwise check word exists in WordNet if (!TextualSimilarity.getInstance().isOOV(word, ps)) newCS.add(word + "#" + ps); } cookedSentence = newCS; return new Pair<List<String>, List<String>>(cookedSentence, out.second); } catch (Exception e) { e.printStackTrace(); } return null; }