Lucene 8.5中的自定义分析器

问题描述 投票:0回答:1

我正在尝试使用Lucene 8.5实现CustomAnalyzer。目的是扩展来自wordnet同义词的查询。我的代码如下:

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();
}

但是,当我运行它时,出现此异常

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)

该文件存在,它的权限是正确的,并且即使我使用绝对路径,也会发生指定的异常。

任何帮助将不胜感激。

java lucene wordnet keyword-expansion
1个回答
0
投票

问题是wn_s.pl不在ClassPath中。我将文件移到src/main/resources目录,此问题已解决。

感谢@Thomas的建议。

© www.soinside.com 2019 - 2024. All rights reserved.