use of edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors in project cogcomp-nlp by CogComp.
the class ChunkerTrain method trainModelsWithParser.
public void trainModelsWithParser(Parser parser, String modeldir, String modelname, double dev_ratio) {
Chunker.isTraining = true;
double tmpF1 = 0;
double bestF1 = 0;
int bestIter = 0;
double[] F1array = new double[iter];
String lcpath = modeldir + File.separator + modelname + ".lc";
String lexpath = modeldir + File.separator + modelname + ".lex";
// Get the total number of training set
int cnt = 0;
LinkedVector ex;
while ((ex = (LinkedVector) parser.next()) != null) {
cnt++;
}
parser.reset();
// Get the boundary between train and dev
long idx = Math.round(cnt * (1 - dev_ratio));
if (idx < 0)
idx = 0;
if (idx > cnt)
idx = cnt;
// Run the learner and save F1 for each iteration
for (int i = 1; i <= iter; i++) {
cnt = 0;
while ((ex = (LinkedVector) parser.next()) != null) {
for (int j = 0; j < ex.size(); j++) {
chunker.learn(ex.get(j));
}
if (cnt >= idx)
break;
else
cnt++;
}
chunker.doneWithRound();
writeModelsToDisk(modeldir, modelname);
// Test on dev set
BIOTester tester = new BIOTester(new Chunker(lcpath, lexpath), new ChunkLabel(), new ChildrenFromVectors(parser));
double[] result = tester.test().getOverallStats();
tmpF1 = result[2];
F1array[i - 1] = tmpF1;
System.out.println("Iteration number : " + i + ". F1 score on devset: " + tmpF1);
parser.reset();
}
// Get the best F1 score and corresponding iter
for (int i = 0; i < iter; i++) {
if (F1array[i] > bestF1) {
bestF1 = F1array[i];
bestIter = i + 1;
}
}
System.out.println("Best #Iter = " + bestIter + " (F1=" + bestF1 + ")");
System.out.println("Rerun the learner using best #Iter...");
// Rerun the learner
for (int i = 1; i <= bestIter; i++) {
while ((ex = (LinkedVector) parser.next()) != null) {
for (int j = 0; j < ex.size(); j++) {
chunker.learn(ex.get(j));
}
}
parser.reset();
chunker.doneWithRound();
System.out.println("Iteration number : " + i);
}
chunker.doneLearning();
}
use of edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors in project cogcomp-nlp by CogComp.
the class ChunkTester method chunkTester.
public static void chunkTester(String testFile, String modeldir, String modelname) {
Parser parser;
String lcpath = modeldir + File.separator + modelname + ".lc";
String lexpath = modeldir + File.separator + modelname + ".lex";
parser = new CoNLL2000Parser(testFile);
BIOTester tester = new BIOTester(new Chunker(lcpath, lexpath), new ChunkLabel(), new ChildrenFromVectors(parser));
tester.test().printPerformance(System.out);
}
use of edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors in project cogcomp-nlp by CogComp.
the class TestChunkerModels method testAccuracy.
public void testAccuracy() {
Parser parser = new ChildrenFromVectors(new CoNLL2000Parser(labeledData));
int numSeen = 0;
int numEqual = 0;
for (Token w = (Token) parser.next(); w != null; w = (Token) parser.next()) {
String prediction = tagger.discreteValue(w);
String raw = w.toString();
String actualChunk = raw.substring(raw.indexOf('(') + 1, raw.indexOf(' '));
if (prediction.equals(actualChunk)) {
numEqual++;
}
numSeen++;
}
logger.info("Total accuracy over " + numSeen + " items: " + String.format("%.2f", 100.0 * (double) numEqual / (double) numSeen) + "%");
}
Aggregations