edu.stanford.nlp.ling.tokensregex.BasicSequenceMatchResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stanford-parser Show documentation
Show all versions of stanford-parser Show documentation
Stanford Parser processes raw text in English, Chinese, German, Arabic, and French, and extracts constituency parse trees.
package edu.stanford.nlp.ling.tokensregex;
import java.util.function.Function;
import edu.stanford.nlp.util.Interval;
import edu.stanford.nlp.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Basic results for a Sequence Match
*
* @author Angel Chang
*/
public class BasicSequenceMatchResult implements SequenceMatchResult {
SequencePattern pattern; // Pattern we matched against
List extends T> elements; // Original sequence
MatchedGroup[] matchedGroups; // Groups that we matched
Object[] matchedResults; // Additional information about matches (per element)
Function, String> nodesToStringConverter;
SequencePattern.VarGroupBindings varGroupBindings;
double score = 0.0;
double priority = 0.0;
int order;
@Override
public List extends T> elements() { return elements; }
@Override
public SequencePattern pattern() { return pattern; }
// public static BasicSequenceMatchResult toBasicSequenceMatchResult(List extends T> elements) {
// BasicSequenceMatchResult matchResult = new BasicSequenceMatchResult();
// matchResult.elements = elements;
// matchResult.matchedGroups = new MatchedGroup[0];
// return matchResult;
// }
@Override
public BasicSequenceMatchResult toBasicSequenceMatchResult() {
return copy();
}
public BasicSequenceMatchResult copy() {
BasicSequenceMatchResult res = new BasicSequenceMatchResult<>();
res.pattern = pattern;
res.elements = elements;
res.matchedGroups = new MatchedGroup[matchedGroups.length];
res.nodesToStringConverter = nodesToStringConverter;
res.score = score;
res.priority = priority;
res.order = order;
res.varGroupBindings = varGroupBindings;
for (int i = 0; i < matchedGroups.length; i++ ) {
if (matchedGroups[i] != null) {
res.matchedGroups[i] = new MatchedGroup(matchedGroups[i]);
}
}
if (matchedResults != null) {
res.matchedResults = new Object[matchedResults.length];
System.arraycopy(res.matchedResults, 0, matchedResults, 0, matchedResults.length);
}
return res;
}
@Override
public Interval getInterval() {
return TO_INTERVAL.apply(this);
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
@Override
public double priority() {
return priority;
}
@Override
public double score() {
return score;
}
@Override
public int start() {
return start(0);
}
@Override
public int start(int group) {
if (group == GROUP_BEFORE_MATCH) {
return 0;
} else if (group == GROUP_AFTER_MATCH) {
return matchedGroups[0].matchEnd;
}
if (matchedGroups[group] != null) {
return matchedGroups[group].matchBegin;
} else {
return -1;
}
}
@Override
public int start(String var) {
int g = getFirstVarGroup(var);
if (g >= 0) {
return start(g);
} else {
return -1;
}
}
@Override
public int end() {
return end(0);
}
@Override
public int end(int group) {
if (group == GROUP_BEFORE_MATCH) {
return matchedGroups[0].matchBegin;
} else if (group == GROUP_AFTER_MATCH) {
return elements.size();
}
if (matchedGroups[group] != null) {
return matchedGroups[group].matchEnd;
} else {
return -1;
}
}
@Override
public int end(String var) {
int g = getFirstVarGroup(var);
if (g >= 0) {
return end(g);
} else {
return -1;
}
}
@Override
public String group() {
return group(0);
}
@Override
public String group(int group) {
List extends T> groupTokens = groupNodes(group);
if (nodesToStringConverter == null) {
return (groupTokens != null)? StringUtils.join(groupTokens, " "): null;
} else {
return nodesToStringConverter.apply(groupTokens);
}
}
@Override
public String group(String var) {
int g = getFirstVarGroup(var);
if (g >= 0) {
return group(g);
} else {
return null;
}
}
@Override
public List groupNodes() {
return groupNodes(0);
}
@Override
public List groupNodes(int group) {
if (group == GROUP_BEFORE_MATCH || group == GROUP_AFTER_MATCH) {
// return a new list so the resulting object is serializable
return new ArrayList<>(elements.subList(start(group), end(group)));
}
if (matchedGroups.length > group && matchedGroups[group] != null) {
// return a new list so the resulting object is serializable
return new ArrayList<>(elements.subList(matchedGroups[group].matchBegin, matchedGroups[group].matchEnd));
} else {
return null;
}
}
@Override
public List extends T> groupNodes(String var) {
int g = getFirstVarGroup(var);
if (g >= 0) {
return groupNodes(g);
} else {
return null;
}
}
@Override
public Object groupValue() {
return groupValue(0);
}
@Override
public Object groupValue(int group) {
if (group == GROUP_BEFORE_MATCH || group == GROUP_AFTER_MATCH) {
// return a new list so the resulting object is serializable
return new ArrayList<>(elements.subList(start(group), end(group)));
}
if (matchedGroups[group] != null) {
return matchedGroups[group].value;
} else {
return null;
}
}
@Override
public Object groupValue(String var) {
int g = getFirstVarGroup(var);
if (g >= 0) {
return groupValue(g);
} else {
return null;
}
}
@Override
public MatchedGroupInfo groupInfo() {
return groupInfo(0);
}
@Override
public MatchedGroupInfo groupInfo(int group) {
List extends T> nodes = groupNodes(group);
if (nodes != null) {
Object value = groupValue(group);
String text = group(group);
List