All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.joliciel.talismane.machineLearning.perceptron.PerceptronClassificationModel Maven / Gradle / Ivy

There is a newer version: 6.1.8
Show newest version
///////////////////////////////////////////////////////////////////////////////
//Copyright (C) 2014 Joliciel Informatique
//
//This file is part of Talismane.
//
//Talismane is free software: you can redistribute it and/or modify
//it under the terms of the GNU Affero General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Talismane is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Affero General Public License for more details.
//
//You should have received a copy of the GNU Affero General Public License
//along with Talismane.  If not, see .
//////////////////////////////////////////////////////////////////////////////
package com.joliciel.talismane.machineLearning.perceptron;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joliciel.talismane.machineLearning.AbstractMachineLearningModel;
import com.joliciel.talismane.machineLearning.ClassificationModel;
import com.joliciel.talismane.machineLearning.ClassificationObserver;
import com.joliciel.talismane.machineLearning.DecisionMaker;
import com.joliciel.talismane.machineLearning.MachineLearningAlgorithm;
import com.typesafe.config.Config;

public class PerceptronClassificationModel extends AbstractMachineLearningModel implements ClassificationModel {
  @SuppressWarnings("unused")
  private static final Logger LOG = LoggerFactory.getLogger(PerceptronClassificationModel.class);
  PerceptronModelParameters params = null;
  PerceptronDecisionMaker decisionMaker;
  private transient Set outcomeNames = null;

  public PerceptronClassificationModel() {
  }

  public PerceptronClassificationModel(PerceptronModelParameters params, Config config, Map> descriptors) {
    super(config, descriptors);
    this.params = params;
  }

  @Override
  public DecisionMaker getDecisionMaker() {
    if (decisionMaker == null) {
      decisionMaker = new PerceptronDecisionMaker(params, this.getPerceptronScoring());
    }
    return decisionMaker;
  }

  @Override
  public ClassificationObserver getDetailedAnalysisObserver(File file) throws IOException {
    return new PerceptronDetailedAnalysisWriter(decisionMaker, file);
  }

  @Override
  public MachineLearningAlgorithm getAlgorithm() {
    return MachineLearningAlgorithm.Perceptron;
  }

  @Override
  public void loadModelFromStream(InputStream inputStream) throws ClassNotFoundException, IOException {
    ObjectInputStream in = new ObjectInputStream(inputStream);
    params = (PerceptronModelParameters) in.readObject();
  }

  @Override
  public void writeModelToStream(OutputStream outputStream) throws IOException {
    ObjectOutputStream out = new ObjectOutputStream(outputStream);

    out.writeObject(params);
  }

  @Override
  public boolean loadDataFromStream(InputStream inputStream, ZipEntry zipEntry) {
    return false;
  }

  @Override
  public void writeDataToStream(ZipOutputStream zos) {
    // nothing to do
  }

  @Override
  public Set getOutcomeNames() {
    if (this.outcomeNames == null) {
      this.outcomeNames = new TreeSet(this.params.getOutcomes());
    }
    return this.outcomeNames;
  }

  @Override
  protected void persistOtherEntries(ZipOutputStream zos) throws IOException {
  }

  public PerceptronScoring getPerceptronScoring() {
    return (PerceptronScoring) this.getModelAttributes().get("scoring");
  }

  @Override
  public void onLoadComplete() {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy