Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1684: Reduce creation of String instances in BrownBigramFeatureGenerator #731

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
public class BrownBigramFeatureGenerator implements AdaptiveFeatureGenerator {

private static final String BROWNCLUSTER = "browncluster";
private static final String FEATURE_NEXT_BROWNCLUSTER_BASE = BROWNCLUSTER + ",n" + BROWNCLUSTER + "=";
private static final String FEATURE_PREV_BROWNCLUSTER_BASE = "p" + BROWNCLUSTER + "," + BROWNCLUSTER + "=";

private final BrownCluster brownCluster;

/**
Expand All @@ -38,20 +42,18 @@ public BrownBigramFeatureGenerator(BrownCluster brownCluster) {
public void createFeatures(List<String> features, String[] tokens, int index,
String[] previousOutcomes) {

List<String> wordClasses = BrownTokenClasses.getWordClasses(tokens[index], brownCluster);
List<String> wc = BrownTokenClasses.getWordClasses(tokens[index], brownCluster);
if (index > 0) {
List<String> prevWordClasses = BrownTokenClasses.getWordClasses(tokens[index - 1], brownCluster);
for (int i = 0; i < wordClasses.size() && i < prevWordClasses.size(); i++) {
features.add("p" + "browncluster" + "," + "browncluster" + "="
+ prevWordClasses.get(i) + "," + wordClasses.get(i));
List<String> prevWC = BrownTokenClasses.getWordClasses(tokens[index - 1], brownCluster);
for (int i = 0; i < wc.size() && i < prevWC.size(); i++) {
features.add(FEATURE_PREV_BROWNCLUSTER_BASE + prevWC.get(i) + "," + wc.get(i));
}
}

if (index + 1 < tokens.length) {
List<String> nextWordClasses = BrownTokenClasses.getWordClasses(tokens[index + 1], brownCluster);
for (int i = 0; i < wordClasses.size() && i < nextWordClasses.size(); i++) {
features.add("browncluster" + "," + "n" + "browncluster" + "="
+ wordClasses.get(i) + "," + nextWordClasses.get(i));
for (int i = 0; i < wc.size() && i < nextWordClasses.size(); i++) {
features.add(FEATURE_NEXT_BROWNCLUSTER_BASE + wc.get(i) + "," + nextWordClasses.get(i));
}
}
}
Expand Down
Loading