You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by co...@apache.org on 2011/01/05 18:32:12 UTC

svn commit: r1055544 - in /incubator/opennlp/trunk/opennlp-tools/src: main/java/opennlp/tools/chunker/ main/java/opennlp/tools/cmdline/ main/java/opennlp/tools/cmdline/chunker/ test/java/opennlp/tools/chunker/ test/resources/opennlp/tools/chunker/

Author: colen
Date: Wed Jan  5 17:32:11 2011
New Revision: 1055544

URL: http://svn.apache.org/viewvc?rev=1055544&view=rev
Log:
OPENNLP-30: Added code and test for Chunker Evaluator

Added:
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java   (with props)
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java   (with props)
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java   (with props)
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java   (with props)
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java   (with props)
    incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/
    incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt   (with props)
Modified:
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.java
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java
    incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkSampleTest.java

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.java?rev=1055544&r1=1055543&r2=1055544&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.java Wed Jan  5 17:32:11 2011
@@ -22,6 +22,8 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
+import opennlp.tools.util.Span;
+
 public class ChunkSample {
   private final List<String> sentence;
 
@@ -58,6 +60,36 @@ public class ChunkSample {
     return preds.toArray(new String[preds.size()]);
   }
   
+  public Span[] getPhrasesAsSpanList() {
+	  List<Span> phrases =  new ArrayList<Span>();
+	  String startTag = "";
+	  int startIndex = 0;
+	  boolean foundPhrase = false;
+	    
+	    for (int ci=0, cn = preds.size(); ci < cn; ci++) {
+	    	String pred = preds.get(ci);
+	    	if( pred.startsWith("B-") || ( !pred.equals("I-" + startTag) && !pred.equals("O") )) { // start
+	    		if(foundPhrase) { // handle the last
+	    			phrases.add(new Span(startIndex, ci, startTag));
+	    		}
+	    		startIndex = ci;
+	    		startTag = pred.substring(2);
+	    		foundPhrase = true;
+	    	} else if(pred.equals("I-" + startTag)) { // middle 
+	    		// do nothing
+	    	} else if(foundPhrase) {// end
+	    		phrases.add(new Span(startIndex, ci, startTag));
+	    		foundPhrase = false;
+	    		startTag = "";
+	    	}
+	    }
+	    if(foundPhrase) { // leftover
+	    	phrases.add(new Span(startIndex, preds.size(), startTag));
+	    }
+	    
+	    return phrases.toArray(new Span[phrases.size()]);
+  }
+  
   @Override
   public String toString() {
     

Added: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java Wed Jan  5 17:32:11 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package opennlp.tools.chunker;
+
+import opennlp.tools.util.eval.Evaluator;
+import opennlp.tools.util.eval.FMeasure;
+
+/**
+ * The {@link ChunkerEvaluator} measures the performance
+ * of the given {@link Chunker} with the provided
+ * reference {@link ChunkSample}s.
+ *
+ * @see Evaluator
+ * @see Chunker
+ * @see ChunkSample
+ */
+public class ChunkerEvaluator extends Evaluator<ChunkSample> {
+
+  private FMeasure fmeasure = new FMeasure();
+  
+  /**
+   * The {@link Chunker} used to create the predicted
+   * {@link ChunkSample} objects.
+   */
+  private Chunker chunker;
+
+  /**
+   * Initializes the current instance with the given
+   * {@link Chunker}.
+   *
+   * @param chunker the {@link Chunker} to evaluate.
+   */
+  public ChunkerEvaluator(Chunker chunker) {
+    this.chunker = chunker;
+  }
+
+  /**
+   * Evaluates the given reference {@link ChunkSample} object.
+   *
+   * This is done by finding the phrases with the
+   * {@link Chunker} in the sentence from the reference
+   * {@link ChunkSample}. The found phrases are then used to
+   * calculate and update the scores.
+   *
+   * @param reference the reference {@link ChunkSample}.
+   */
+  public void evaluateSample(ChunkSample reference) {
+	  
+	String[] preds = chunker.chunk(reference.getSentence(), reference.getTags());
+	ChunkSample result = new ChunkSample(reference.getSentence(), reference.getTags(), preds);
+
+    fmeasure.updateScores(reference.getPhrasesAsSpanList(), result.getPhrasesAsSpanList());
+  }
+  
+  public FMeasure getFMeasure() {
+    return fmeasure;
+  }
+
+}

Propchange: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java?rev=1055544&r1=1055543&r2=1055544&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java Wed Jan  5 17:32:11 2011
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import opennlp.tools.cmdline.chunker.ChunkerEvaluatorTool;
 import opennlp.tools.cmdline.chunker.ChunkerMETool;
 import opennlp.tools.cmdline.chunker.ChunkerTrainerTool;
 import opennlp.tools.cmdline.namefind.CensusDictionaryCreatorTool;
@@ -100,6 +101,7 @@ public final class CLI {
     // Chunker
     tools.add(new ChunkerMETool());
     tools.add(new ChunkerTrainerTool());
+    tools.add(new ChunkerEvaluatorTool());
     
     // Parser
     tools.add(new ParserTool());

Added: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java Wed Jan  5 17:32:11 2011
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.cmdline.chunker;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import opennlp.tools.chunker.ChunkSample;
+import opennlp.tools.chunker.ChunkerEvaluator;
+import opennlp.tools.chunker.ChunkerME;
+import opennlp.tools.chunker.ChunkerModel;
+import opennlp.tools.cmdline.ArgumentParser;
+import opennlp.tools.cmdline.ArgumentParser.OptionalParameter;
+import opennlp.tools.cmdline.ArgumentParser.ParameterDescription;
+import opennlp.tools.cmdline.CLI;
+import opennlp.tools.cmdline.CmdLineTool;
+import opennlp.tools.cmdline.CmdLineUtil;
+import opennlp.tools.cmdline.PerformanceMonitor;
+import opennlp.tools.cmdline.TerminateToolException;
+import opennlp.tools.util.ObjectStream;
+
+public final class ChunkerEvaluatorTool implements CmdLineTool {
+	
+  /**
+   * Create a list of expected parameters.
+   */
+  interface Parameters {
+    
+    @ParameterDescription(valueName = "charsetName")
+    @OptionalParameter(defaultValue="UTF-8")
+    String getEncoding();
+    
+    @ParameterDescription(valueName = "model")
+    String getModel();
+    
+    @ParameterDescription(valueName = "data")
+    String getData();
+  }
+
+  public String getName() {
+    return "ChunkerEvaluator";
+  }
+
+  public String getShortDescription() {
+    return "Measures the performance of the Chunker model with the reference data";
+  }
+
+  public String getHelp() {
+    return "Usage: " + CLI.CMD + " " + getName() + " " + ArgumentParser.createUsage(Parameters.class);
+  }
+
+  public void run(String[] args) {
+
+  	if (!ArgumentParser.validateArguments(args, Parameters.class)) {
+      System.err.println(getHelp());
+      throw new TerminateToolException(1);
+    }
+  	
+  	Parameters params = ArgumentParser.parse(args, Parameters.class);
+  	
+  	File testData = new File(params.getData());
+
+    CmdLineUtil.checkInputFile("Test data", testData);
+
+    Charset encoding = Charset.forName(params.getEncoding());
+
+    if (encoding == null) {
+      System.out.println(getHelp());
+      throw new TerminateToolException(1);
+    }
+
+    ChunkerModel model = new ChunkerModelLoader().load(new File(params.getModel()));
+
+    ChunkerEvaluator evaluator = new ChunkerEvaluator(new ChunkerME(model));
+    
+    final ObjectStream<ChunkSample> sampleStream = ChunkerTrainerTool.openSampleData("Test",
+        testData, encoding);
+
+    final PerformanceMonitor monitor = new PerformanceMonitor("sent");
+
+    ObjectStream<ChunkSample> measuredSampleStream = new ObjectStream<ChunkSample>() {
+
+      public ChunkSample read() throws IOException {
+        monitor.incrementCounter();
+        return sampleStream.read();
+      }
+
+      public void reset() throws IOException {
+        sampleStream.reset();
+      }
+
+      public void close() throws IOException {
+        sampleStream.close();
+      }
+    };
+
+    monitor.startAndPrintThroughput();
+
+    try {
+      evaluator.evaluate(measuredSampleStream);
+    } catch (IOException e) {
+      System.err.println("failed");
+      System.err.println("Reading test data error " + e.getMessage());
+      throw new TerminateToolException(-1);
+    } finally {
+      try {
+        measuredSampleStream.close();
+      } catch (IOException e) {
+        // sorry that this can fail
+      }
+    }
+
+    monitor.stopAndPrintFinalResult();
+
+    System.out.println();
+
+    System.out.println(evaluator.getFMeasure());
+  }
+}

Propchange: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkSampleTest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkSampleTest.java?rev=1055544&r1=1055543&r2=1055544&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkSampleTest.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkSampleTest.java Wed Jan  5 17:32:11 2011
@@ -20,6 +20,13 @@ package opennlp.tools.chunker;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import opennlp.tools.util.PlainTextByLineStream;
+import opennlp.tools.util.Span;
+
 import org.junit.Test;
 
 public class ChunkSampleTest {
@@ -87,4 +94,49 @@ public class ChunkSampleTest {
     assertEquals(" [NP Forecasts_NNS ] [PP for_IN ] [NP the_DT trade_NN figures_NNS ] " +
     		"[VP range_VBP ] [ADVP widely_RB ] ._.", sample.toString());
   }
+	
+  @Test
+  public void testAsSpan() {
+	ChunkSample sample = new ChunkSample(createSentence(), createTags(),
+			createChunks());
+	Span[] spans = sample.getPhrasesAsSpanList();
+
+	assertEquals(5, spans.length);
+	assertEquals(new Span(0, 1, "NP"), spans[0]);
+	assertEquals(new Span(1, 2, "PP"), spans[1]);
+	assertEquals(new Span(2, 5, "NP"), spans[2]);
+	assertEquals(new Span(5, 6, "VP"), spans[3]);
+	assertEquals(new Span(6, 7, "ADVP"), spans[4]);
+  }
+
+  @Test
+  public void testRegions() throws IOException {
+	InputStream in = getClass().getClassLoader()
+			.getResourceAsStream("opennlp/tools/chunker/output.txt");
+
+	String encoding = "UTF-8";
+
+	DummyChunkSampleStream predictedSample = new DummyChunkSampleStream(
+			new PlainTextByLineStream(new InputStreamReader(in,
+					encoding)), false);
+
+	ChunkSample cs1 = predictedSample.read();
+	String[] g1 = Span.spansToStrings(cs1.getPhrasesAsSpanList(), cs1.getSentence());
+	assertEquals(15, g1.length);
+	
+	ChunkSample cs2 = predictedSample.read();
+	String[] g2 = Span.spansToStrings(cs2.getPhrasesAsSpanList(), cs2.getSentence());
+	assertEquals(10, g2.length);
+	
+	ChunkSample cs3 = predictedSample.read();
+	String[] g3 = Span.spansToStrings(cs3.getPhrasesAsSpanList(), cs3.getSentence());
+	assertEquals(7, g3.length);
+	assertEquals("United", g3[0]);
+	assertEquals("'s directors", g3[1]);
+	assertEquals("voted", g3[2]);
+	assertEquals("themselves", g3[3]);
+	assertEquals("their spouses", g3[4]);
+	assertEquals("lifetime access", g3[5]);
+	assertEquals("to", g3[6]);
+	}
 }

Added: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java Wed Jan  5 17:32:11 2011
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.chunker;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import opennlp.tools.util.PlainTextByLineStream;
+import opennlp.tools.util.eval.FMeasure;
+
+import org.junit.Test;
+
+/**
+ * Tests for {@link ChunkerEvaluator}.
+ * 
+ * @see ChunkerEvaluator
+ */
+public class ChunkerEvaluatorTest {
+	
+	private static final double DELTA = 1.0E-9d;
+
+	/**
+	 * Checks the evaluator results against the results got using the conlleval,
+	 * available at http://www.cnts.ua.ac.be/conll2000/chunking/output.html
+	 * The output.txt file has only 3 sentences, but can be replaced by the one
+	 * available at the conll2000 site to validate using a bigger sample.
+	 * @throws IOException
+	 */
+	@Test
+	public void testEvaluator() throws IOException {
+		InputStream inPredicted = getClass().getClassLoader().getResourceAsStream(
+				"opennlp/tools/chunker/output.txt");
+		InputStream inExpected = getClass().getClassLoader().getResourceAsStream(
+		"opennlp/tools/chunker/output.txt");
+
+		String encoding = "UTF-8";
+
+		DummyChunkSampleStream predictedSample = new DummyChunkSampleStream(
+				new PlainTextByLineStream(new InputStreamReader(inPredicted, encoding)), true);
+		
+		DummyChunkSampleStream expectedSample = new DummyChunkSampleStream(
+				new PlainTextByLineStream(new InputStreamReader(inExpected)), false);
+		
+		Chunker dummyChunker = new DummyChunker(predictedSample);
+		ChunkerEvaluator evaluator = new ChunkerEvaluator(dummyChunker);
+		
+		evaluator.evaluate(expectedSample);
+		
+		FMeasure fm = evaluator.getFMeasure();
+		
+		assertEquals(0.8d, fm.getPrecisionScore(), DELTA);
+		assertEquals(0.875d, fm.getRecallScore(), DELTA);
+	}
+
+}

Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java Wed Jan  5 17:32:11 2011
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.chunker;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import opennlp.tools.util.FilterObjectStream;
+import opennlp.tools.util.ObjectStream;
+
+/**
+ * This dummy chunk sample stream reads a file formatted as described at
+ * <a hraf="http://www.cnts.ua.ac.be/conll2000/chunking/output.html/">] and
+ * can be used together with DummyChunker simulate a chunker.
+ */
+public class DummyChunkSampleStream extends
+		FilterObjectStream<String, ChunkSample> {
+
+	boolean mIsPredicted;
+	int count = 0;
+
+	// the predicted flag sets if the stream will contain the expected or the
+	// predicted tags.
+	public DummyChunkSampleStream(ObjectStream<String> samples,
+			boolean isPredicted) {
+		super(samples);
+		mIsPredicted = isPredicted;
+	}
+
+	/**
+	 * Returns a pair representing the expected and the predicted at 0: the
+	 * chunk tag according to the corpus at 1: the chunk tag predicted
+	 * 
+	 * @see opennlp.tools.util.ObjectStream#read()
+	 */
+	public ChunkSample read() throws IOException {
+
+		List<String> toks = new ArrayList<String>();
+		List<String> posTags = new ArrayList<String>();
+		List<String> chunkTags = new ArrayList<String>();
+		List<String> predictedChunkTags = new ArrayList<String>();
+
+		for (String line = samples.read(); line != null && !line.equals(""); line = samples
+				.read()) {
+			String[] parts = line.split(" ");
+			if (parts.length != 4) {
+				System.err.println("Skipping corrupt line " + count + ": "
+						+ line);
+			} else {
+				toks.add(parts[0]);
+				posTags.add(parts[1]);
+				chunkTags.add(parts[2]);
+				predictedChunkTags.add(parts[3]);
+			}
+			count++;
+		}
+
+		if (toks.size() > 0) {
+			if (mIsPredicted) {
+				return new ChunkSample(toks.toArray(new String[toks.size()]),
+						posTags.toArray(new String[posTags.size()]),
+						predictedChunkTags
+								.toArray(new String[predictedChunkTags.size()]));
+			} else
+				return new ChunkSample(toks.toArray(new String[toks.size()]),
+						posTags.toArray(new String[posTags.size()]),
+						chunkTags.toArray(new String[chunkTags.size()]));
+		} else {
+			return null;
+		}
+
+	}
+
+}

Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java Wed Jan  5 17:32:11 2011
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.chunker;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import opennlp.tools.util.Sequence;
+
+/**
+ * This dummy chunker implementation reads a file formatted as described at
+ * <a hraf="http://www.cnts.ua.ac.be/conll2000/chunking/output.html/">] to
+ * simulate a Chunker. The file has samples of sentences, with target and
+ * predicted values.
+ */
+public class DummyChunker implements Chunker {
+
+	private DummyChunkSampleStream mSampleStream;
+
+	public DummyChunker(DummyChunkSampleStream aSampleStream) {
+		mSampleStream = aSampleStream;
+	}
+
+	public List<String> chunk(List<String> toks, List<String> tags) {
+		return Arrays.asList(chunk(toks.toArray(new String[toks.size()]),
+				tags.toArray(new String[tags.size()])));
+	}
+
+	public String[] chunk(String[] toks, String[] tags) {
+		try {
+			ChunkSample predsSample = mSampleStream.read();
+
+			// checks if the streams are sync
+			for (int i = 0; i < toks.length; i++) {
+				if (!toks[i].equals(predsSample.getSentence()[i])
+						|| !tags[i].equals(predsSample.getTags()[i])) {
+					throw new RuntimeException("The streams are not sync!"
+							+ "\n expected sentence: " + Arrays.toString(toks)
+							+ "\n expected tags: " + Arrays.toString(tags)
+							+ "\n predicted sentence: "
+							+ Arrays.toString(predsSample.getSentence())
+							+ "\n predicted tags: "
+							+ Arrays.toString(predsSample.getTags()));
+				}
+			}
+
+			return predsSample.getPreds();
+
+		} catch (IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public Sequence[] topKSequences(List<String> sentence, List<String> tags) {
+		return null;
+	}
+
+	public Sequence[] topKSequences(String[] sentence, String[] tags,
+			double minSequenceScore) {
+		return null;
+	}
+
+}

Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt?rev=1055544&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt Wed Jan  5 17:32:11 2011
@@ -0,0 +1,60 @@
+Rockwell NNP B-NP I-NP
+International NNP I-NP I-NP
+Corp. NNP I-NP I-NP
+'s POS B-NP B-NP
+Tulsa NNP I-NP I-NP
+unit NN I-NP I-NP
+said VBD B-VP B-VP
+it PRP B-NP B-NP
+signed VBD B-VP B-VP
+a DT B-NP B-NP
+tentative JJ I-NP I-NP
+agreement NN I-NP I-NP
+extending VBG B-VP B-VP
+its PRP$ B-NP B-NP
+contract NN I-NP I-NP
+with IN B-PP B-PP
+Boeing NNP B-NP I-NP
+Co. NNP I-NP I-NP
+to TO B-VP B-PP
+provide VB I-VP I-VP
+structural JJ B-NP I-NP
+parts NNS I-NP I-NP
+for IN B-PP B-PP
+Boeing NNP B-NP I-NP
+'s POS B-NP B-NP
+747 CD I-NP I-NP
+jetliners NNS I-NP I-NP
+. . O O
+
+Rockwell NNP B-NP I-NP
+said VBD B-VP B-VP
+the DT B-NP B-NP
+agreement NN I-NP I-NP
+calls VBZ B-VP B-VP
+for IN B-SBAR B-PP
+it PRP B-NP B-NP
+to TO B-VP B-PP
+supply VB I-VP I-VP
+200 CD B-NP I-NP
+additional JJ I-NP B-NP
+so-called JJ I-NP I-NP
+shipsets NNS I-NP I-NP
+for IN B-PP B-PP
+the DT B-NP B-NP
+planes NNS I-NP I-NP
+. . O O
+
+United NNP B-NP I-NP
+'s POS B-NP B-NP
+directors NNS I-NP I-NP
+voted VBD B-VP B-VP
+themselves PRP B-NP B-NP
+, , O O
+and CC O O
+their PRP$ B-NP B-NP
+spouses NNS I-NP I-NP
+, , O O
+lifetime NN B-NP I-NP
+access NN I-NP I-NP
+to TO B-PP B-PP

Propchange: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/chunker/output.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain