/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you 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 org.elasticsearch.index.analysis;
import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.ja.JapaneseTokenizer;
import org.apache.lucene.analysis.ja.JapaneseTokenizer.Mode;
import org.apache.lucene.analysis.ja.dict.UserDictionary;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettings;
import java.io.IOException;
import java.io.Reader;
/**
*/
public class KuromojiTokenizerFactory extends AbstractTokenizerFactory {
private static final String USER_DICT_OPTION = "user_dictionary";
private final UserDictionary userDictionary;
private final Mode mode;
private boolean discartPunctuation;
@Inject
public KuromojiTokenizerFactory(Index index, @IndexSettings Settings indexSettings, Environment env, @Assisted String name, @Assisted Settings settings) {
super(index, indexSettings, name, settings);
mode = getMode(settings);
userDictionary = getUserDictionary(env, settings);
discartPunctuation = settings.getAsBoolean("discard_punctuation", true);
}
public static UserDictionary getUserDictionary(Environment env, Settings settings) {
try {
final Reader reader = Analysis.getReaderFromFile(env, settings, USER_DICT_OPTION);
if (reader == null) {
return null;
} else {
try {
return new UserDictionary(reader);
} finally {
reader.close();
}
}
} catch (IOException e) {
throw new ElasticSearchException("failed to load kuromoji user dictionary", e);
}
}
public static JapaneseTokenizer.Mode getMode(Settings settings) {
JapaneseTokenizer.Mode mode = JapaneseTokenizer.DEFAULT_MODE;
String modeSetting = settings.get("mode", null);
if (modeSetting != null) {
if ("search".equalsIgnoreCase(modeSetting)) {
mode = JapaneseTokenizer.Mode.SEARCH;
} else if ("normal".equalsIgnoreCase(modeSetting)) {
mode = JapaneseTokenizer.Mode.NORMAL;
} else if ("extended".equalsIgnoreCase(modeSetting)) {
mode = JapaneseTokenizer.Mode.EXTENDED;
}
}
return mode;
}
@Override
public Tokenizer create(Reader reader) {
return new JapaneseTokenizer(reader, userDictionary,
discartPunctuation, mode);
}
}