opennlp.model.AbstractModelReader 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.model;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;
public abstract class AbstractModelReader {
/**
* The number of predicates contained in the model.
*/
protected int NUM_PREDS;
protected DataReader dataReader;
public AbstractModelReader(File f) throws IOException {
String filename = f.getName();
InputStream input;
// handle the zipped/not zipped distinction
if (filename.endsWith(".gz")) {
input = new GZIPInputStream(new FileInputStream(f));
filename = filename.substring(0,filename.length()-3);
}
else {
input = new FileInputStream(f);
}
// handle the different formats
if (filename.endsWith(".bin")) {
this.dataReader = new BinaryFileDataReader(input);
}
else { // filename ends with ".txt"
this.dataReader = new PlainTextFileDataReader(input);
}
}
public AbstractModelReader(DataReader dataReader) {
super();
this.dataReader = dataReader;
}
/**
* Implement as needed for the format the model is stored in.
*/
public int readInt() throws java.io.IOException {
return dataReader.readInt();
}
/**
* Implement as needed for the format the model is stored in.
*/
public double readDouble() throws java.io.IOException {
return dataReader.readDouble();
}
/**
* Implement as needed for the format the model is stored in.
*/
public String readUTF() throws java.io.IOException {
return dataReader.readUTF();
}
public AbstractModel getModel() throws IOException {
checkModelType();
return constructModel();
}
public abstract void checkModelType() throws java.io.IOException;
public abstract AbstractModel constructModel() throws java.io.IOException;
protected String[] getOutcomes() throws java.io.IOException {
int numOutcomes = readInt();
String[] outcomeLabels = new String[numOutcomes];
for (int i=0; i