
eus.ixa.ixa.pipe.nerc.features.TokenClassFeatureGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ixa-pipe-nerc Show documentation
Show all versions of ixa-pipe-nerc Show documentation
ixa pipes sequence labeler (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.nerc.features;
import java.util.List;
import java.util.regex.Pattern;
import opennlp.tools.util.featuregen.FeatureGeneratorAdapter;
import opennlp.tools.util.featuregen.StringPattern;
/**
* Generates a class name for the specified token. The classes are as follows
* where the first matching class is used:
*
* - lc - lowercase alphabetic
* - 2d - two digits
* - 4d - four digits
* - an - alpha-numeric
* - dd - digits and dashes
* - ds - digits and slashes
* - dc - digits and commas
* - dp - digits and periods
* - num - digits
* - sc - single capital letter
* - ac - all capital letters
* - ic - initial capital letter
* - other - other
*
*/
public class TokenClassFeatureGenerator extends FeatureGeneratorAdapter {
private static Pattern capPeriod;
static {
capPeriod = Pattern.compile("^[A-Z]\\.$");
}
private boolean generateWordAndClassFeature;
public TokenClassFeatureGenerator() {
this(true);
}
public TokenClassFeatureGenerator(boolean generateWordAndClassFeature) {
this.generateWordAndClassFeature = generateWordAndClassFeature;
}
public void createFeatures(List features, String[] tokens, int index,
String[] preds) {
String wordClass = tokenShapeFeature(tokens[index]);
features.add("wc=" + wordClass);
if (generateWordAndClassFeature) {
features.add("w&c=" + tokens[index].toLowerCase()
+ "," + wordClass);
}
}
public static String tokenShapeFeature(String token) {
StringPattern pattern = StringPattern.recognize(token);
String feat;
if (pattern.isAllLowerCaseLetter()) {
feat = "lc";
} else if (pattern.digits() == 2) {
feat = "2d";
} else if (pattern.digits() == 4) {
feat = "4d";
}
else if (pattern.containsDigit()) {
if (pattern.containsLetters()) {
feat = "an";
} else if (pattern.containsHyphen()) {
feat = "dd";
} else if (pattern.containsSlash()) {
feat = "ds";
} else if (pattern.containsComma()) {
feat = "dc";
} else if (pattern.containsPeriod()) {
feat = "dp";
} else {
feat = "num";
}
} else if (pattern.isAllCapitalLetter() && token.length() == 1) {
feat = "sc";
} else if (pattern.isAllCapitalLetter()) {
feat = "ac";
} else if (capPeriod.matcher(token).find()) {
feat = "cp";
} else if (pattern.isInitialCapitalLetter()) {
feat = "ic";
} else {
feat = "other";
}
return (feat);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy