    001    package calhoun.analysis.crf.io;
    002
    003    import java.io.IOException;
    004    import java.util.ArrayList;
    005    import java.util.Iterator;
    006    import java.util.List;
    007    import java.util.Map;
    008
    009    /** Utility class to abstract out some of the common behavior for {@link InputHandler}s.
    010     */
    011    public abstract class InputHandlerBase implements InputHandler {
    012            private static final long serialVersionUID = 4002620265503458109L;
    013
    014            Iterator<? extends InputSequence<?>> createCompositeInput(List<Map<String, InputSequence<?>>> inputs) {
    015                    // Create a composite input
    016                    List<InputSequenceComposite> ret = new ArrayList();
    017                    for(Map<String, InputSequence<?>> input : inputs) {
    018                            ret.add(new InputSequenceComposite(input));
    019                    }
    020                    return ret.iterator();
    021
    022            }
    023
    024            List<? extends TrainingSequence<?>> readTrainingData(String location, String trainingLocation, TrainingSequenceIO hiddenStateReader, boolean predict) throws IOException {
    025                    Iterator<? extends InputSequence<Map<String, Object>>> inputIter = (Iterator<? extends InputSequence<Map<String, Object>>>) readInputData(location);
    026
    027                    List<TrainingSequence<Map<String, Object>>> ret = new ArrayList<TrainingSequence<Map<String, Object>>>();
    028                    while(inputIter.hasNext()) {
    029                            ret.add(new TrainingSequence(inputIter.next()));
    030                    }
    031
    032                    TrainingSequenceIO readerToUse;
    033                    if (predict) {
    034                            readerToUse = new AllIntergenicHiddenStateReader();
    035                    } else {
    036                            readerToUse = hiddenStateReader;
    037                    }
    038
    039                    readerToUse.readTrainingSequences(trainingLocation, ret);
    040                    return ret;
    041            }
    042
    043    }