001 /*
002 * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003 *
004 * This file is part of the Java Data Mining Package (JDMP).
005 * See the NOTICE file distributed with this work for additional
006 * information regarding copyright ownership and licensing.
007 *
008 * JDMP is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * JDMP is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public
019 * License along with JDMP; if not, write to the
020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021 * Boston, MA 02110-1301 USA
022 */
023
024 package org.jdmp.mallet.tagger;
025
026 import java.util.ArrayList;
027 import java.util.List;
028
029 import org.jdmp.core.algorithm.tagger.AbstractTagger;
030 import org.ujmp.core.Matrix;
031
032 import cc.mallet.fst.CRF;
033 import cc.mallet.fst.CRFOptimizableByLabelLikelihood;
034 import cc.mallet.fst.CRFTrainerByValueGradients;
035 import cc.mallet.fst.SimpleTagger;
036 import cc.mallet.optimize.Optimizable;
037 import cc.mallet.pipe.Pipe;
038 import cc.mallet.pipe.SerialPipes;
039 import cc.mallet.pipe.TokenSequence2FeatureVectorSequence;
040 import cc.mallet.pipe.tsf.TokenText;
041 import cc.mallet.pipe.tsf.TokenTextCharNGrams;
042 import cc.mallet.pipe.tsf.TokenTextCharPrefix;
043 import cc.mallet.pipe.tsf.TokenTextCharSuffix;
044 import cc.mallet.types.Alphabet;
045 import cc.mallet.types.Instance;
046 import cc.mallet.types.InstanceList;
047 import cc.mallet.util.CommandOption;
048
049 public class MalletTagger extends AbstractTagger {
050 private static final long serialVersionUID = 4905729231422289232L;
051
052 private static final CommandOption.String defaultOption = new CommandOption.String(
053 SimpleTagger.class, "default-label", "STRING", true, "O",
054 "Label for initial context and uninteresting tokens", null);
055
056 private CRF crf = null;
057
058 private CRFTrainerByValueGradients crfTrainer = null;
059
060 private SerialPipes serialPipes = null;
061
062 public MalletTagger() {
063 }
064
065 @Override
066 public List<Matrix> tag(String input) throws Exception {
067 return null;
068 }
069
070 @Override
071 public List<Matrix> tag(Matrix input) throws Exception {
072 return null;
073 }
074
075 private SerialPipes getPipes() {
076 if (serialPipes == null) {
077 List<Pipe> pipes = new ArrayList<Pipe>();
078 pipes.add(new Matrix2TokenSequencePipe());
079 pipes.add(new TokenText());
080 pipes.add(new TokenTextCharPrefix());
081 pipes.add(new TokenTextCharSuffix());
082 pipes.add(new TokenTextCharNGrams());
083 pipes.add(new TokenSequence2FeatureVectorSequence());
084 serialPipes = new SerialPipes(pipes);
085 serialPipes.getTargetAlphabet().lookupIndex(defaultOption.value);
086 serialPipes.setTargetProcessing(true);
087 }
088 return serialPipes;
089 }
090
091 public void train(List<String> sentences) throws Exception {
092 for (String s : sentences) {
093 // train(s);
094 }
095 }
096
097 public Alphabet getLabelAlphabet() {
098 return getPipes().getTargetAlphabet();
099 }
100
101 public Alphabet getDataAlphabet() {
102 return getPipes().getDataAlphabet();
103 }
104
105 public void train(Matrix matrix) throws Exception {
106 Instance matrixInstance = new Instance(matrix, null, null, null);
107 InstanceList trainingData = new InstanceList(getPipes());
108 trainingData.addThruPipe(matrixInstance);
109
110 if (crf == null) {
111 crf = new CRF(getPipes(), null);
112 crf.addFullyConnectedStatesForLabels();
113 crf.setWeightsDimensionAsIn(trainingData, false);
114 CRFOptimizableByLabelLikelihood optLabel = new CRFOptimizableByLabelLikelihood(
115 crf, trainingData);
116 Optimizable.ByGradientValue[] opts = new Optimizable.ByGradientValue[] { optLabel };
117 crfTrainer = new CRFTrainerByValueGradients(crf, opts);
118 crfTrainer.setMaxResets(0);
119 }
120
121 crfTrainer.train(trainingData, Integer.MAX_VALUE);
122 }
123
124 public void test(Matrix matrix) throws Exception {
125 Instance matrixInstance = new Instance(matrix, null, null, null);
126 Instance i = crf.label(matrixInstance);
127 System.out.println(i);
128 }
129
130 }