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/28 13:24:21 UTC

svn commit: r1064640 - in /incubator/opennlp/trunk/opennlp-tools/src: main/java/opennlp/tools/chunker/ChunkSample.java test/java/opennlp/tools/chunker/ChunkSampleTest.java

Author: colen
Date: Fri Jan 28 12:24:21 2011
New Revision: 1064640

URL: http://svn.apache.org/viewvc?rev=1064640&view=rev
Log:
OPENNLP-62 Improved ChunkSample.phrasesAsSpanList method and added more tests

Modified:
    incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkSample.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=1064640&r1=1064639&r2=1064640&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 Fri Jan 28 12:24:21 2011
@@ -45,8 +45,7 @@ public class ChunkSample {
    */
   public ChunkSample(String[] sentence, String[] tags, String[] preds) {
     
-    if (sentence.length != tags.length || tags.length != preds.length)
-      throw new IllegalArgumentException("All arrays must have the same length!");
+    validateArguments(sentence.length, tags.length, preds.length);
     
     this.sentence = Collections.unmodifiableList(new ArrayList<String>(Arrays.asList(sentence)));
     this.tags = Collections.unmodifiableList(new ArrayList<String>(Arrays.asList(tags)));
@@ -64,9 +63,9 @@ public class ChunkSample {
    *          Chunk tags in B-* I-* notation
    */
   public ChunkSample(List<String> sentence, List<String> tags, List<String> preds) {
-  	if (sentence.size() != tags.size()  || tags.size()  != preds.size() )
-      throw new IllegalArgumentException("All arrays must have the same length!");
-  	
+    
+    validateArguments(sentence.size(), tags.size(), preds.size());
+    
     this.sentence = Collections.unmodifiableList(new ArrayList<String>((sentence)));
     this.tags = Collections.unmodifiableList(new ArrayList<String>((tags)));
     this.preds = Collections.unmodifiableList(new ArrayList<String>((preds)));
@@ -107,11 +106,10 @@ public class ChunkSample {
   public static Span[] phrasesAsSpanList(String[] aSentence, String[] aTags,
       String[] aPreds) {
 
-    if (aSentence.length != aTags.length || aTags.length != aPreds.length)
-      throw new IllegalArgumentException(
-          "All arrays must have the same length!");
+    validateArguments(aSentence.length, aTags.length, aPreds.length);
 
-    List<Span> phrases = new ArrayList<Span>();
+    // initialize with the list maximum size
+    List<Span> phrases = new ArrayList<Span>(aSentence.length); 
     String startTag = "";
     int startIndex = 0;
     boolean foundPhrase = false;
@@ -141,6 +139,12 @@ public class ChunkSample {
     return phrases.toArray(new Span[phrases.size()]);
   }
   
+  private static void validateArguments(int sentenceSize, int tagsSize, int predsSize) throws IllegalArgumentException {
+    if (sentenceSize != tagsSize || tagsSize != predsSize)
+      throw new IllegalArgumentException(
+          "All arrays must have the same length!");
+  }
+  
   /**
    * Creates a nice to read string for the phrases formatted as following: <br>
    * <code>

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=1064640&r1=1064639&r2=1064640&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 Fri Jan 28 12:24:21 2011
@@ -25,6 +25,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringReader;
+import java.util.Arrays;
 
 import opennlp.tools.util.PlainTextByLineStream;
 import opennlp.tools.util.Span;
@@ -48,6 +49,14 @@ public class ChunkSampleTest {
         "figures",
         "range",
         "widely",
+        ",",
+        "Forecasts",
+        "for",
+        "the",
+        "trade",
+        "figures",
+        "range",
+        "widely",
         "."
     };
   }
@@ -62,6 +71,14 @@ public class ChunkSampleTest {
         "NNS",
         "VBP",
         "RB",
+        ",",
+        "NNS",
+        "IN",
+        "DT",
+        "NN",
+        "NNS",
+        "VBP",
+        "RB",
         "."
     };
   }
@@ -74,8 +91,16 @@ public class ChunkSampleTest {
         "I-NP",
         "I-NP",
         "B-VP",
-        "B-ADVP"
-        ,"O"
+        "B-ADVP",
+        "O",
+        "B-NP",
+        "B-PP",
+        "B-NP",
+        "I-NP",
+        "I-NP",
+        "B-VP",
+        "B-ADVP",
+        "O"
     };
   }
   
@@ -114,7 +139,8 @@ public class ChunkSampleTest {
     ChunkSample sample = new ChunkSample(createSentence(), createTags(), createChunks());
     
     assertEquals(" [NP Forecasts_NNS ] [PP for_IN ] [NP the_DT trade_NN figures_NNS ] " +
-    		"[VP range_VBP ] [ADVP widely_RB ] ._.", sample.nicePrint());
+    		"[VP range_VBP ] [ADVP widely_RB ] ,_, [NP Forecasts_NNS ] [PP for_IN ] [NP the_DT trade_NN figures_NNS ] " +
+            "[VP range_VBP ] [ADVP widely_RB ] ._.", sample.nicePrint());
   }
 	
   @Test
@@ -123,12 +149,17 @@ public class ChunkSampleTest {
 			createChunks());
 	Span[] spans = sample.getPhrasesAsSpanList();
 
-	assertEquals(5, spans.length);
+	assertEquals(10, 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]);
+	assertEquals(new Span(8, 9, "NP"), spans[5]);
+    assertEquals(new Span(9, 10, "PP"), spans[6]);
+    assertEquals(new Span(10, 13, "NP"), spans[7]);
+    assertEquals(new Span(13, 14, "VP"), spans[8]);
+    assertEquals(new Span(14, 15, "ADVP"), spans[9]);
   }
   
 	@Test
@@ -136,12 +167,17 @@ public class ChunkSampleTest {
 		Span[] spans = ChunkSample.phrasesAsSpanList(createSentence(),
 				createTags(), createChunks());
 
-		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]);
+	    assertEquals(10, 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]);
+	    assertEquals(new Span(8, 9, "NP"), spans[5]);
+	    assertEquals(new Span(9, 10, "PP"), spans[6]);
+	    assertEquals(new Span(10, 13, "NP"), spans[7]);
+	    assertEquals(new Span(13, 14, "VP"), spans[8]);
+	    assertEquals(new Span(14, 15, "ADVP"), spans[9]);
 	}
 
   @Test
@@ -174,4 +210,35 @@ public class ChunkSampleTest {
 	assertEquals("lifetime access", g3[5]);
 	assertEquals("to", g3[6]);
 	}
+  
+
+  // following are some tests to check the argument validation. Since all uses
+  // the same validateArguments method, we do a deeper test only once
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidPhraseAsSpan1() {
+    ChunkSample.phrasesAsSpanList(new String[2], new String[1], new String[1]);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidPhraseAsSpan2() {
+    ChunkSample.phrasesAsSpanList(new String[1], new String[2], new String[1]);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidPhraseAsSpan3() {
+    ChunkSample.phrasesAsSpanList(new String[1], new String[1], new String[2]);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidChunkSampleArray() {
+    new ChunkSample(new String[1], new String[1], new String[2]);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testInvalidChunkSampleList() {
+    new ChunkSample(Arrays.asList(new String[1]), Arrays.asList(new String[1]),
+        Arrays.asList(new String[2]));
+  }
+  
 }