001/** 002 * Copyright (c) 2012, 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 */ 024 025package org.cleartk.ml.feature.transform; 026 027import java.io.IOException; 028import java.net.URI; 029 030import org.cleartk.ml.Instance; 031 032/** 033 * Copyright (c) 2012, Regents of the University of Colorado <br> 034 * All rights reserved. 035 * <p> 036 * TrainableExtractors defines a extractors that can be trained to fix up data based on a set of 037 * instances. Prototypical cases include computing statistics for normalization such as mean, 038 * standard deviation, min, max, or for computing corpus tf*idf values 039 * 040 * <p> 041 * TrainableExtractors that have not yet been trained cannot be used as subextractors inside of any 042 * other feature extractor, though they can have subextractors of their own. So for example, while 043 * the following will work: 044 * <p> 045 * 046 * <code> 047 * new TfidfExtractor(new ContextExtractor<Token>(Token.class, new CoveredTextExtractor(), new 048 * Preceding(2))) 049 * </code> 050 * <p> 051 * the following will not: 052 * <p> 053 * <code> 054 * new ContextExtractor<Token>(Token.class, new TfidfExtractor(new CoveredTextExtractor()), new 055 * Preceding(2)) 056 * </code> 057 */ 058public interface TrainableExtractor<OUTCOME_T> { 059 060 /** 061 * In the prototypical case, train takes a collection of instances and computes statistics over 062 * the values such as computing mean, standard deviation, TF*IDF, etc... 063 * 064 * @param instances 065 * - URI pointing to the output location for saving statistics 066 */ 067 public void train(Iterable<Instance<OUTCOME_T>> instances); 068 069 /** 070 * Saves statistics from train in location URI 071 */ 072 public void save(URI uri) throws IOException; 073 074 /** 075 * Loads statistics from location URI 076 */ 077 public void load(URI uri) throws IOException; 078 079 /** 080 * Transforms all features handled by this extractor. Called on an Instance that was created 081 * before {@link #train(Iterable)} was called, to complete the processing of the Instance. 082 * 083 * @param instance 084 * An instance that was created before {@link #train(Iterable)} was called. 085 * @return A copy of the instance, where processing of the instances is complete. 086 */ 087 public Instance<OUTCOME_T> transform(Instance<OUTCOME_T> instance); 088 089}