
eus.ixa.ixa.pipe.ml.features.CharacterNgramFeatureGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ixa-pipe-ml Show documentation
Show all versions of ixa-pipe-ml Show documentation
IXA pipes machine learning component (ixa2.si.ehu.es/ixa-pipes).
/*
* Copyright 2014 Rodrigo Agerri
Licensed 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 eus.ixa.ixa.pipe.ml.features;
import java.util.List;
import java.util.Map;
import eus.ixa.ixa.pipe.ml.utils.Flags;
import opennlp.tools.ngram.NGramModel;
import opennlp.tools.util.InvalidFormatException;
import opennlp.tools.util.StringList;
import opennlp.tools.util.featuregen.CustomFeatureGenerator;
import opennlp.tools.util.featuregen.FeatureGeneratorResourceProvider;
/**
* The {@link CharacterNgramFeatureGenerator} uses character ngrams to
* generate features about each token.
* The minimum and maximum length can be specified.
*/
public class CharacterNgramFeatureGenerator extends CustomFeatureGenerator {
private Map attributes;
/**
* Initializes the current instance.
*/
public CharacterNgramFeatureGenerator() {
}
public void createFeatures(List features, String[] tokens, int index, String[] preds) {
NGramModel model = new NGramModel();
model.add(tokens[index], Integer.parseInt(attributes.get("minLength")), Integer.parseInt(attributes.get("maxLength")));
for (StringList tokenList : model) {
if (tokenList.size() > 0) {
features.add("ng=" + tokenList.getToken(0).toLowerCase());
if (Flags.DEBUG) {
System.err.println("-> " + tokenList.getToken(0).toLowerCase() + ": ng=" + tokenList.getToken(0).toLowerCase());
}
}
}
}
@Override
public void updateAdaptiveData(String[] tokens, String[] outcomes) {
}
@Override
public void clearAdaptiveData() {
}
@Override
public void init(Map properties,
FeatureGeneratorResourceProvider resourceProvider)
throws InvalidFormatException {
this.attributes = properties;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy