<commit_before>/** 
 * Copyright (c) 2009, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */

package org.cleartk.example.pos;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.File;

import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.pear.util.FileUtil;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.cleartk.CleartkComponents;
import org.cleartk.ViewNames;
import org.cleartk.classifier.libsvm.DefaultMultiClassLIBSVMDataWriterFactory;
import org.cleartk.classifier.mallet.DefaultMalletCRFDataWriterFactory;
import org.cleartk.classifier.mallet.DefaultMalletDataWriterFactory;
import org.cleartk.classifier.opennlp.DefaultMaxentDataWriterFactory;
import org.cleartk.classifier.svmlight.DefaultOVASVMlightDataWriterFactory;
import org.cleartk.classifier.viterbi.ViterbiClassifier;
import org.cleartk.sentence.opennlp.OpenNLPSentenceSegmenter;
import org.cleartk.syntax.treebank.TreebankGoldAnnotator;
import org.cleartk.token.TokenAnnotator;
import org.cleartk.token.snowball.DefaultSnowballStemmer;
import org.cleartk.token.snowball.SnowballStemmer;
import org.cleartk.util.FilesCollectionReader;
import org.junit.After;
import org.junit.Test;
import org.uutuc.factory.AnalysisEngineFactory;
import org.uutuc.factory.CollectionReaderFactory;
import org.uutuc.util.HideOutput;
import org.uutuc.util.SimplePipeline;
import org.uutuc.util.TearDownUtil;

/**
 * <br>
 * Copyright (c) 2009, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * @author <NAME>
 */

public class ExamplePosClassifierTest {

	private String baseDirectory = "test/data/example/pos";
	
	public static boolean runLongTests = false;
	
	@After
	public void tearDown() {
		TearDownUtil.removeDirectory(new File(baseDirectory).getParentFile());
	}

	@Test
	public void testLibsvm() throws Exception {
		System.out.println(" running test org.cleartk.example.pos.ExamplePosClassifierTest.testLibsvm()");
		String outputDirectory = baseDirectory+"/libsvm";
		
		AnalysisEngineDescription dataWriter = CleartkComponents.createViterbiDataWriterAnnotator(
				ExamplePOSAnnotationHandler.class, DefaultMultiClassLIBSVMDataWriterFactory.class,
				outputDirectory,
				DefaultMultiClassLIBSVMDataWriterFactory.PARAM_LOAD_ENCODERS_FROM_FILE_SYSTEM, false);

		testClassifier(dataWriter, outputDirectory, 1, "-t", "0"); //MultiClassLIBSVMClassifier.score is not implemented so we cannot have a stack size greater than 1.
		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		boolean badTags = firstLine.equals("2008/NN Sichuan/NN earthquake/NN From/NN Wikipedia/NN ,/NN the/NN free/NN encyclopedia/NN");
		assertFalse(badTags);
		assertEquals("2008/NN Sichuan/NN earthquake/NN From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/NN", firstLine);
		
	}

	@Test
	public void testMalletCRF() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMalletCRF()");

		if(!runLongTests) {
			System.out.println("skipping because training takes ~2 minutes. please modify the source to run this test.");
			return;
		}

		
		String outputDirectory = baseDirectory+"/malletcrf"; 
		AnalysisEngineDescription dataWriter = CleartkComponents.createSequentialDataWriterAnnotator(
				ExamplePOSAnnotationHandler.class, DefaultMalletCRFDataWriterFactory.class,
				outputDirectory);
		testClassifier(dataWriter, outputDirectory, -1); //viterbi stack size is meaningless here so pass in an invalid value to make sure it is ignored.

		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		assertEquals("2008/NN Sichuan/JJ earthquake/NNS From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/NN", firstLine);
		
	}

	@Test
	public void testMalletCRF2() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMalletCRF2()");

		if(!runLongTests) {
			System.out.println("skipping because training takes ~2 minutes.  please modify the source to run this test.");
			return;
		}

		String outputDirectory = baseDirectory+"/malletcrf-compressed"; 
		AnalysisEngineDescription dataWriter = CleartkComponents.createSequentialDataWriterAnnotator(
					ExamplePOSAnnotationHandler.class, DefaultMalletCRFDataWriterFactory.class, outputDirectory);
		AnalysisEngineFactory.setConfigurationParameters(dataWriter, DefaultMalletCRFDataWriterFactory.PARAM_COMPRESS, true);
		testClassifier(dataWriter, outputDirectory, -1); //viterbi stack size is meaningless here so pass in an invalid value to make sure it is ignored.
		
		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		assertEquals("2008/IN Sichuan/JJ earthquake/NNS From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/NN", firstLine);
	}

	@Test
	public void testMaxent() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMaxent()");<commit_msg>expected tags changed slightly for new version of mallet<commit_after>/** 
 * Copyright (c) 2009, Regents of the University of Colorado 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. 
 */

package org.cleartk.example.pos;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.File;

import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.pear.util.FileUtil;
import org.apache.uima.resource.metadata.TypeSystemDescription;
import org.cleartk.CleartkComponents;
import org.cleartk.ViewNames;
import org.cleartk.classifier.libsvm.DefaultMultiClassLIBSVMDataWriterFactory;
import org.cleartk.classifier.mallet.DefaultMalletCRFDataWriterFactory;
import org.cleartk.classifier.mallet.DefaultMalletDataWriterFactory;
import org.cleartk.classifier.opennlp.DefaultMaxentDataWriterFactory;
import org.cleartk.classifier.svmlight.DefaultOVASVMlightDataWriterFactory;
import org.cleartk.classifier.viterbi.ViterbiClassifier;
import org.cleartk.sentence.opennlp.OpenNLPSentenceSegmenter;
import org.cleartk.syntax.treebank.TreebankGoldAnnotator;
import org.cleartk.token.TokenAnnotator;
import org.cleartk.token.snowball.DefaultSnowballStemmer;
import org.cleartk.token.snowball.SnowballStemmer;
import org.cleartk.util.FilesCollectionReader;
import org.junit.After;
import org.junit.Test;
import org.uutuc.factory.AnalysisEngineFactory;
import org.uutuc.factory.CollectionReaderFactory;
import org.uutuc.util.HideOutput;
import org.uutuc.util.SimplePipeline;
import org.uutuc.util.TearDownUtil;

/**
 * <br>
 * Copyright (c) 2009, Regents of the University of Colorado <br>
 * All rights reserved.
 * 
 * @author <NAME>
 */

public class ExamplePosClassifierTest {

	private String baseDirectory = "test/data/example/pos";
	
	public static boolean runLongTests = false;
	
	@After
	public void tearDown() {
		TearDownUtil.removeDirectory(new File(baseDirectory).getParentFile());
	}

	@Test
	public void testLibsvm() throws Exception {
		System.out.println(" running test org.cleartk.example.pos.ExamplePosClassifierTest.testLibsvm()");
		String outputDirectory = baseDirectory+"/libsvm";
		
		AnalysisEngineDescription dataWriter = CleartkComponents.createViterbiDataWriterAnnotator(
				ExamplePOSAnnotationHandler.class, DefaultMultiClassLIBSVMDataWriterFactory.class,
				outputDirectory,
				DefaultMultiClassLIBSVMDataWriterFactory.PARAM_LOAD_ENCODERS_FROM_FILE_SYSTEM, false);

		testClassifier(dataWriter, outputDirectory, 1, "-t", "0"); //MultiClassLIBSVMClassifier.score is not implemented so we cannot have a stack size greater than 1.
		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		boolean badTags = firstLine.equals("2008/NN Sichuan/NN earthquake/NN From/NN Wikipedia/NN ,/NN the/NN free/NN encyclopedia/NN");
		assertFalse(badTags);
		assertEquals("2008/NN Sichuan/NN earthquake/NN From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/NN", firstLine);
		
	}

	@Test
	public void testMalletCRF() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMalletCRF()");

		if(!runLongTests) {
			System.out.println("skipping because training takes ~2 minutes. please modify the source to run this test.");
			return;
		}

		
		String outputDirectory = baseDirectory+"/malletcrf"; 
		AnalysisEngineDescription dataWriter = CleartkComponents.createSequentialDataWriterAnnotator(
				ExamplePOSAnnotationHandler.class, DefaultMalletCRFDataWriterFactory.class,
				outputDirectory);
		testClassifier(dataWriter, outputDirectory, -1); //viterbi stack size is meaningless here so pass in an invalid value to make sure it is ignored.

		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		assertEquals("2008/NN Sichuan/JJ earthquake/NNS From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/IN", firstLine);
		
	}

	@Test
	public void testMalletCRF2() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMalletCRF2()");

		if(!runLongTests) {
			System.out.println("skipping because training takes ~2 minutes.  please modify the source to run this test.");
			return;
		}

		String outputDirectory = baseDirectory+"/malletcrf-compressed"; 
		AnalysisEngineDescription dataWriter = CleartkComponents.createSequentialDataWriterAnnotator(
					ExamplePOSAnnotationHandler.class, DefaultMalletCRFDataWriterFactory.class, outputDirectory);
		AnalysisEngineFactory.setConfigurationParameters(dataWriter, DefaultMalletCRFDataWriterFactory.PARAM_COMPRESS, true);
		testClassifier(dataWriter, outputDirectory, -1); //viterbi stack size is meaningless here so pass in an invalid value to make sure it is ignored.
		
		String firstLine = FileUtil.loadListOfStrings(new File(outputDirectory + "/2008_Sichuan_earthquake.txt.pos"))[0].trim();
		assertEquals("2008/IN Sichuan/JJ earthquake/NNS From/IN Wikipedia/NN ,/, the/DT free/NN encyclopedia/IN", firstLine);
	}

	@Test
	public void testMaxent() throws Exception {
		System.out.println("running test org.cleartk.example.pos.ExamplePosClassifierTest.testMaxent()");
{'fraction_non_alphanumeric': 0.08332710000747999, 'fraction_numerical': 0.00718079138305034, 'mean_word_length': 5.595954612728169, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4744065', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3701, 'n_words': 1083}