| package org.maltparser.parser.guide.instance;
|
|
|
| import java.lang.reflect.Constructor;
|
| import java.lang.reflect.InvocationTargetException;
|
| import java.util.ArrayList;
|
| import java.util.Formatter;
|
|
|
| import org.maltparser.core.exception.MaltChainedException;
|
| import org.maltparser.core.feature.FeatureModel;
|
| import org.maltparser.core.feature.FeatureVector;
|
| import org.maltparser.core.feature.function.FeatureFunction;
|
| import org.maltparser.core.feature.function.Modifiable;
|
| import org.maltparser.core.syntaxgraph.DependencyStructure;
|
| import org.maltparser.ml.LearningMethod;
|
| import org.maltparser.ml.lib.LibLinear;
|
| import org.maltparser.ml.lib.LibSvm;
|
| import org.maltparser.parser.guide.ClassifierGuide;
|
| import org.maltparser.parser.guide.GuideException;
|
| import org.maltparser.parser.guide.Model;
|
| import org.maltparser.parser.history.action.SingleDecision;
|
|
|
|
|
| |
| |
| |
|
|
| public class AtomicModel implements InstanceModel {
|
| public static final Class<?>[] argTypes = { org.maltparser.parser.guide.instance.InstanceModel.class, java.lang.Integer.class };
|
| private final Model parent;
|
| private final String modelName;
|
|
|
| private final int index;
|
| private final LearningMethod method;
|
| private int frequency = 0;
|
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| public AtomicModel(int index, Model parent) throws MaltChainedException {
|
| this.parent = parent;
|
| this.index = index;
|
| if (index == -1) {
|
| this.modelName = parent.getModelName()+".";
|
| } else {
|
| this.modelName = parent.getModelName()+"."+new Formatter().format("%03d", index)+".";
|
| }
|
|
|
| this.frequency = 0;
|
| Integer learnerMode = null;
|
| if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.CLASSIFY) {
|
| learnerMode = LearningMethod.CLASSIFY;
|
| } else if (getGuide().getGuideMode() == ClassifierGuide.GuideMode.BATCH) {
|
| learnerMode = LearningMethod.BATCH;
|
| }
|
|
|
|
|
| Class<?> clazz = (Class<?>)getGuide().getConfiguration().getOptionValue("guide", "learner");
|
| if (clazz == org.maltparser.ml.lib.LibSvm.class) {
|
| this.method = new LibSvm(this, learnerMode);
|
| } else if (clazz == org.maltparser.ml.lib.LibLinear.class) {
|
| this.method = new LibLinear(this, learnerMode);
|
| } else {
|
| Object[] arguments = {this, learnerMode};
|
| try {
|
| Constructor<?> constructor = clazz.getConstructor(argTypes);
|
| this.method = (LearningMethod)constructor.newInstance(arguments);
|
| } catch (NoSuchMethodException e) {
|
| throw new GuideException("The learner class '"+clazz.getName()+"' cannot be initialized. ", e);
|
| } catch (InstantiationException e) {
|
| throw new GuideException("The learner class '"+clazz.getName()+"' cannot be initialized. ", e);
|
| } catch (IllegalAccessException e) {
|
| throw new GuideException("The learner class '"+clazz.getName()+"' cannot be initialized. ", e);
|
| } catch (InvocationTargetException e) {
|
| throw new GuideException("The learner class '"+clazz.getName()+"' cannot be initialized. ", e);
|
| }
|
| }
|
|
|
|
|
| if (learnerMode == LearningMethod.BATCH && index == -1 && getGuide().getConfiguration() != null) {
|
| getGuide().getConfiguration().writeInfoToConfigFile(method.toString());
|
| }
|
| }
|
|
|
| public void addInstance(FeatureVector featureVector, SingleDecision decision) throws MaltChainedException {
|
| try {
|
| method.addInstance(decision, featureVector);
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
| }
|
|
|
|
|
| public void noMoreInstances(FeatureModel featureModel) throws MaltChainedException {
|
| try {
|
| method.noMoreInstances();
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
| }
|
|
|
| public void finalizeSentence(DependencyStructure dependencyGraph) throws MaltChainedException {
|
| try {
|
| method.finalizeSentence(dependencyGraph);
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
| }
|
|
|
| public boolean predict(FeatureVector featureVector, SingleDecision decision) throws MaltChainedException {
|
| try {
|
| return method.predict(featureVector, decision);
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
| }
|
|
|
| public FeatureVector predictExtract(FeatureVector featureVector, SingleDecision decision) throws MaltChainedException {
|
| try {
|
|
|
| if (method.predict(featureVector, decision)) {
|
| return featureVector;
|
| }
|
| return null;
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
| }
|
|
|
| public FeatureVector extract(FeatureVector featureVector) throws MaltChainedException {
|
| return featureVector;
|
| }
|
|
|
| public void terminate() throws MaltChainedException {
|
| if (method != null) {
|
| method.terminate();
|
| }
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| public void moveAllInstances(AtomicModel model, FeatureFunction divideFeature, ArrayList<Integer> divideFeatureIndexVector) throws MaltChainedException {
|
| if (method == null) {
|
| throw new GuideException("The learner cannot be found. ");
|
| } else if (model == null) {
|
| throw new GuideException("The guide model cannot be found. ");
|
| } else if (divideFeature == null) {
|
| throw new GuideException("The divide feature cannot be found. ");
|
| } else if (divideFeatureIndexVector == null) {
|
| throw new GuideException("The divide feature index vector cannot be found. ");
|
| }
|
| ((Modifiable)divideFeature).setFeatureValue(index);
|
| method.moveAllInstances(model.getMethod(), divideFeature, divideFeatureIndexVector);
|
| method.terminate();
|
| }
|
|
|
| |
| |
| |
| |
|
|
| public void train() throws MaltChainedException {
|
| try {
|
| method.train();
|
| method.terminate();
|
| } catch (NullPointerException e) {
|
| throw new GuideException("The learner cannot be found. ", e);
|
| }
|
|
|
|
|
| }
|
|
|
| |
| |
| |
| |
|
|
| public Model getParent() throws MaltChainedException {
|
| if (parent == null) {
|
| throw new GuideException("The atomic model can only be used by a parent model. ");
|
| }
|
| return parent;
|
| }
|
|
|
|
|
| public String getModelName() {
|
| return modelName;
|
| }
|
|
|
| |
| |
| |
| |
|
|
|
|
|
|
|
|
|
|
| public ClassifierGuide getGuide() {
|
| return parent.getGuide();
|
| }
|
|
|
| |
| |
| |
| |
|
|
| public int getIndex() {
|
| return index;
|
| }
|
|
|
| |
| |
| |
| |
|
|
| public int getFrequency() {
|
| return frequency;
|
| }
|
|
|
| |
| |
|
|
| public void increaseFrequency() {
|
| if (parent instanceof InstanceModel) {
|
| ((InstanceModel)parent).increaseFrequency();
|
| }
|
| frequency++;
|
| }
|
|
|
| public void decreaseFrequency() {
|
| if (parent instanceof InstanceModel) {
|
| ((InstanceModel)parent).decreaseFrequency();
|
| }
|
| frequency--;
|
| }
|
| |
| |
| |
| |
|
|
| protected void setFrequency(int frequency) {
|
| this.frequency = frequency;
|
| }
|
|
|
| |
| |
| |
| |
|
|
| public LearningMethod getMethod() {
|
| return method;
|
| }
|
|
|
|
|
| |
| |
|
|
| public String toString() {
|
| final StringBuilder sb = new StringBuilder();
|
| sb.append(method.toString());
|
| return sb.toString();
|
| }
|
| }
|
|
|