WordnetSynonymParser in Lucene

2019-05-20 13:10发布

问题:

I am new to Lucene and I'm trying to use WordnetSynonymParser to expand queries using the wordnet synonyms prolog. Here is what I have till now:

public class CustomAnalyzer extends Analyzer {

@Override
protected TokenStreamComponents createComponents(String fieldName, Reader reader){
    // TODO Auto-generated method stub
    Tokenizer source = new ClassicTokenizer(Version.LUCENE_47, reader);
    TokenStream filter = new StandardFilter(Version.LUCENE_47, source);
    filter = new LowerCaseFilter(Version.LUCENE_47,filter);
    SynonymMap mySynonymMap = null;
    try {
        mySynonymMap = buildSynonym();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    filter = new SynonymFilter(filter, mySynonymMap, false);        
    return new TokenStreamComponents(source, filter);
}

private SynonymMap buildSynonym() throws IOException
{
    File file = new File("wn/wn_s.pl");
    InputStream stream = new FileInputStream(file);
    Reader rulesReader = new InputStreamReader(stream); 
    SynonymMap.Builder parser = null;
    parser = new WordnetSynonymParser(true, true, new StandardAnalyzer(Version.LUCENE_47));
    ((WordnetSynonymParser) parser).add(rulesReader);         
    SynonymMap synonymMap = parser.build();
    return synonymMap;
}
}

I get the error "The method add(CharsRef, CharsRef, boolean) in the type SynonymMap.Builder is not applicable for the arguments (Reader)"

However, the documentation of WordnetSynonymParser expects a Reader argument for the add function.

What am I doing wrong here? Any help is appreciated.

回答1:

If you are seeing documentation stating that WordNetSynonymParser has a method add(Reader), you are probably looking at documentation for an older version. The method certainly isn't there in the source code for 4.7. As of version 4.6.0, the method you are looking for is WordnetSynonymParser.parse(Reader).