    001    package calhoun.analysis.crf.io;
    002
    003    import java.io.BufferedReader;
    004    import java.io.IOException;
    005    import java.io.Writer;
    006    import java.util.List;
    007    import java.util.Map;
    008
    009    import calhoun.util.Assert;
    010
    011    /** reads in an input consisting of a string.  Can be used as a standalone
    012     * input component or part of an interleaved input.
    013     */
    014    public class StringInput extends InterleavedInputComponentBase  {
    015            private static final long serialVersionUID = -2944973705715162476L;
    016
    017            public boolean read(BufferedReader r, Map<String, InputSequence<?>> output) throws IOException {
    018                    String data = r.readLine();
    019                    if(data == null)
    020                            return false;
    021                    output.put(name, new InputSequenceCharacter(data));
    022                    return true;
    023            }
    024
    025            public void write(Writer w, Map<String, ? extends InputSequence<?>> data) throws IOException {
    026                    InputSequenceCharacter comp = (InputSequenceCharacter) data.get(name);
    027                    Assert.a(comp != null, "No component found in input: "+name+".  Entry is "+data.keySet().iterator().next());
    028                    w.write(comp.getString());
    029                    w.write('\n');
    030            }
    031
    032            /** Convenience function for creating training sequences in test data. */
    033            public static List<? extends TrainingSequence<Character>> prepareData(String str) {
    034                    try {
    035                            return (List<? extends TrainingSequence<Character>>) new InputHandlerInterleaved(new StringInput(), true).readTrainingData(str) ;
    036                    }
    037                    catch(Exception ex) {
    038                            throw new RuntimeException(ex);
    039                    }
    040            }
    041    }