@@ -0,0 +1,2530 @@
+package kldtz.github.com.hmmt.evaluation;%0A%0Aimport java.io.BufferedWriter;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException;%0Aimport java.io.UncheckedIOException;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.BitSet;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport kldtz.github.com.hmmt.corpus.CorpusFormat;%0Aimport kldtz.github.com.hmmt.counts.Counts;%0Aimport kldtz.github.com.hmmt.counts.CountsBuilder;%0Aimport kldtz.github.com.hmmt.tagger.TaggerBuilder;%0Aimport kldtz.github.com.hmmt.utils.Utils;%0A%0Apublic class KFoldCrossValidation %7B%0A%09static final Logger logger = LoggerFactory.getLogger(KFoldCrossValidation.class);%0A%09private Path corpusPath;%0A%09private CorpusFormat corpusFormat;%0A%09private int numSentences;%0A%09%0A%09public KFoldCrossValidation(Path corpusPath, CorpusFormat corpusFormat) %7B%0A%09%09this.corpusPath = corpusPath;%0A%09%09this.corpusFormat = corpusFormat;%0A%09%09numSentences = Utils.countSentencesInFile(corpusPath, corpusFormat);%0A%09%7D%0A%09%0A%09private int computeSubsetSize(int k) %7B%0A%09%09return Math.round((float) numSentences / k);%0A%09%7D%0A%09%0A%09private BitSet selectTestInstances(int fold, int subsetSize) %7B%0A%09%09BitSet testInstances = new BitSet();%0A%09%09testInstances.set(fold * subsetSize, Math.min((fold + 1) * subsetSize, numSentences));%0A%09%09return testInstances;%0A%09%7D%0A%09%0A%09public void trainAndTagOnFolds(Path output, int k) %7B%0A%09%09try (FileWriter fileWriter = new FileWriter(output.toFile());%0A%09%09%09%09BufferedWriter writer = new BufferedWriter(fileWriter);) %7B%0A%09%09%09SplitEvaluator splitEvaluator = new SplitEvaluator(corpusPath, corpusFormat, writer);%0A%09%09%09writer.write(EvaluationData.getHeader());%0A%09%09%09writer.newLine();%0A%09%09%09int subsetSize = computeSubsetSize(k);%0A%09%09%09for (int fold = 0; fold %3C k; fold++) %7B%0A%09%09%09%09logger.info(%22Starting iteration number %22 + (fold + 1));%0A%09%09%09%09BitSet testInstances = selectTestInstances(fold, subsetSize);%0A%09%09%09%09Counts counts = new CountsBuilder(corpusPath, corpusFormat, testInstances).build();%0A%09%09%09%09splitEvaluator.setLexicon(counts.getLexicon());%0A%09%09%09%09splitEvaluator.setTagger(new TaggerBuilder(counts).build());%0A%09%09%09%09splitEvaluator.evaluateSplit(testInstances);%0A%09%09%09%7D%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new UncheckedIOException(e);%0A%09%09%7D%0A%09%09logger.info(%22Completed all %22 + k + %22 iterations%22);%0A%09%7D%0A%09%0A%09public static void main(String%5B%5D args) %7B%0A%09%09Path corpusPath = Paths.get(%22data/private/tiger.conll%22);%0A%09%09KFoldCrossValidation mccv = new KFoldCrossValidation(corpusPath, CorpusFormat.CONLL);%0A%09%09Path output = Paths.get(%22data/private/10-Fold_Cross_Validation_Tiger.csv%22);%0A%09%09mccv.trainAndTagOnFolds(output, 10);%0A%09%7D%0A%7D%0A

Add 10-fold cross-validation