uk.ac.susx.tag.method51.text.StopwordService.java Source code

Java tutorial

Introduction

Here is the source code for uk.ac.susx.tag.method51.text.StopwordService.java

Source

package uk.ac.susx.tag.method51.text;

/*
 * #%L
 * StopwordService.java - method51 - University of Sussex - 2,013
 * %%
 * Copyright (C) 2013 - 2014 University of Sussex
 * %%
 * 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.
 * #L%
 */

import com.google.common.io.Resources;
import uk.ac.susx.tag.method51.core.collections.ImmutableSet;

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * User: Simon Wibberley <sw206@sussex.ac.uk>
 * Date: 03/01/2014
 * Time: 17:21
 */
public class StopwordService {

    private static final Map<String, ImmutableSet<String>> stopwordLists = new HashMap<>();

    public static synchronized ImmutableSet<String> getStopwords(String key) {

        if (stopwordLists.containsKey(key)) {

            return stopwordLists.get(key);
        } else {

            ImmutableSet<String> stopwords = load(key);
            stopwordLists.put(key, stopwords);

            return stopwords;
        }
    }

    /**
     * load stop words from specified source
     *
     */
    private static ImmutableSet<String> load(String source) {
        Set<String> set = new HashSet<>();
        source = "stopwords/" + source;
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(Resources.getResource(StopwordService.class, source).openStream()));) {
            String line;
            while ((line = reader.readLine()) != null) {
                set.add(line.trim());
            }
            return new ImmutableSet<>(set);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}