com.flaptor.indextank.query.analyzers.StopAnalyzer.java Source code

Java tutorial

Introduction

Here is the source code for com.flaptor.indextank.query.analyzers.StopAnalyzer.java

Source

/*
 * Copyright (c) 2011 LinkedIn, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.flaptor.indextank.query.analyzers;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
import org.json.simple.JSONArray;

import com.google.common.collect.ImmutableSet;

public abstract class StopAnalyzer extends StandardAnalyzer {

    private static final String STOPWORDS = "stopwords";

    public StopAnalyzer(Map<Object, Object> analyzerConfiguration) {
        super(Version.LUCENE_CURRENT, getStopWords(analyzerConfiguration));
    }

    private static Set<String> getStopWords(Map<Object, Object> analyzerConfiguration) {
        if (analyzerConfiguration.containsKey(STOPWORDS)) {
            JSONArray stopwordList = (JSONArray) analyzerConfiguration.get(STOPWORDS);
            Set<String> stopwords = new HashSet<String>(stopwordList.size());
            for (Object stopword : stopwordList) {
                if (!(stopword instanceof String)) {
                    throw new IllegalArgumentException("Stopwords aren't Strings");
                }
                stopwords.add((String) stopword);
            }
            return stopwords;
        } else {
            return ImmutableSet.of();
        }
    }
}