You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by ko...@apache.org on 2017/10/03 00:40:11 UTC

[opennlp] branch master updated: OPENNLP-1137: Add more tests and check overlapping of name spans to NameSample (#268)

This is an automated email from the ASF dual-hosted git repository.

koji pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/master by this push:
     new a0611b0  OPENNLP-1137: Add more tests and check overlapping of name spans to NameSample (#268)
a0611b0 is described below

commit a0611b06f05e3c37c622f48a783836f89247c237
Author: Koji Sekiguchi <ko...@rondhuit.com>
AuthorDate: Tue Oct 3 09:40:09 2017 +0900

    OPENNLP-1137: Add more tests and check overlapping of name spans to NameSample (#268)
---
 .../java/opennlp/tools/namefind/NameSample.java    | 10 +++++++++-
 .../opennlp/tools/namefind/NameSampleTest.java     | 23 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
index edd7506..6db4b69 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
@@ -71,7 +71,15 @@ public class NameSample {
     }
     isClearAdaptiveData = clearAdaptiveData;
 
-    // TODO: Check that name spans are not overlapping, otherwise throw exception
+    // Check that name spans are not overlapping, otherwise throw exception
+    if (this.names.size() > 1) {
+      for (int i = 1; i < this.names.size(); i++) {
+        if (this.names.get(i).getStart() < this.names.get(i - 1).getEnd()) {
+          throw new RuntimeException(String.format("name spans %s and %s are overlapped",
+              this.names.get(i - 1), this.names.get(i)));
+        }
+      }
+    }
   }
 
   /**
diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java
index 760e282..0078731 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java
@@ -100,6 +100,20 @@ public class NameSampleTest {
   }
 
   /**
+   * Test if it fails to name spans are overlapping
+   */
+  @Test(expected = RuntimeException.class)
+  public void testOverlappingNameSpans() throws Exception {
+
+    String[] sentence = {"A", "Place", "a", "time", "A", "Person", "."};
+
+    Span[] names = {new Span(0, 2, "Place"), new Span(3, 5, "Person"),
+        new Span(2, 4, "Time")};
+
+    new NameSample(sentence, names, false);
+  }
+
+  /**
    * Checks if could create a NameSample without NameTypes, generate the
    * string representation and validate it.
    */
@@ -227,6 +241,15 @@ public class NameSampleTest {
     NameSample.parse("<START:abc>a> token <END>", false);
   }
 
+  /**
+   * Test if it fails to parse nested names
+   * @throws Exception
+   */
+  @Test(expected = IOException.class)
+  public void testNestedNameSpans() throws Exception {
+    NameSample.parse("<START:Person> <START:Location> Kennedy <END> City <END>", false);
+  }
+
   @Test
   public void testEquals() {
     Assert.assertFalse(createGoldSample() == createGoldSample());

-- 
To stop receiving notification emails like this one, please contact
['"commits@opennlp.apache.org" <co...@opennlp.apache.org>'].