Exemplo 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;
  }
Exemplo n.º 2
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);
     }
   }
 }
Exemplo n.º 3
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;
  }