use of com.joliciel.talismane.machineLearning.features.BooleanFeature in project talismane by joliciel-informatique.
the class PosTaggerFeatureParser method getRules.
/**
* @param ruleDescriptors
* @return
* @throws TalismaneException
* if a rule is incorrectly configured
*/
public List<PosTaggerRule> getRules(List<String> ruleDescriptors) throws TalismaneException {
List<PosTaggerRule> rules = new ArrayList<PosTaggerRule>();
FunctionDescriptorParser descriptorParser = new FunctionDescriptorParser();
for (String ruleDescriptor : ruleDescriptors) {
LOG.debug(ruleDescriptor);
if (ruleDescriptor.length() > 0 && !ruleDescriptor.startsWith("#")) {
String[] ruleParts = ruleDescriptor.split("\t");
String posTagCode = ruleParts[0];
PosTag posTag = null;
boolean negative = false;
String descriptor = null;
String descriptorName = null;
if (ruleParts.length > 2) {
descriptor = ruleParts[2];
descriptorName = ruleParts[1];
} else {
descriptor = ruleParts[1];
}
if (posTagCode.length() == 0) {
if (descriptorName == null) {
throw new TalismaneException("Rule without PosTag must have a name.");
}
} else {
if (posTagCode.startsWith("!")) {
negative = true;
posTagCode = posTagCode.substring(1);
}
posTag = TalismaneSession.get(sessionId).getPosTagSet().getPosTag(posTagCode);
}
FunctionDescriptor functionDescriptor = descriptorParser.parseDescriptor(descriptor);
if (descriptorName != null)
functionDescriptor.setDescriptorName(descriptorName);
List<PosTaggerFeature<?>> myFeatures = this.parseDescriptor(functionDescriptor);
if (posTag != null) {
for (PosTaggerFeature<?> feature : myFeatures) {
if (feature instanceof BooleanFeature) {
@SuppressWarnings("unchecked") BooleanFeature<PosTaggerContext> condition = (BooleanFeature<PosTaggerContext>) feature;
PosTaggerRule rule = new PosTaggerRule(condition, posTag);
rule.setNegative(negative);
rules.add(rule);
} else {
throw new TalismaneException("Rule must be based on a boolean feature.");
}
}
// next feature
}
// is it a rule, or just a descriptor
}
// proper rule descriptor
}
// next rule descriptor
return rules;
}
use of com.joliciel.talismane.machineLearning.features.BooleanFeature in project talismane by joliciel-informatique.
the class ParserFeatureParser method getRules.
public List<ParserRule> getRules(List<String> ruleDescriptors) throws TalismaneException {
List<ParserRule> rules = new ArrayList<ParserRule>();
FunctionDescriptorParser descriptorParser = new FunctionDescriptorParser();
for (String ruleDescriptor : ruleDescriptors) {
LOG.debug(ruleDescriptor);
if (ruleDescriptor.trim().length() > 0 && !ruleDescriptor.startsWith("#")) {
String[] ruleParts = ruleDescriptor.split("\t");
String transitionCode = ruleParts[0];
Transition transition = null;
Set<Transition> transitions = null;
boolean negative = false;
String descriptor = null;
String descriptorName = null;
if (ruleParts.length > 2) {
descriptor = ruleParts[2];
descriptorName = ruleParts[1];
} else {
descriptor = ruleParts[1];
}
if (transitionCode.length() == 0) {
if (descriptorName == null) {
throw new TalismaneException("Rule without Transition must have a name.");
}
} else {
if (transitionCode.startsWith("!")) {
negative = true;
String[] transitionCodes = transitionCode.substring(1).split(";");
transitions = new HashSet<Transition>();
for (String code : transitionCodes) {
Transition oneTransition = TalismaneSession.get(sessionId).getTransitionSystem().getTransitionForCode(code);
transitions.add(oneTransition);
}
transition = transitions.iterator().next();
} else {
transition = TalismaneSession.get(sessionId).getTransitionSystem().getTransitionForCode(transitionCode);
}
}
FunctionDescriptor functionDescriptor = descriptorParser.parseDescriptor(descriptor);
if (descriptorName != null)
functionDescriptor.setDescriptorName(descriptorName);
List<ParseConfigurationFeature<?>> myFeatures = this.parseDescriptor(functionDescriptor);
if (transition != null) {
for (ParseConfigurationFeature<?> feature : myFeatures) {
if (feature instanceof BooleanFeature) {
@SuppressWarnings("unchecked") BooleanFeature<ParseConfigurationWrapper> condition = (BooleanFeature<ParseConfigurationWrapper>) feature;
if (negative) {
ParserRule rule = new ParserRule(condition, transitions, true);
rules.add(rule);
} else {
ParserRule rule = new ParserRule(condition, transition, false);
rules.add(rule);
}
} else {
throw new TalismaneException("Rule must be based on a boolean feature.");
}
}
// next feature
}
// is it a rule, or just a descriptor
}
// proper rule descriptor
}
// next rule descriptor
return rules;
}
Aggregations