Esempio n. 1
0
  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;
  }