Search in sources :

Example 1 with ChunkLabel

use of edu.illinois.cs.cogcomp.chunker.main.lbjava.ChunkLabel 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();
}
Also used : ChildrenFromVectors(edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors) LinkedVector(edu.illinois.cs.cogcomp.lbjava.parse.LinkedVector) BIOTester(edu.illinois.cs.cogcomp.lbjava.nlp.seg.BIOTester) Chunker(edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker) ChunkLabel(edu.illinois.cs.cogcomp.chunker.main.lbjava.ChunkLabel)

Example 2 with ChunkLabel

use of edu.illinois.cs.cogcomp.chunker.main.lbjava.ChunkLabel 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);
}
Also used : ChildrenFromVectors(edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors) BIOTester(edu.illinois.cs.cogcomp.lbjava.nlp.seg.BIOTester) Chunker(edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker) ChunkLabel(edu.illinois.cs.cogcomp.chunker.main.lbjava.ChunkLabel) CoNLL2000Parser(edu.illinois.cs.cogcomp.chunker.utils.CoNLL2000Parser) Parser(edu.illinois.cs.cogcomp.lbjava.parse.Parser) CoNLL2000Parser(edu.illinois.cs.cogcomp.chunker.utils.CoNLL2000Parser)

Aggregations

ChunkLabel (edu.illinois.cs.cogcomp.chunker.main.lbjava.ChunkLabel)2 Chunker (edu.illinois.cs.cogcomp.chunker.main.lbjava.Chunker)2 BIOTester (edu.illinois.cs.cogcomp.lbjava.nlp.seg.BIOTester)2 ChildrenFromVectors (edu.illinois.cs.cogcomp.lbjava.parse.ChildrenFromVectors)2 CoNLL2000Parser (edu.illinois.cs.cogcomp.chunker.utils.CoNLL2000Parser)1 LinkedVector (edu.illinois.cs.cogcomp.lbjava.parse.LinkedVector)1 Parser (edu.illinois.cs.cogcomp.lbjava.parse.Parser)1