Custom Analyzer in Lucene 8.5

642 Views Asked by At

I am trying to implement a CustomAnalyzer using Lucene 8.5. It's purpose it to expand queries with synonyms from wordnet. My code is the following:

private static CustomAnalyzer customAnalyzer() throws IOException {
        Map<String, String> sargs = new HashMap<>();
        sargs.put("synonyms", "wn_s.pl");
        sargs.put("format", "wordnet");

        CustomAnalyzer.Builder builder = CustomAnalyzer.builder()
                .withTokenizer(StandardTokenizerFactory.class)
                .addTokenFilter(EnglishPossessiveFilterFactory.class)
                .addTokenFilter(LowerCaseFilterFactory.class)
                .addTokenFilter(StopFilterFactory.class)
                .addTokenFilter(PorterStemFilterFactory.class)
                .addTokenFilter(SynonymGraphFilterFactory.class, sargs);
        return builder.build();
}

However, when I run it I get this exception

java.io.IOException: Resource not found: wn_s.pl
    at org.apache.lucene.analysis.util.ClasspathResourceLoader.openResource(ClasspathResourceLoader.java:77)
    at org.apache.lucene.analysis.synonym.SynonymGraphFilterFactory.loadSynonyms(SynonymGraphFilterFactory.java:179)
    at org.apache.lucene.analysis.synonym.SynonymGraphFilterFactory.inform(SynonymGraphFilterFactory.java:154)
    at org.apache.lucene.analysis.custom.CustomAnalyzer$Builder.applyResourceLoader(CustomAnalyzer.java:544)
    at org.apache.lucene.analysis.custom.CustomAnalyzer$Builder.addTokenFilter(CustomAnalyzer.java:321)
    at Main.customAnalyzer(Main.java:78)
    at Main.main(Main.java:34)

The file exists, it's permissions are correct, and the specified exception occurs even if I use an absolute path.

Any help will be appreciated.

1

There are 1 best solutions below

0
On

The problem was that the wn_s.pl was not in the ClassPath. I moved the file to the src/main/resources directory and the issue was fixed.

Thanks @Thomas for the suggestion.