opennlp.tools.sentdetect.SentenceModel Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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 opennlp.tools.sentdetect;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
import opennlp.tools.dictionary.Dictionary;
import opennlp.tools.ml.model.MaxentModel;
import opennlp.tools.util.BaseToolFactory;
import opennlp.tools.util.InvalidFormatException;
import opennlp.tools.util.model.BaseModel;
import opennlp.tools.util.model.ModelUtil;
/**
* The {@link SentenceModel} is the model used by a learnable
* {@link SentenceDetector}.
*
* @see SentenceDetectorME
*/
public class SentenceModel extends BaseModel {
private static final long serialVersionUID = -8374532795287293730L;
private static final String COMPONENT_NAME = "SentenceDetectorME";
private static final String MAXENT_MODEL_ENTRY_NAME = "sent.model";
/**
* Initializes a {@link SentenceModel} instance via given parameters.
*
* @param languageCode The ISO language code for this model. Must not be {@code null}.
* @param sentModel A valid {@link MaxentModel}.
* @param manifestInfoEntries Additional information kept in the manifest.
* @param sdFactory The {@link SentenceDetectorFactory} for creating related objects.
*/
public SentenceModel(String languageCode, MaxentModel sentModel,
Map manifestInfoEntries, SentenceDetectorFactory sdFactory) {
super(COMPONENT_NAME, languageCode, manifestInfoEntries, sdFactory);
artifactMap.put(MAXENT_MODEL_ENTRY_NAME, sentModel);
checkArtifactMap();
}
/**
* Initializes a {@link SentenceModel} instance via a valid {@link InputStream}.
*
* @param in The {@link InputStream} used for loading the model.
*
* @throws IOException Thrown if IO errors occurred during initialization.
*/
public SentenceModel(InputStream in) throws IOException {
super(COMPONENT_NAME, in);
}
/**
* Initializes a {@link SentenceModel} instance via a valid {@link File}.
*
* @param modelFile The {@link File} used for loading the model.
*
* @throws IOException Thrown if IO errors occurred during initialization.
*/
public SentenceModel(File modelFile) throws IOException {
super(COMPONENT_NAME, modelFile);
}
/**
* Initializes a {@link SentenceModel} instance via a valid {@link Path}.
*
* @param modelPath The {@link Path} used for loading the model.
*
* @throws IOException Thrown if IO errors occurred during initialization.
*/
public SentenceModel(Path modelPath) throws IOException {
this(modelPath.toFile());
}
/**
* Initializes a {@link SentenceModel} instance via a valid {@link URL}.
*
* @param modelURL The {@link URL} used for loading the model.
*
* @throws IOException Thrown if IO errors occurred during initialization.
*/
public SentenceModel(URL modelURL) throws IOException {
super(COMPONENT_NAME, modelURL);
}
@Override
protected void validateArtifactMap() throws InvalidFormatException {
super.validateArtifactMap();
if (!(artifactMap.get(MAXENT_MODEL_ENTRY_NAME) instanceof MaxentModel)) {
throw new InvalidFormatException("Unable to find " + MAXENT_MODEL_ENTRY_NAME +
" maxent model!");
}
if (!ModelUtil.validateOutcomes(getMaxentModel(), SentenceDetectorME.SPLIT,
SentenceDetectorME.NO_SPLIT)) {
throw new InvalidFormatException("The maxent model is not compatible " +
"with the sentence detector!");
}
}
public SentenceDetectorFactory getFactory() {
return (SentenceDetectorFactory) this.toolFactory;
}
@Override
protected Class extends BaseToolFactory> getDefaultFactory() {
return SentenceDetectorFactory.class;
}
public MaxentModel getMaxentModel() {
return (MaxentModel) artifactMap.get(MAXENT_MODEL_ENTRY_NAME);
}
public Dictionary getAbbreviations() {
if (getFactory() != null) {
return getFactory().getAbbreviationDictionary();
}
return null;
}
public boolean useTokenEnd() {
return getFactory() == null || getFactory().isUseTokenEnd();
}
public char[] getEosCharacters() {
if (getFactory() != null) {
return getFactory().getEOSCharacters();
}
return null;
}
}