コード例 #1
0
ファイル: WordNetConnector.java プロジェクト: boricles/nor2o
  protected boolean isMeronym(String term1, String term2, POS pos) {
    HashSet<String> res = new HashSet<String>();
    IndexWord iw;
    try {
      iw = wordnet.lookupIndexWord(pos, term1);
      if (iw == null) return false;

      PointerTargetTree holonyms = PointerUtils.getInstance().getInheritedHolonyms(iw.getSense(1));
      PointerTargetTree partHolonyms =
          PointerUtils.getInstance().getInheritedPartHolonyms(iw.getSense(1));
      PointerTargetTree substanceHolonyms =
          PointerUtils.getInstance().getInheritedSubstanceHolonyms(iw.getSense(1));
      PointerTargetTree memberHolonyms =
          PointerUtils.getInstance().getInheritedMemberHolonyms(iw.getSense(1));
      List list = (ArrayList) holonyms.toList();
      list.addAll((ArrayList) partHolonyms.toList());
      list.addAll((ArrayList) substanceHolonyms.toList());
      list.addAll((ArrayList) memberHolonyms.toList());
      for (int i = 0; i < list.size(); i++) {
        PointerTargetNodeList ptnl = (PointerTargetNodeList) list.get(i);
        for (int j = 0; j < ptnl.size(); j++) {
          PointerTargetNode ptn = (PointerTargetNode) ptnl.get(j);
          if (isTermContainedInWords(ptn.getSynset(), term2)) return true;
        }
      }
    } catch (JWNLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return false;
  }
コード例 #2
0
  public static int getWNSense(Synset[] synset1, Synset[] synset2) {
    for (Synset element : synset1) {
      for (int j = 0; j < synset2.length && j < NumericFeature.WN_SENSE_MAX; j++) {
        try {
          // get the highest hypernym
          PointerTargetNodeList dp;
          Synset cur = element;
          while ((dp = PointerUtils.getInstance().getDirectHypernyms(cur)) != null
              && !dp.isEmpty()) {
            Object pt = dp.get(0);
            PointerTargetNode ptn = (PointerTargetNode) pt;
            cur = ptn.getSynset();
          }
          // System.err.println("top level parent: "+cur.getWord(0).getLemma()+"
          // "+isWNHypernym(synset2[j],
          // cur)+" "+cur.equals(synset2[j]));
          if (isWNHypernym(synset2[j], cur)
              >= 0) // System.out.println(synset2[j]+" *** "+cur+" *** "+synset1[i]);
          return (j + 1);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }

    return NumericFeature.WN_SENSE_MAX;
  }
コード例 #3
0
  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;
  }
コード例 #4
0
  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;
  }
コード例 #5
0
  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;
  }
コード例 #6
0
  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;
  }