org.apache.lucene.analysis.synonym.SolrSynonymParser Java Examples

The following examples show how to use org.apache.lucene.analysis.synonym.SolrSynonymParser. 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: TestConditionalTokenFilter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testWrapGraphs() throws Exception {

    TokenStream stream = whitespaceMockTokenizer("a b c d e");

    SynonymMap sm;
    try (Analyzer analyzer = new MockAnalyzer(random())) {
      SolrSynonymParser parser = new SolrSynonymParser(true, true, analyzer);
      parser.parse(new StringReader("a b, f\nc d, g"));
      sm = parser.build();
    }

    TokenStream ts = new SkipMatchingFilter(stream, in -> new SynonymGraphFilter(in, sm, true), "c");

    assertTokenStreamContents(ts, new String[]{
        "f", "a", "b", "c", "d", "e"
        },
        null, null, null,
        new int[]{
        1, 0, 1, 1, 1, 1
        },
        new int[]{
        2, 1, 1, 1, 1, 1
        });

  }
 
Example #2
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void inform( SolrCore core ) {
  if (initParams != null) {
    SolrResourceLoader resourceLoader = core.getResourceLoader( );
      
    synonymsFile = (String)initParams.get( "synonyms" );
    if (synonymsFile != null) {
      Analyzer analyzer = new Analyzer() {
      @Override
        protected TokenStreamComponents createComponents(String fieldName) {
          Tokenizer tokenizer = new KeywordTokenizer();
          return new TokenStreamComponents(tokenizer, tokenizer );
        }
      };
              
      try {
        SolrSynonymParser parser = new SolrSynonymParser(true, true, analyzer);
        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                                                                    .onUnmappableCharacter(CodingErrorAction.REPORT);
                  
        parser.parse(new InputStreamReader( resourceLoader.openResource(synonymsFile), decoder));
        this.synonyms = parser.build( );
      }
      catch ( Exception e ) {
        // ???
        Log.warn( "Parsing Synonyms Got Exception " + e );
      }
    }
      
    String stopwordsFile = (String)initParams.get( "stopwords" );
    if (stopwordsFile != null) {
      this.stopwords = new HashSet<String>( );
      try {
        BufferedReader br = new BufferedReader( new InputStreamReader( resourceLoader.openResource( stopwordsFile )));
        String line = null;
        while ((line = br.readLine( )) != null) {
          stopwords.add( line.toLowerCase( ) );
        }
        br.close( );
      }
      catch ( IOException ioe ) {
        Log.warn( "Adding Stopwords Got Exception " + ioe );
      }
    }
  }
    
  core.registerFirstSearcherListener( this );
  core.registerNewSearcherListener( this );
}
 
Example #3
Source File: QueryAutoFilteringComponent.java    From query-autofiltering-component with Apache License 2.0 4 votes vote down vote up
@Override
public void inform( SolrCore core ) {
  if (initParams != null) {
    SolrResourceLoader resourceLoader = core.getResourceLoader( );
      
    synonymsFile = (String)initParams.get( "synonyms" );
    if (synonymsFile != null) {
      Analyzer analyzer = new Analyzer() {
      @Override
        protected TokenStreamComponents createComponents(String fieldName, Reader reader) {
          Tokenizer tokenizer = new KeywordTokenizer( reader );
          return new TokenStreamComponents(tokenizer, tokenizer );
        }
      };
              
      try {
        SolrSynonymParser parser = new SolrSynonymParser(true, true, analyzer);
        CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
                                                                    .onUnmappableCharacter(CodingErrorAction.REPORT);
                  
        parser.parse(new InputStreamReader( resourceLoader.openResource(synonymsFile), decoder));
        this.synonyms = parser.build( );
      }
      catch ( Exception e ) {
        // ???
        Log.warn( "Parsing Synonyms Got Exception " + e );
      }
    }
      
    String stopwordsFile = (String)initParams.get( "stopwords" );
    if (stopwordsFile != null) {
      this.stopwords = new HashSet<String>( );
      try {
        BufferedReader br = new BufferedReader( new InputStreamReader( resourceLoader.openResource( stopwordsFile )));
        String line = null;
        while ((line = br.readLine( )) != null) {
          stopwords.add( line.toLowerCase( ) );
        }
        br.close( );
      }
      catch ( IOException ioe ) {
        Log.warn( "Adding Stopwords Got Exception " + ioe );
      }
    }
  }
    
  core.registerFirstSearcherListener( this );
  core.registerNewSearcherListener( this );
}