smile.nlp.dictionary.SimpleDictionary Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2010 Haifeng Li
*
* 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 smile.nlp.dictionary;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Iterator;
/**
* A simple implementation of dictionary interface.
*
* @author Haifeng Li
*/
public class SimpleDictionary implements Dictionary {
/**
* A list of abbreviations.
*/
private HashSet dict;
/**
* Constructor.
* @param resource the file name of dictionary. The file should be in plain
* text, in which each line is a word.
*/
public SimpleDictionary(String resource) {
dict = new HashSet<>();
File file = new File(resource);
try (BufferedReader input = file.exists() ?
new BufferedReader(new FileReader(resource)) :
new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(resource)))) {
String line = null;
while ((line = input.readLine()) != null) {
line = line.trim();
// Remove blank line or single capital characters from dictionary.
if (!line.isEmpty() && !line.matches("^[A-Z]$")) {
dict.add(line);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public boolean contains(String word) {
return dict.contains(word);
}
@Override
public int size() {
return dict.size();
}
@Override
public Iterator iterator() {
return dict.iterator();
}
}