Esempio n. 1
1
  public static synchronized Dictionary initializeWordNet() {

    if (wordnet != null) return wordnet;

    try {
      String propsFileText = FileUtils.readFile(Utils.class.getResourceAsStream(propsFile));
      Map<String, String> map = Maps.newTreeMap();
      map.put("WordNet_dictionary_path", Utils.getConfig().getString("WordNet_dictionary_path"));
      propsFileText = StringUtil.macroReplace(propsFileText, map);
      JWNL.initialize(new StringInputStream(propsFileText));
      // JWNL.initialize(new FileInputStream(propsFile));
      wordnet = Dictionary.getInstance();
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    SUPERTYPE_SYNSETS = new Synset[SUPERTYPES.length];
    Synset[] classSynset;
    IndexWord iw;
    int count = 0;
    for (String type : SUPERTYPES) {
      try {
        iw = wordnet.getIndexWord(POS.NOUN, type);
      } catch (JWNLException e) {
        throw new RuntimeException(e);
      }
      if (iw == null) {
        System.err.println(type);
        continue;
      }

      try {
        classSynset = iw.getSenses();
      } catch (JWNLException e) {
        throw new RuntimeException(e);
      }
      // System.err.println("**********************");
      if (classSynset.length > 1) {
        // for(Synset cs:classSynset)
        // System.err.println(cs);
        if (type.equals("abstraction")) {
          SUPERTYPE_SYNSETS[count] = classSynset[5];
        } else if (type.equals("measure")) {
          SUPERTYPE_SYNSETS[count] = classSynset[2];
        } else if (type.equals("state")) {
          SUPERTYPE_SYNSETS[count] = classSynset[3];
        } else if (type.equals("act")) {
          SUPERTYPE_SYNSETS[count] = classSynset[1];
        } else {
          SUPERTYPE_SYNSETS[count] = classSynset[0];
        }
      }
      count++;
    }
    if (wordnet == null) throw new RuntimeException("WordNet not intialized");
    else {
      System.out.println("Wordnet initialized " + wordnet);
    }
    return wordnet;
  }
Esempio n. 2
0
  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;
  }
Esempio n. 3
0
  /**
   * Gets the lemma of a word.
   *
   * @param word a word
   * @param pos part of speech
   * @return lemma or the input if it could not be lemmatized
   */
  public static String getLemma(String word, POS pos) {
    if (wDict == null) {
      return word;
    }
    IndexWord indexWord = null;

    try {
      indexWord = wDict.lookupIndexWord(pos, word);
    } catch (JWNLException e) {
    }

    return (indexWord != null) ? indexWord.getLemma() : word;
  }
Esempio n. 4
0
  @Override
  public String toString() {
    StringBuilder out = new StringBuilder("");
    out.append("Schema ---\n");
    out.append("Word Count: ").append(words.size()).append("\n");
    for (String s : words) {
      out.append(s).append(" ");
    }
    out.append("\n");
    for (IndexWord w : indexes) {
      out.append(w.getLemma()).append(":").append(w.getPOS().getLabel()).append(" ");
    }
    out.append("\n");

    return out.toString();
  }
Esempio n. 5
0
 public void getAllSenses(String word) {
   getAllIndexWords(word);
   allSenses.clear();
   Iterator<IndexWord> itr = allIndexWords.iterator();
   IndexWord iWord = null;
   while (itr.hasNext()) {
     iWord = itr.next();
     try {
       Synset[] senses = iWord.getSenses();
       for (int i = 0; i < senses.length; i++) {
         allSenses.add(senses[i]);
       }
     } catch (Exception e) {
       // System.out.println("getSynonym ERROR: " + word);
     }
   }
 }
Esempio n. 6
0
  protected String getSpecificRelation(
      IndexWord word1, String term1, IndexWord word2, String term2, POS[] posES) {
    String relation = "";
    int depth = 1110;
    // int depth = 1;
    try {
      List<PointerType> typesconcept1 = PointerType.getAllPointerTypesForPOS(word1.getPOS());
      List<PointerType> typesconcept2 = PointerType.getAllPointerTypesForPOS(word2.getPOS());

      Set<PointerType> pointersSet = new HashSet<PointerType>();
      pointersSet.addAll(typesconcept1);
      pointersSet.addAll(typesconcept2);

      Synset[] concept1Synsets = word1.getSenses();
      // Synset concept1Synset = concept1Synsets[0];

      Synset[] concept2Synsets = word2.getSenses();
      // Synset concept2Synset = concept2Synsets[0];

      List<PointerType> pts = PointerType.getAllPointerTypes();

      if (concept1Synsets != null && concept2Synsets != null) {
        Set<Relationship> relsSet = new HashSet<Relationship>();

        for (int i = 0; i < concept1Synsets.length; i++) {
          Synset concept1Synset, concept2Synset;

          if (isSynsetOfPOS(concept1Synsets[i], posES)
              && isTermContainedInWords(concept1Synsets[i], term1)) {
            concept1Synset = concept1Synsets[i];

            for (int j = 0; j < concept2Synsets.length; j++)
              if (isSynsetOfPOS(concept2Synsets[j], posES)
                  && isTermContainedInWords(concept2Synsets[i], term2)) {
                concept2Synset = concept2Synsets[j];

                for (PointerType pt : pts) {
                  // RelationshipList list = relationFinder.findRelationships(concept1Synset,
                  // concept2Synset, pt);
                  RelationshipList list =
                      relationFinder.findRelationships(
                          concept1Synset, concept2Synset, pt, RELATIONSHIP_DEEP);

                  if (list != null && !list.isEmpty()) {
                    Relationship rel = list.getShallowest();
                    if (rel.getDepth() < depth) depth = rel.getDepth();
                    relsSet.add(rel);
                    list.clear();
                    list = null;
                  }
                }
              }
          }
        }
        relation = getRelevantRelation(relsSet, depth);
      }
    } catch (JWNLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return relation;
  }