Skip to content

Commit d3aa54d

Browse files
committed
move failsafe that checks for no votes < threshold into the classifier itself and update the analysis string as well.
1 parent 1b989c2 commit d3aa54d

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

java/src/org/antlr/codebuff/Formatter.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.antlr.codebuff;
22

3-
import org.antlr.codebuff.misc.HashBag;
43
import org.antlr.v4.runtime.CommonToken;
54
import org.antlr.v4.runtime.CommonTokenStream;
65
import org.antlr.v4.runtime.ParserRuleContext;
@@ -17,14 +16,12 @@
1716
import static org.antlr.codebuff.CollectFeatures.CAT_ALIGN_WITH_ANCESTOR_CHILD;
1817
import static org.antlr.codebuff.CollectFeatures.CAT_INDENT;
1918
import static org.antlr.codebuff.CollectFeatures.CAT_INDENT_FROM_ANCESTOR_FIRST_TOKEN;
20-
import static org.antlr.codebuff.CollectFeatures.CAT_NO_ALIGNMENT;
2119
import static org.antlr.codebuff.CollectFeatures.FEATURES_ALIGN;
2220
import static org.antlr.codebuff.CollectFeatures.FEATURES_INJECT_NL;
2321
import static org.antlr.codebuff.CollectFeatures.FEATURES_INJECT_WS;
2422
import static org.antlr.codebuff.CollectFeatures.INDEX_FIRST_ON_LINE;
2523
import static org.antlr.codebuff.CollectFeatures.INDEX_PREV_END_COLUMN;
2624
import static org.antlr.codebuff.CollectFeatures.MAX_CONTEXT_DIFF_THRESHOLD;
27-
import static org.antlr.codebuff.CollectFeatures.MAX_CONTEXT_DIFF_THRESHOLD2;
2825
import static org.antlr.codebuff.CollectFeatures.earliestAncestorStartingWithToken;
2926
import static org.antlr.codebuff.CollectFeatures.getNodeFeatures;
3027
import static org.antlr.codebuff.CollectFeatures.getRealTokens;
@@ -126,13 +123,6 @@ public void processToken(int indexIntoRealTokens, int tokenIndexInStream) {
126123
features[INDEX_FIRST_ON_LINE] = injectNewline; // use \n prediction to match exemplars for alignment
127124

128125
int align = alignClassifier.classify(k, features, corpus.align, MAX_CONTEXT_DIFF_THRESHOLD);
129-
if ( align==CAT_NO_ALIGNMENT ) {
130-
HashBag<Integer> votes = alignClassifier.votes(k, features, corpus.align, MAX_CONTEXT_DIFF_THRESHOLD);
131-
if ( votes.size()==0 ) {
132-
// try with less strict match threshold to get some indication of alignment
133-
align = alignClassifier.classify(k, features, corpus.align, MAX_CONTEXT_DIFF_THRESHOLD2);
134-
}
135-
}
136126

137127
int ws = wsClassifier.classify(k, features, corpus.injectWS, MAX_CONTEXT_DIFF_THRESHOLD);
138128

java/src/org/antlr/codebuff/kNNClassifier.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.Arrays;
88
import java.util.List;
99

10+
import static org.antlr.codebuff.CollectFeatures.MAX_CONTEXT_DIFF_THRESHOLD2;
11+
1012
/** A kNN (k-Nearest Neighbor) classifier */
1113
public abstract class kNNClassifier {
1214
protected final Corpus corpus;
@@ -50,6 +52,10 @@ public int[] classify(int k, int[] unknown, double distanceThreshold) {
5052
*/
5153
public int classify(int k, int[] unknown, List<Integer> Y, double distanceThreshold) {
5254
HashBag<Integer> votes = votes(k, unknown, Y, distanceThreshold);
55+
if ( votes.size()==0 ) {
56+
// try with less strict match threshold to get some indication of alignment
57+
votes = votes(k, unknown, Y, MAX_CONTEXT_DIFF_THRESHOLD2);
58+
}
5359
return getCategoryWithMostVotes(votes);
5460
}
5561

@@ -94,6 +100,11 @@ public HashBag<Integer> getVotesBag(Neighbor[] kNN, int k, int[] unknown, List<I
94100
public String getPredictionAnalysis(InputDocument doc, int k, int[] unknown, List<Integer> Y, double distanceThreshold) {
95101
Neighbor[] kNN = kNN(unknown, k, distanceThreshold);
96102
HashBag<Integer> votes = getVotesBag(kNN, k, unknown, Y);
103+
if ( votes.size()==0 ) {
104+
// try with less strict match threshold to get some indication of alignment
105+
kNN = kNN(unknown, k, MAX_CONTEXT_DIFF_THRESHOLD2);
106+
votes = getVotesBag(kNN, k, unknown, Y);
107+
}
97108

98109
StringBuilder buf = new StringBuilder();
99110
buf.append(CollectFeatures.featureNameHeader(FEATURES));

0 commit comments

Comments
 (0)