@SuppressWarnings("unchecked")
  public DictionaryCatalog(
      Dictionary dictionary,
      DictionaryFileType fileType,
      Class desiredDictionaryFileType,
      Map<String, Param> params)
      throws JWNLException {
    this.files = new EnumMap<POS, E>(POS.class);
    this.dictionary = dictionary;
    this.fileType = fileType;

    if (!params.containsKey(DICTIONARY_PATH_KEY)) {
      throw new JWNLException(
          dictionary.getMessages().resolveMessage("DICTIONARY_EXCEPTION_052", DICTIONARY_PATH_KEY));
    }
    String path = params.get(DICTIONARY_PATH_KEY).getValue();

    if (!params.containsKey(DICTIONARY_FILE_TYPE_KEY)) {
      throw new JWNLException(
          dictionary
              .getMessages()
              .resolveMessage("DICTIONARY_EXCEPTION_052", DICTIONARY_FILE_TYPE_KEY));
    }

    try {
      Class fileClass;
      try {
        fileClass = Class.forName(params.get(DICTIONARY_FILE_TYPE_KEY).getValue());
        if (!desiredDictionaryFileType.isAssignableFrom(fileClass)) {
          throw new JWNLException(
              dictionary
                  .getMessages()
                  .resolveMessage(
                      "DICTIONARY_EXCEPTION_003",
                      new Object[] {fileClass, desiredDictionaryFileType.getCanonicalName()}));
        }
      } catch (ClassNotFoundException e) {
        throw new JWNLException(
            dictionary.getMessages().resolveMessage("DICTIONARY_EXCEPTION_002"), e);
      }

      DictionaryFileFactory<E> factory =
          (DictionaryFileFactory<E>) params.get(DICTIONARY_FILE_TYPE_KEY).create();
      for (POS pos : POS.getAllPOS()) {
        E file = factory.newInstance(dictionary, path, pos, fileType);
        files.put(file.getPOS(), file);
      }
    } catch (JWNLException e) {
      throw new JWNLException(
          dictionary.getMessages().resolveMessage("DICTIONARY_EXCEPTION_018", fileType), e);
    }
  }
示例#2
0
  public Synset(Dictionary dictionary, POS pos) throws JWNLException {
    super(dictionary);
    if (null == pos) {
      throw new IllegalArgumentException(JWNL.resolveMessage("DICTIONARY_EXCEPTION_041"));
    }
    this.pos = pos;
    pointers = new PointerList();
    words = new WordList();

    if (null != dictionary && dictionary.isEditable()) {
      dictionary.addSynset(this);
    }
  }
示例#3
0
 @Override
 public void setDictionary(Dictionary dictionary) throws JWNLException {
   if (dictionary != this.dictionary) {
     if (null != this.dictionary) {
       Dictionary old = this.dictionary;
       this.dictionary = dictionary;
       old.removeElement(this);
     }
     super.setDictionary(dictionary);
     if (null != dictionary) {
       dictionary.addElement(this);
     }
   }
 }
 private static Set<Long> findNounHypernyms(Dictionary dic) throws SMatchException {
   log.info("Creating noun hypernyms array...");
   try {
     Set<Long> keys = new HashSet<>();
     int count = 0;
     Iterator<Synset> it = dic.getSynsetIterator(POS.NOUN);
     while (it.hasNext()) {
       count++;
       if (0 == count % 10000) {
         log.debug("noun hypernyms: " + count);
       }
       Synset source = it.next();
       long sourceOffset = source.getOffset();
       traverseTreeMG(keys, PointerUtils.getHypernymTree(source), sourceOffset);
       traverseTreeMG(keys, PointerUtils.getInheritedHolonyms(source), sourceOffset);
       traverseTreeMG(keys, PointerUtils.getInheritedMemberHolonyms(source), sourceOffset);
       traverseTreeMG(keys, PointerUtils.getInheritedPartHolonyms(source), sourceOffset);
       traverseTreeMG(keys, PointerUtils.getInheritedSubstanceHolonyms(source), sourceOffset);
       traverseListMG(keys, PointerUtils.getHolonyms(source), sourceOffset);
       traverseListMG(keys, PointerUtils.getMemberHolonyms(source), sourceOffset);
       traverseListMG(keys, PointerUtils.getPartHolonyms(source), sourceOffset);
       traverseListMG(keys, PointerUtils.getSubstanceHolonyms(source), sourceOffset);
     }
     log.info("Noun hypernyms: " + keys.size());
     return keys;
   } catch (JWNLException e) {
     throw new SMatchException(e.getClass().getSimpleName() + ": " + e.getMessage(), e);
   }
 }
 /* (non-Javadoc)
  * @see ac.biu.nlp.nlp.instruments.dictionary.wordnet.jwnl.PartialJwnlDictionary#getIndexWord(java.lang.String, ac.biu.nlp.nlp.instruments.dictionary.wordnet.WordNetPartOfSpeech)
  */
 protected IndexWord getIndexWord(String lemma, WordNetPartOfSpeech partOfSpeech)
     throws JWNLException {
   return doNotProcessThisWord(lemma)
       ? null
       : extJwnlRealDictionary.lookupIndexWord(
           ExtJwnlUtils.getJwnlPartOfSpeec(partOfSpeech), lemma);
 }
 private static Set<Long> findAdverbAntonyms(Dictionary dic) throws SMatchException {
   log.info("Creating adverb antonyms array...");
   try {
     Set<Long> keys = new HashSet<>();
     int count = 0;
     Iterator<Synset> it = dic.getSynsetIterator(POS.ADVERB);
     while (it.hasNext()) {
       count++;
       if (0 == count % 1000) {
         log.debug("adverb antonyms: " + count);
       }
       Synset source = it.next();
       long sourceOffset = source.getOffset();
       List<Pointer> pointers = source.getPointers(PointerType.ANTONYM);
       for (Pointer ptr : pointers) {
         long targetOffset = ptr.getTargetOffset();
         long key;
         if (targetOffset > sourceOffset) {
           key = (targetOffset << 32) + sourceOffset;
         } else {
           key = (sourceOffset << 32) + targetOffset;
         }
         keys.add(key);
       }
     }
     log.info("Adverbs antonyms: " + keys.size());
     return keys;
   } catch (JWNLException e) {
     throw new SMatchException(e.getClass().getSimpleName() + ": " + e.getMessage(), e);
   }
 }
 private static Set<Long> findNominalizations(Dictionary dic) throws SMatchException {
   log.info("Creating nominalizations array...");
   try {
     Set<Long> keys = new HashSet<>();
     int count = 0;
     Iterator<Synset> it = dic.getSynsetIterator(POS.VERB);
     while (it.hasNext()) {
       count++;
       if (0 == count % 1000) {
         log.debug("nominalizations: " + count);
       }
       Synset source = it.next();
       List<Pointer> pointers = source.getPointers(PointerType.DERIVATION);
       for (Pointer pointer : pointers) {
         if (POS.NOUN.equals(pointer.getTargetPOS())) {
           long targetOffset = pointer.getTargetOffset();
           long key = (source.getOffset() << 32) + targetOffset;
           keys.add(key);
         }
       }
     }
     log.info("Nominalizations: " + keys.size());
     return keys;
   } catch (JWNLException e) {
     throw new SMatchException(e.getClass().getSimpleName() + ": " + e.getMessage(), e);
   }
 }
  /**
   * Ctor
   *
   * @param wnDictionaryDir
   * @throws WordNetInitializationException
   */
  public ExtJwnlDictionary(File wnDictionaryDir) throws WordNetInitializationException {
    if (wnDictionaryDir == null) throw new WordNetInitializationException("null WN directory");
    if (!wnDictionaryDir.exists())
      throw new WordNetInitializationException(
          "given WN directory " + wnDictionaryDir + " doesn't exist");

    File propsFile = null;
    try {
      propsFile = ExtJwnlDictionaryInitializer.makePropsFile(wnDictionaryDir);
      this.extJwnlRealDictionary = Dictionary.getInstance(new FileInputStream(propsFile));
    } catch (FileNotFoundException e) {
      throw new WordNetInitializationException("props file " + propsFile + " not found", e);
    } catch (net.sf.extjwnl.JWNLException e) {
      throw new WordNetInitializationException(
          "error instantiating a Dictionary with " + propsFile, e);
    }
  }
  /**
   * Return all {@link Synset}s of a particular POS
   *
   * @param pos
   * @return
   * @throws WordNetException
   */
  public List<Synset> getAllWords(WordNetPartOfSpeech pos) throws WordNetException {
    List<Synset> synsets = new ArrayList<Synset>();
    try {
      @SuppressWarnings("rawtypes")
      Iterator iter =
          extJwnlRealDictionary.getIndexWordIterator(ExtJwnlUtils.getJwnlPartOfSpeec(pos));
      while (iter.hasNext()) {
        IndexWord indexWord = (IndexWord) iter.next();
        synsets.addAll(indexWordToList(indexWord));
      }

    } catch (JWNLException e) {
      throw new WordNetException("looking for all <" + pos + ">s failed. See nested exception", e);
    }

    return synsets;
  }
 /*
  * (non-Javadoc)
  * @see ac.biu.nlp.nlp.instruments.dictionary.wordnet.Dictionary#getSynsetsOf(java.lang.String, ac.biu.nlp.nlp.instruments.dictionary.wordnet.WordNetPartOfSpeech)
  */
 public Set<Synset> getSynsetsOf(String lemma, WordNetPartOfSpeech partOfSpeech)
     throws WordNetException {
   if (doNotProcessThisWord(lemma)) return new LinkedHashSet<Synset>();
   else {
     try {
       IndexWord indexWord =
           extJwnlRealDictionary.lookupIndexWord(
               ExtJwnlUtils.getJwnlPartOfSpeec(partOfSpeech), lemma);
       return indexWordToSet(indexWord);
     } catch (JWNLException e) {
       throw new WordNetException(
           "looking for word <"
               + lemma
               + "> with part of speech: "
               + partOfSpeech.toString()
               + " failed. See nested exception",
           e);
     }
   }
 }
 private static Set<Long> findAdjectiveAntonyms(Dictionary dic) throws SMatchException {
   log.info("Creating adjective antonyms array...");
   try {
     Set<Long> keys = new HashSet<>();
     int count = 0;
     Iterator<Synset> it = dic.getSynsetIterator(POS.ADJECTIVE);
     while (it.hasNext()) {
       count++;
       if (0 == count % 1000) {
         log.debug("adjective antonyms: " + count);
       }
       Synset current = it.next();
       traverseTree(keys, PointerUtils.getExtendedAntonyms(current), current.getOffset());
       traverseListSym(keys, PointerUtils.getAntonyms(current), current.getOffset());
     }
     log.info("Adjective antonyms: " + keys.size());
     return keys;
   } catch (JWNLException e) {
     throw new SMatchException(e.getClass().getSimpleName() + ": " + e.getMessage(), e);
   }
 }
 /*
  * (non-Javadoc)
  * @see ac.biu.nlp.nlp.instruments.dictionary.wordnet.Dictionary#getSortedSynsetOf(java.lang.String)
  */
 public Map<WordNetPartOfSpeech, List<Synset>> getSortedSynsetOf(String lemma)
     throws WordNetException {
   Map<WordNetPartOfSpeech, List<Synset>> ret =
       new LinkedHashMap<WordNetPartOfSpeech, List<Synset>>();
   if (doNotProcessThisWord(lemma)) ;
   else {
     try {
       IndexWordSet indexWordSet = extJwnlRealDictionary.lookupAllIndexWords(lemma);
       if (indexWordSet != null) {
         for (WordNetPartOfSpeech partOfSpeech : WordNetPartOfSpeech.values()) {
           POS pos = ExtJwnlUtils.getJwnlPartOfSpeec(partOfSpeech);
           if (indexWordSet.getIndexWord(pos) != null)
             ret.put(partOfSpeech, indexWordToList(indexWordSet.getIndexWord(pos)));
         }
       }
     } catch (JWNLException e) {
       throw new WordNetException(
           "looking for lemma <" + lemma + "> failed. See nested exception", e);
     }
   }
   return ret;
 }
示例#13
0
  /**
   * Provides a WordNET dictionary
   *
   * @return wordnet
   */
  public static Dictionary setupWordNet() {
    // set up properties file
    String propsFile = "file_properties.xml";
    FileInputStream properties = null;
    try {
      properties = new FileInputStream(propsFile);
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();
    }

    // create a dictionary and run the analytics
    try {

      // run
      if (dictionary == null) {
        // new style, instance dictionary
        dictionary = Dictionary.getInstance(properties);
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(-1);
    }
    return dictionary;
  }
  private static Set<Long> findNounAntonyms(Dictionary dic) throws SMatchException {
    log.info("Creating noun antonyms array...");
    try {
      Set<Long> keys = new HashSet<>();
      int count = 0;
      Iterator<Synset> it = dic.getSynsetIterator(POS.NOUN);
      while (it.hasNext()) {
        count++;
        if (0 == count % 10000) {
          log.debug("noun antonyms: " + count);
        }
        Synset source = it.next();

        cartPr(keys, source.getPointers(PointerType.PART_MERONYM));
        cartPr(keys, source.getPointers(PointerType.SUBSTANCE_MERONYM));
        cartPr(keys, source.getPointers(PointerType.MEMBER_MERONYM));
      }

      log.info("Noun antonyms: " + keys.size());
      return keys;
    } catch (JWNLException e) {
      throw new SMatchException(e.getClass().getSimpleName() + ": " + e.getMessage(), e);
    }
  }
示例#15
0
  public void run(String sentense, String word) throws JWNLException {
    WORD = dictionary.lookupIndexWord(POS.NOUN, word);

    leskImplementor.disambiguate(sentense, WORD);
  }
示例#16
0
 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
   in.defaultReadObject();
   dictionary = Dictionary.getRestoreDictionary();
 }