ChunkSampleStream extends FilterObjectStream<String, ChunkSample> { public ChunkSample read() throws IOException { List<String> toks = new ArrayList<String>(); List<String> tags = new ArrayList<String>(); List<String> preds = new ArrayList<String>(); for (String line = samples.read(); line !=null && !line.equals(""); line = samples.read()) { String[] parts = line.split(" "); if (parts.length != 3) { System.err.println("Skipping corrupt line: "+line); } else { toks.add(parts[0]); tags.add(parts[1]); preds.add(parts[2]); } } if (toks.size() > 0) { return new ChunkSample(toks.toArray(new String[toks.size()]), tags.toArray(new String[tags.size()]), preds.toArray(new String[preds.size()])); } else { return null; } } ChunkSampleStream(ObjectStream<String> samples); ChunkSample read();  }
@Test public void testReadingEvents() throws IOException { StringBuilder sample = new StringBuilder(); sample.append("word11 tag11 pred11"); sample.append('\n'); sample.append("word12 tag12 pred12"); sample.append('\n'); sample.append("word13 tag13 pred13"); sample.append('\n'); sample.append('\n'); sample.append("word21 tag21 pred21"); sample.append('\n'); sample.append("word22 tag22 pred22"); sample.append('\n'); sample.append("word23 tag23 pred23"); sample.append('\n'); ObjectStream<String> stringStream = new PlainTextByLineStream(new StringReader(sample.toString())); ObjectStream<ChunkSample> chunkStream = new ChunkSampleStream(stringStream); ChunkSample firstSample = chunkStream.read(); assertEquals("word11", firstSample.getSentence()[0]); assertEquals("tag11", firstSample.getTags()[0]); assertEquals("pred11", firstSample.getPreds()[0]); assertEquals("word12", firstSample.getSentence()[1]); assertEquals("tag12", firstSample.getTags()[1]); assertEquals("pred12", firstSample.getPreds()[1]); assertEquals("word13", firstSample.getSentence()[2]); assertEquals("tag13", firstSample.getTags()[2]); assertEquals("pred13", firstSample.getPreds()[2]); ChunkSample secondSample = chunkStream.read(); assertEquals("word21", secondSample.getSentence()[0]); assertEquals("tag21", secondSample.getTags()[0]); assertEquals("pred21", secondSample.getPreds()[0]); assertEquals("word22", secondSample.getSentence()[1]); assertEquals("tag22", secondSample.getTags()[1]); assertEquals("pred22", secondSample.getPreds()[1]); assertEquals("word23", secondSample.getSentence()[2]); assertEquals("tag23", secondSample.getTags()[2]); assertEquals("pred23", secondSample.getPreds()[2]); assertNull(chunkStream.read()); }