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

opennlp.tools.ml.TrainerFactory 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.ml;

import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import opennlp.tools.ml.maxent.GIS;
import opennlp.tools.ml.maxent.quasinewton.QNTrainer;
import opennlp.tools.ml.perceptron.PerceptronTrainer;
import opennlp.tools.ml.perceptron.SimplePerceptronSequenceTrainer;
import opennlp.tools.util.ext.ExtensionLoader;
import opennlp.tools.util.ext.ExtensionNotLoadedException;

public class TrainerFactory {

  public enum TrainerType {
    EVENT_MODEL_TRAINER,
    EVENT_MODEL_SEQUENCE_TRAINER,
    SEQUENCE_TRAINER
  }

  // built-in trainers
  private static final Map BUILTIN_TRAINERS;

  static {
    Map _trainers = new HashMap();
    _trainers.put(GIS.MAXENT_VALUE, GIS.class);
    _trainers.put(QNTrainer.MAXENT_QN_VALUE, QNTrainer.class);
    _trainers.put(PerceptronTrainer.PERCEPTRON_VALUE, PerceptronTrainer.class);
    _trainers.put(SimplePerceptronSequenceTrainer.PERCEPTRON_SEQUENCE_VALUE,
        SimplePerceptronSequenceTrainer.class);

    BUILTIN_TRAINERS = Collections.unmodifiableMap(_trainers);
  }

  @Deprecated
  private static String getPluggableTrainerType(String className) {
    try {
      Class trainerClass = Class.forName(className);
      if(trainerClass != null) {

        if (EventTrainer.class.isAssignableFrom(trainerClass)) {
          return EventTrainer.EVENT_VALUE;
        }
        else if (EventModelSequenceTrainer.class.isAssignableFrom(trainerClass)) {
          return EventModelSequenceTrainer.SEQUENCE_VALUE;
        }
        else if (SequenceTrainer.class.isAssignableFrom(trainerClass)) {
          return SequenceTrainer.SEQUENCE_VALUE;
        }
      }
    } catch (ClassNotFoundException e) {
    }

    return null;
  }

  /**
   * Determines the trainer type based on the ALGORITHM_PARAM value.
   *
   * @param trainParams
   * @return the trainer type or null if type couldn't be determined.
   */
  public static TrainerType getTrainerType(Map trainParams){

    String alogrithmValue = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);

    // Check if it is defaulting to the MAXENT trainer
    if (alogrithmValue == null) {
      return TrainerType.EVENT_MODEL_TRAINER;
    }

    Class trainerClass = BUILTIN_TRAINERS.get(alogrithmValue);

    if(trainerClass != null) {

      if (EventTrainer.class.isAssignableFrom(trainerClass)) {
        return TrainerType.EVENT_MODEL_TRAINER;
      }
      else if (EventModelSequenceTrainer.class.isAssignableFrom(trainerClass)) {
        return TrainerType.EVENT_MODEL_SEQUENCE_TRAINER;
      }
      else if (SequenceTrainer.class.isAssignableFrom(trainerClass)) {
        return TrainerType.SEQUENCE_TRAINER;
      }
    }

    // Try to load the different trainers, and return the type on success

    try {
      ExtensionLoader.instantiateExtension(EventTrainer.class, alogrithmValue);
      return TrainerType.EVENT_MODEL_TRAINER;
    }
    catch (ExtensionNotLoadedException e) {
    }

    try {
      ExtensionLoader.instantiateExtension(EventModelSequenceTrainer.class, alogrithmValue);
      return TrainerType.EVENT_MODEL_SEQUENCE_TRAINER;
    }
    catch (ExtensionNotLoadedException e) {
    }

    try {
      ExtensionLoader.instantiateExtension(SequenceTrainer.class, alogrithmValue);
      return TrainerType.SEQUENCE_TRAINER;
    }
    catch (ExtensionNotLoadedException e) {
    }

    return null;
  }

  /**
   * @deprecated use getTrainerType instead!
   */
  @Deprecated
  public static boolean isSupportEvent(Map trainParams) {

    String trainerType = trainParams.get(AbstractTrainer.TRAINER_TYPE_PARAM);

    if (trainerType == null) {
      String alogrithmValue = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);
      if (alogrithmValue != null) {
        trainerType = getPluggableTrainerType(trainParams.get(AbstractTrainer.ALGORITHM_PARAM));
      }
    }

    if (trainerType != null) {
      return EventTrainer.EVENT_VALUE.equals(trainerType);
    }

    return true;
  }

  /**
   * @deprecated use getTrainerType instead!
   */
  @Deprecated
  public static boolean isSupportSequence(Map trainParams) {
    return isSupportEventModelSequenceTraining(trainParams);
  }

  /**
   * @deprecated use getTrainerType instead!
   */
  @Deprecated
  public static boolean isSupportEventModelSequenceTraining(Map trainParams) {

    String trainerType = trainParams.get(AbstractTrainer.TRAINER_TYPE_PARAM);

    if (trainerType == null) {
      String alogrithmValue = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);
      if (alogrithmValue != null) {
        trainerType = getPluggableTrainerType(trainParams.get(AbstractTrainer.ALGORITHM_PARAM));
      }
    }

    return EventModelSequenceTrainer.SEQUENCE_VALUE.equals(trainerType);
  }

  /**
   * @deprecated use getTrainerType instead!
   */
  @Deprecated
  public static boolean isSupportSequenceTraining(Map trainParams) {
    String trainerType = trainParams.get(AbstractTrainer.TRAINER_TYPE_PARAM);

    if (trainerType == null) {
      String alogrithmValue = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);
      if (alogrithmValue != null) {
        trainerType = getPluggableTrainerType(trainParams.get(AbstractTrainer.ALGORITHM_PARAM));
      }
    }

    return SequenceTrainer.SEQUENCE_VALUE.equals(trainerType);
  }

  // TODO: How to do the testing ?!
  // is support event sequence ?
  // is support sequence ?

  /**
   * @deprecated use getTrainerType instead!
   */
  @Deprecated
  public static boolean isSequenceTraining(Map trainParams) {
    return SimplePerceptronSequenceTrainer.PERCEPTRON_SEQUENCE_VALUE
        .equals(trainParams.get(AbstractTrainer.ALGORITHM_PARAM));
  }

  public static SequenceTrainer getSequenceModelTrainer(Map trainParams,
      Map reportMap) {
    String trainerType = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);

    if (trainerType != null) {
      if (BUILTIN_TRAINERS.containsKey(trainerType)) {
        SequenceTrainer trainer =  TrainerFactory. createBuiltinTrainer(
            BUILTIN_TRAINERS.get(trainerType));
        trainer.init(trainParams, reportMap);
        return trainer;
      } else {
        SequenceTrainer trainer = ExtensionLoader.instantiateExtension(SequenceTrainer.class, trainerType);
        trainer.init(trainParams, reportMap);
        return trainer;
      }
    }
    else {
      throw new IllegalArgumentException("Trainer type couldn't be determined!");
    }
  }

  public static EventModelSequenceTrainer getEventModelSequenceTrainer(Map trainParams,
      Map reportMap) {
    String trainerType = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);
    if (trainerType != null) {
      if (BUILTIN_TRAINERS.containsKey(trainerType)) {
        EventModelSequenceTrainer trainer = TrainerFactory. createBuiltinTrainer(
            BUILTIN_TRAINERS.get(trainerType));
        trainer.init(trainParams, reportMap);
        return trainer;
      } else {
        EventModelSequenceTrainer trainer =
            ExtensionLoader.instantiateExtension(EventModelSequenceTrainer.class, trainerType);
        trainer.init(trainParams, reportMap);
        return trainer;
      }
    }
    else {
      throw new IllegalArgumentException("Trainer type couldn't be determined!");
    }
  }

  @Deprecated
  public static EventModelSequenceTrainer getSequenceTrainer(
      Map trainParams, Map reportMap) {
    return getEventModelSequenceTrainer(trainParams, reportMap);
  }

  public static EventTrainer getEventTrainer(Map trainParams,
      Map reportMap) {
    String trainerType = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);
    if (trainerType == null) {
      // default to MAXENT
      AbstractEventTrainer trainer = new GIS();
      trainer.init(trainParams, reportMap);
      return trainer;
    }
    else {
      if (BUILTIN_TRAINERS.containsKey(trainerType)) {
        EventTrainer trainer = TrainerFactory. createBuiltinTrainer(
            BUILTIN_TRAINERS.get(trainerType));
        trainer.init(trainParams, reportMap);
        return trainer;
      } else {
        EventTrainer trainer = ExtensionLoader.instantiateExtension(EventTrainer.class, trainerType);
        trainer.init(trainParams, reportMap);
        return trainer;
      }
    }
  }

  public static boolean isValid(Map trainParams) {

    // TODO: Need to validate all parameters correctly ... error prone?!

    String algorithmName = trainParams.get(AbstractTrainer.ALGORITHM_PARAM);

    // If a trainer type can be determined, then the trainer is valid!
    if (algorithmName != null &&
        !(BUILTIN_TRAINERS.containsKey(algorithmName) || getTrainerType(trainParams) != null)) {
      return false;
    }

    try {
      String cutoffString = trainParams.get(AbstractTrainer.CUTOFF_PARAM);
      if (cutoffString != null) Integer.parseInt(cutoffString);

      String iterationsString = trainParams.get(AbstractTrainer.ITERATIONS_PARAM);
      if (iterationsString != null) Integer.parseInt(iterationsString);
    }
    catch (NumberFormatException e) {
      return false;
    }

    String dataIndexer = trainParams.get(AbstractEventTrainer.DATA_INDEXER_PARAM);

    if (dataIndexer != null) {
      if (!(AbstractEventTrainer.DATA_INDEXER_ONE_PASS_VALUE.equals(dataIndexer)
          || AbstractEventTrainer.DATA_INDEXER_TWO_PASS_VALUE.equals(dataIndexer))) {
        return false;
      }
    }

    // TODO: Check data indexing ...

    return true;
  }

  private static  T createBuiltinTrainer(Class trainerClass) {
    T theTrainer = null;
    if (trainerClass != null) {
      try {
        Constructor contructor = trainerClass.getConstructor();
        theTrainer = contructor.newInstance();
      } catch (Exception e) {
        String msg = "Could not instantiate the "
            + trainerClass.getCanonicalName()
            + ". The initialization throw an exception.";
        System.err.println(msg);
        e.printStackTrace();
        throw new IllegalArgumentException(msg, e);
      }
    }

    return theTrainer;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy