Skip to content

Commit d9d89e9

Browse files
AngledLuffaStanford NLP
authored and
Stanford NLP
committed
Factor out the code to read the weight argument
1 parent 745a787 commit d9d89e9

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/edu/stanford/nlp/parser/common/ArgUtils.java

+21-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import java.io.FileFilter;
44
import java.io.PrintStream;
5+
import java.util.regex.Pattern;
56
import edu.stanford.nlp.io.NumberRangeFileFilter;
67
import edu.stanford.nlp.io.NumberRangesFileFilter;
78
import edu.stanford.nlp.util.Pair;
9+
import edu.stanford.nlp.util.Triple;
810

911
/**
1012
* Utility methods or common blocks of code for dealing with parser
@@ -30,14 +32,28 @@ public static void printArgs(String[] args, PrintStream ps) {
3032
ps.println();
3133
}
3234

35+
static final Pattern DOUBLE_PATTERN = Pattern.compile("[-]?[0-9]+[.][0-9]+");
36+
3337
public static Pair<String, FileFilter> getTreebankDescription(String[] args, int argIndex, String flag) {
38+
Triple<String, FileFilter, Double> description = getWeightedTreebankDescription(args, argIndex, flag);
39+
return Pair.makePair(description.first(), description.second());
40+
}
41+
42+
public static Triple<String, FileFilter, Double> getWeightedTreebankDescription(String[] args, int argIndex, String flag) {
3443
String path = null;
3544
FileFilter filter = null;
45+
Double weight = 1.0;
3646
// the next arguments are the treebank path and maybe the range for testing
3747
int numSubArgs = numSubArgs(args, argIndex);
38-
if (numSubArgs > 0 && numSubArgs < 3) {
48+
if (numSubArgs > 0 && numSubArgs < 4) {
3949
argIndex++;
4050
path = args[argIndex++];
51+
boolean hasWeight = false;
52+
if (numSubArgs > 1 && DOUBLE_PATTERN.matcher(args[argIndex + numSubArgs - 2]).matches()) {
53+
weight = Double.parseDouble(args[argIndex + numSubArgs - 2]);
54+
hasWeight = true;
55+
numSubArgs--;
56+
}
4157
if (numSubArgs == 2) {
4258
filter = new NumberRangesFileFilter(args[argIndex++], true);
4359
} else if (numSubArgs == 3) {
@@ -51,9 +67,12 @@ public static Pair<String, FileFilter> getTreebankDescription(String[] args, int
5167
filter = new NumberRangesFileFilter(args[argIndex++], true);
5268
}
5369
}
70+
if (hasWeight) {
71+
argIndex++;
72+
}
5473
} else {
5574
throw new IllegalArgumentException("Bad arguments after " + flag);
5675
}
57-
return Pair.makePair(path, filter);
76+
return Triple.makeTriple(path, filter, weight);
5877
}
5978
}

src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -1245,18 +1245,12 @@ public static void main(String[] args) {
12451245
treebankPath = treebankDescription.first();
12461246
trainFilter = treebankDescription.second();
12471247
} else if (args[argIndex].equalsIgnoreCase("-train2")) {
1248-
// TODO: we could use the fully expressive -train options if
1249-
// we add some mechanism for returning leftover options from
1250-
// ArgUtils.getTreebankDescription
12511248
// train = true; // cdm july 2005: should require -train for this
1252-
int numSubArgs = ArgUtils.numSubArgs(args, argIndex);
1253-
argIndex++;
1254-
if (numSubArgs < 2) {
1255-
throw new RuntimeException("Error: -train2 <treebankPath> [<ranges>] <weight>.");
1256-
}
1257-
secondaryTreebankPath = args[argIndex++];
1258-
secondaryTrainFilter = (numSubArgs == 3) ? new NumberRangesFileFilter(args[argIndex++], true) : null;
1259-
secondaryTreebankWeight = Double.parseDouble(args[argIndex++]);
1249+
Triple<String, FileFilter, Double> treebankDescription = ArgUtils.getWeightedTreebankDescription(args, argIndex, "-train2");
1250+
argIndex = argIndex + ArgUtils.numSubArgs(args, argIndex) + 1;
1251+
secondaryTreebankPath = treebankDescription.first();
1252+
secondaryTrainFilter = treebankDescription.second();
1253+
secondaryTreebankWeight = treebankDescription.third();
12601254
} else if (args[argIndex].equalsIgnoreCase("-tLPP") && (argIndex + 1 < args.length)) {
12611255
try {
12621256
op.tlpParams = (TreebankLangParserParams) Class.forName(args[argIndex + 1]).newInstance();

0 commit comments

Comments
 (0)