Search in sources :

Example 1 with PerceptronClassificationModel

use of com.joliciel.talismane.machineLearning.perceptron.PerceptronClassificationModel in project talismane by joliciel-informatique.

the class MachineLearningModelFactory method getMachineLearningModel.

public MachineLearningModel getMachineLearningModel(ZipInputStream zis) throws ClassNotFoundException {
    try {
        MachineLearningModel machineLearningModel = null;
        ZipEntry ze = zis.getNextEntry();
        if (!ze.getName().equals("algorithm.txt")) {
            throw new JolicielException("Expected algorithm.txt as first entry in zip. Was: " + ze.getName());
        }
        // note: assuming the model type will always be the first entry
        @SuppressWarnings("resource") Scanner typeScanner = new Scanner(zis, "UTF-8");
        MachineLearningAlgorithm algorithm = MachineLearningAlgorithm.MaxEnt;
        if (typeScanner.hasNextLine()) {
            String algorithmString = typeScanner.nextLine();
            try {
                algorithm = MachineLearningAlgorithm.valueOf(algorithmString);
            } catch (IllegalArgumentException iae) {
                LogUtils.logError(LOG, iae);
                throw new JolicielException("Unknown algorithm: " + algorithmString);
            }
        } else {
            throw new JolicielException("Cannot find algorithm in zip file");
        }
        switch(algorithm) {
            case MaxEnt:
                machineLearningModel = new MaximumEntropyModel();
                break;
            case LinearSVM:
                machineLearningModel = new LinearSVMModel();
                break;
            case LinearSVMOneVsRest:
                machineLearningModel = new LinearSVMOneVsRestModel();
                break;
            case Perceptron:
                machineLearningModel = new PerceptronClassificationModel();
                break;
            default:
                throw new JolicielException("Machine learning algorithm not yet supported: " + algorithm);
        }
        while ((ze = zis.getNextEntry()) != null) {
            LOG.debug(ze.getName());
            machineLearningModel.loadZipEntry(zis, ze);
        }
        // next zip entry
        machineLearningModel.onLoadComplete();
        return machineLearningModel;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    } finally {
        try {
            zis.close();
        } catch (IOException ioe) {
            LogUtils.logError(LOG, ioe);
        }
    }
}
Also used : Scanner(java.util.Scanner) LinearSVMOneVsRestModel(com.joliciel.talismane.machineLearning.linearsvm.LinearSVMOneVsRestModel) JolicielException(com.joliciel.talismane.utils.JolicielException) MaximumEntropyModel(com.joliciel.talismane.machineLearning.maxent.MaximumEntropyModel) PerceptronClassificationModel(com.joliciel.talismane.machineLearning.perceptron.PerceptronClassificationModel) LinearSVMModel(com.joliciel.talismane.machineLearning.linearsvm.LinearSVMModel) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Aggregations

LinearSVMModel (com.joliciel.talismane.machineLearning.linearsvm.LinearSVMModel)1 LinearSVMOneVsRestModel (com.joliciel.talismane.machineLearning.linearsvm.LinearSVMOneVsRestModel)1 MaximumEntropyModel (com.joliciel.talismane.machineLearning.maxent.MaximumEntropyModel)1 PerceptronClassificationModel (com.joliciel.talismane.machineLearning.perceptron.PerceptronClassificationModel)1 JolicielException (com.joliciel.talismane.utils.JolicielException)1 IOException (java.io.IOException)1 Scanner (java.util.Scanner)1 ZipEntry (java.util.zip.ZipEntry)1