예제 #1
0
 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());
     }
   }
 }
예제 #2
0
  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);
  }
예제 #3
0
  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;
  }
예제 #4
0
  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;
  }
예제 #5
0
  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;
  }
예제 #6
0
  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 Duple<CrownOperations.Reason, ISynset> getEstimatedSynonym(
      String targetLemma, Set<String> synonyms, POS pos, String gloss) {

    Counter<ISynset> synsetCounts = new ObjectCounter<ISynset>();

    List<String> lemmasInWn = new ArrayList<String>();
    for (String lemma : synonyms) {
      // Get the WordNet sysnet if it exists
      Set<ISynset> senses = WordNetUtils.getSynsets(dict, lemma, pos);
      if (senses.isEmpty()) continue;

      lemmasInWn.add(lemma);
      synsetCounts.countAll(senses);

      // Get the hypernyms of the synset and count their occurrence too
      for (ISynset synset : senses) {
        // Do a sanity check that avoids attaching this Entry if its
        // lemma appears anywhere near the synonoyms.  This check
        // potentially has some false positives since we might avoid
        // putting the lemma somewhere valid (in which case it would
        // have more than would valid location) but is used to avoid
        // noisy integration
        if (WordNetUtils.isAlreadyInWordNet(dict, targetLemma, pos, synset)) {
          return null;
        }

        for (ISynsetID hyper : synset.getRelatedSynsets(Pointer.HYPERNYM)) {
          ISynset hyperSyn = dict.getSynset(hyper);
          if (WordNetUtils.isAlreadyInWordNet(dict, targetLemma, pos, hyperSyn)) {
            return null;
          }
          synsetCounts.count(hyperSyn);
        }
      }
    }

    // Return null if we couldn't find any of the lemma's synonyms or
    // hyponyms in WordNet
    if (synsetCounts.items().isEmpty()) return null;

    // If there was only one lemma in this list in WordNet, try comparing
    // the glosses for just that word to find a match
    if (lemmasInWn.size() == 1) {
      double maxScore = 0;
      ISynset best = null;
      String bestGloss = null;
      Set<ISynset> candidateSynonymSynsets = WordNetUtils.getSynsets(dict, lemmasInWn.get(0), pos);
      for (ISynset candidate : candidateSynonymSynsets) {

        String wnExtendedGloss = WordNetUtils.getGlossWithoutExamples(candidate);
        double score = simFunc.compare(gloss, wnExtendedGloss);
        if (maxScore < score) {
          maxScore = score;
          best = candidate;
          bestGloss = wnExtendedGloss;
        }
      }

      CrownOperations.Reason r = new CrownOperations.Reason(getClass());
      r.set("relation_type", "synonym");
      r.set("heuristic", "single-synonym");
      r.set("max_score", maxScore);
      return new Duple<CrownOperations.Reason, ISynset>(r, best);
    } else {
      // Check for whether there were ties in the max
      ISynset mostFreq = synsetCounts.max();
      int mostFreqCount = synsetCounts.getCount(mostFreq);
      List<ISynset> ties = new ArrayList<ISynset>();
      for (ISynset syn : synsetCounts.items()) {
        int c = synsetCounts.getCount(syn);
        if (c == mostFreqCount) ties.add(syn);
      }

      // If there was only one synset that had the maximum count, then we
      // report this
      if (ties.size() == 1) {

        CrownOperations.Reason r = new CrownOperations.Reason(getClass());
        r.set("relation_type", "synonym");
        r.set("heuristic", "unambiguous-max");
        r.set("count", mostFreqCount);
        return new Duple<CrownOperations.Reason, ISynset>(r, mostFreq);
      }
      // Otherwise, we try breaking ties between the synsets using gloss
      // similarity
      else {

        double maxScore = 0;
        ISynset best = null;
        String bestGloss = null;
        for (ISynset candidate : ties) {
          String wnExtendedGloss = WordNetUtils.getGlossWithoutExamples(candidate);
          double score = simFunc.compare(gloss, wnExtendedGloss);
          if (maxScore < score) {
            maxScore = score;
            best = candidate;
            bestGloss = wnExtendedGloss;
          }
        }

        CrownOperations.Reason r = new CrownOperations.Reason(getClass());
        r.set("relation_type", "synonym");
        r.set("heuristic", "tied-synonyms");
        r.set("max_score", maxScore);
        return new Duple<CrownOperations.Reason, ISynset>(r, best);
      }
    }
  }