net.sf.extjwnl.JWNLException Java Examples

The following examples show how to use net.sf.extjwnl.JWNLException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SynonymProvider.java    From lucene4ir with Apache License 2.0 5 votes vote down vote up
public static Set<String> getSynonyms(String word) throws JWNLException {
    IndexWord[] allWords = getWordArray(word);
    if (allWords.length == 1) {
        Set<String> retData = new HashSet<>();
        for (Synset synset : allWords[0].getSenses()) {
            for (Word wordObj : synset.getWords()) {
                retData.add(wordObj.getLemma());
            }
        }
        return retData;
    }
    return null;
}
 
Example #2
Source File: SynonymProvider.java    From lucene4ir with Apache License 2.0 5 votes vote down vote up
public static Set<String> getHypernym(String word) throws JWNLException {
    IndexWord[] allWords = getWordArray(word);
    if (allWords.length == 1) {
        Set<String> retData = new HashSet<>();
    }
    return null;
}
 
Example #3
Source File: SharedWordNetResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean doInitialize(
    final ResourceSpecifier specifier, final Map<String, Object> additionalParams)
    throws ResourceInitializationException {

  try {
    dictionary = Dictionary.getDefaultResourceInstance();
  } catch (final JWNLException e) {
    throw new ResourceInitializationException(e);
  }

  return super.doInitialize(specifier, additionalParams);
}
 
Example #4
Source File: SharedWordNetResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected void doDestroy() {
  super.doDestroy();

  try {
    dictionary.close();
  } catch (final JWNLException e) {
    getLogger().warn("WordNet dictionary did not close cleanly", e);
  } finally {
    dictionary = null;
  }
}
 
Example #5
Source File: SharedWordNetResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Lookup the word from the dictionary, performing lemmisation if required.
 *
 * @param pos the pos
 * @param word the word
 * @return the WordNet word, (as an optional)
 */
public Optional<IndexWord> lookupWord(final POS pos, final String word) {
  try {
    return Optional.ofNullable(dictionary.lookupIndexWord(pos, word));
  } catch (final JWNLException e) {
    getMonitor().warn("Lookup word failed", e);
    return Optional.empty();
  }
}
 
Example #6
Source File: SharedWordNetResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Get an exact lemma from the dictionary, .
 *
 * @param pos the pos
 * @param lemma the lemma
 * @return the WordNet word (as an optional)
 */
public Optional<IndexWord> getWord(final POS pos, final String lemma) {
  try {
    return Optional.ofNullable(dictionary.getIndexWord(pos, lemma));
  } catch (final JWNLException e) {
    getMonitor().warn("Get word failed", e);
    return Optional.empty();
  }
}
 
Example #7
Source File: SharedWordNetResourceTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetWord() throws JWNLException {
  final Optional<IndexWord> missing = wnr.getWord(POS.VERB, "employs");
  Assert.assertFalse(missing.isPresent());

  final IndexWord employ = wnr.getWord(POS.VERB, "employ").get();
  Assert.assertNotNull(employ);
  Assert.assertEquals("employ", employ.getLemma());
}
 
Example #8
Source File: SharedWordNetResourceTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testSuperSense() throws JWNLException {
  final List<String> word = wnr.getSuperSenses(POS.VERB, "employs").collect(Collectors.toList());

  Assert.assertTrue("consumption".equals(word.get(0)) || "consumption".equals(word.get(1)));
  Assert.assertTrue("social".equals(word.get(0)) || "social".equals(word.get(1)));
}
 
Example #9
Source File: SharedWordNetResourceTest.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingLookupWord() throws JWNLException {
  final Optional<IndexWord> word1 = wnr.lookupWord(POS.VERB, "ascasdcscz");
  Assert.assertFalse(word1.isPresent());

  final Optional<IndexWord> word2 = wnr.getWord(POS.VERB, "ascasdcscz");
  Assert.assertFalse(word2.isPresent());

  final long count = wnr.getSuperSenses(POS.VERB, "ascasdcscz").count();
  Assert.assertEquals(0, count);

  final Optional<String> word = wnr.getBestSuperSense(POS.VERB, "ascasdcscz");
  Assert.assertFalse(word.isPresent());
}
 
Example #10
Source File: RetrievalAppQueryExpansion.java    From lucene4ir with Apache License 2.0 4 votes vote down vote up
public ScoreDoc[] runQuery(String qno, String queryTerms){
    ScoreDoc[] hits = null;
    System.out.println("Query No.: " + qno + " " + queryTerms);

    try {

        //Multi-field query
        String[] fields = new String[]{"title", "content"};

        // A query builder for constructing a complex query
        BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();

        // Boost the original query by a factor of 5
        // Query terms are important but the original terms are importantER
        BoostQuery termQuery = new BoostQuery(parser.parse(queryTerms), 5.0f);

        // Add it to the query builder
        queryBuilder.add(termQuery, BooleanClause.Occur.MUST);

        // Print out what Lucene generated
        // System.out.println(query.toString());

        // Find the synonyms of each term in the original query
        StringBuilder sb = new StringBuilder();
        for (String queryTerm : queryTerms.trim().split(" ")) {
            try {
                Set<String> synonyms = SynonymProvider.getSynonyms(queryTerm);

                if (synonyms == null) {
                    continue;
                }

                Iterator<String> it = synonyms.iterator();

                while (it.hasNext()) {
                    sb.append(it.next());
                    sb.append(" ");
                }

            } catch (JWNLException e) {
                e.printStackTrace();
            }

        }
        String querySynonymized = sb.toString();

        // If we found some synonyms, construct a query and add it to the query builder
        if (querySynonymized.length() > 0) {
            Query queryExpanded = parser.parse(querySynonymized);
            queryBuilder.add(queryExpanded, BooleanClause.Occur.SHOULD);
        }

        // Construct the final query and run it
        Query finalQuery = queryBuilder.build();

        try {
            TopDocs results = searcher.search(finalQuery, 1000);
            hits = results.scoreDocs;
        }
        catch (IOException ioe){
            System.out.println(" caught a " + ioe.getClass() + "\n with message: " + ioe.getMessage());
        }


    } catch (ParseException pe){
        System.out.println("Cant parse query");
    }
    return hits;
}
 
Example #11
Source File: SynonymProvider.java    From lucene4ir with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws JWNLException {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
        System.out.println(getSynonyms(scanner.next()));
    }
}
 
Example #12
Source File: SynonymProvider.java    From lucene4ir with Apache License 2.0 4 votes vote down vote up
private static IndexWord[] getWordArray(String word) throws JWNLException {
    if (dictionary == null) {
        throw exc;
    }
    return dictionary.lookupAllIndexWords(word).getIndexWordArray();
}
 
Example #13
Source File: SharedWordNetResourceTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testLookupWord() throws JWNLException {
  final IndexWord word = wnr.lookupWord(POS.VERB, "employing").get();
  Assert.assertEquals("employ", word.getLemma());
}
 
Example #14
Source File: SharedWordNetResourceTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testBestSuperSense() throws JWNLException {
  final Optional<String> word = wnr.getBestSuperSense(POS.VERB, "know");

  Assert.assertEquals("cognition", word.get());
}
 
Example #15
Source File: InteractionIdentifierTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Before
public void before() throws JWNLException {
  identifier = new InteractionIdentifier(monitor, 1, 2, 0.2);
}