001/** 002 * Copyright (c) 2007-2008, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * 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. 010 * 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. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.ml.util; 025 026import java.util.ArrayList; 027import java.util.List; 028 029import org.apache.uima.analysis_engine.AnalysisEngine; 030import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 031import org.apache.uima.jcas.JCas; 032import org.cleartk.ml.DataWriter; 033import org.cleartk.ml.DataWriterFactory; 034import org.cleartk.ml.Instance; 035 036/** 037 * A simple {@link DataWriter} that stores all instances in a public attribute. Intended only for 038 * testing. 039 * 040 * <br> 041 * Copyright (c) 2007-2008, Regents of the University of Colorado <br> 042 * All rights reserved. 043 * 044 * @author Philip Ogren 045 * @author Steven Bethard 046 */ 047public class PublicFieldDataWriter<T> implements DataWriter<T> { 048 049 public List<Instance<T>> instances; 050 051 public PublicFieldDataWriter() { 052 this.instances = new ArrayList<Instance<T>>(); 053 } 054 055 public void finish() { 056 } 057 058 public void write(Instance<T> instance) { 059 this.instances.add(instance); 060 } 061 062 /** 063 * Returns a single static instance of InstanceCollector<String>. 064 */ 065 public static class StringFactory implements DataWriterFactory<String> { 066 private static PublicFieldDataWriter<String> collector = new PublicFieldDataWriter<String>(); 067 068 public DataWriter<String> createDataWriter() { 069 return collector; 070 } 071 072 public static List<Instance<String>> collectInstances(AnalysisEngine engine, JCas jCas) 073 throws AnalysisEngineProcessException { 074 return PublicFieldDataWriter.collectInstances(engine, jCas, collector); 075 } 076 } 077 078 /** 079 * Returns a single static instance of InstanceCollector<String>. 080 */ 081 public static class BooleanFactory implements DataWriterFactory<Boolean> { 082 private static PublicFieldDataWriter<Boolean> collector = new PublicFieldDataWriter<Boolean>(); 083 084 public DataWriter<Boolean> createDataWriter() { 085 return collector; 086 } 087 088 public static List<Instance<Boolean>> collectInstances(AnalysisEngine engine, JCas jCas) 089 throws AnalysisEngineProcessException { 090 return PublicFieldDataWriter.collectInstances(engine, jCas, collector); 091 } 092 } 093 094 static <T> List<Instance<T>> collectInstances( 095 AnalysisEngine engine, 096 JCas jCas, 097 PublicFieldDataWriter<T> collector) throws AnalysisEngineProcessException { 098 collector.instances.clear(); 099 engine.process(jCas); 100 engine.collectionProcessComplete(); 101 return collector.instances; 102 } 103}