Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

get hypernyms as a list of words

Help
amya
2006-01-22
2013-04-29
  • amya
    amya
    2006-01-22

    I would like to have the hypernyms of a specific word as a list (or a vector) of words. Can some one give me a sample code?
    Thanks.

     
    • devilspawn
      devilspawn
      2007-06-28

      A PointerTargetTree object is composed of PointerTargetTreeNode objects through the PointerUtils.makePointerTargetTreeList() function. These nodes  are extension of PointerTargetNode which is made of both the PointerTarget object (i.e. Synset object) and the PointerType, in this case PointerType.HYPONYM and the information related to it.

      I have tried to modify the PointerUtils.makePointerTargetTreeList() function but I have been unable to make it work for Word only. I think one option is to create a new PointerTarget object which only hold information about the lemma rather than using the Synset object.

      I have also tried to use an ArrayList instead of PointerTargetNodeList to create the hypernym/hyponym tree but have so far failed to make it work.

      I hope this helps.

       
    • devilspawn
      devilspawn
      2007-06-28

      private ArrayList getHypernym( PointerTargetTreeNodeList l, ArrayList p ) throws JWNLException {
        ArrayList parent = p;
        for (Iterator itr = l.iterator(); itr.hasNext();) {
          PointerTargetNode node = (PointerTargetNode) itr.next();
          Synset synset = node.getSynset();
          String name = synset.getWord(0).getLemma();
          parent.add(name);        
          PointerTargetNodeList targets = new PointerTargetNodeList(synset.getTargets(PointerType.HYPERNYM));
          if (targets.size() > 0) {
            parent = getHypernymTerm( targets, parent, name);
          }
        }
        return parent;
      }
         
      private ArrayList getHypernymTerm( PointerTargetNodeList l, ArrayList p, String s ) 
              throws JWNLException{
        ArrayList parent = p;
        if ( !s.equals("entity") ) {
          for (Iterator itr = l.iterator(); itr.hasNext();) {
            PointerTargetNode node = (PointerTargetNode) itr.next();
            Synset synset = node.getSynset();
            String term = synset.getWord(0).getLemma();
            parent.add(term);
            PointerTargetNodeList targets = new PointerTargetNodeList(synset.getTargets(PointerType.HYPERNYM) );
            if (targets.size() > 0) {
              parent = getHypernymTerm( targets, parent, term);
            }
          }
        }
        return parent;
      }

      This code does what you want.