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 2017/08/02 11:25:45 UTC
[opennlp] branch master updated: OPENNLP-1121: Sort NameSample
spans to avoid serialization issues
This is an automated email from the ASF dual-hosted git repository.
colen 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 4362e02 OPENNLP-1121: Sort NameSample spans to avoid serialization issues
4362e02 is described below
commit 4362e02ed0404d12ca75ee3476d4a32f9f671811
Author: William D C M SILVA <co...@apache.org>
AuthorDate: Mon Jul 31 12:10:22 2017 -0300
OPENNLP-1121: Sort NameSample spans to avoid serialization issues
---
.../java/opennlp/tools/namefind/NameSample.java | 5 +--
.../opennlp/tools/namefind/NameSampleTest.java | 38 ++++++++++++++++++++++
2 files changed, 41 insertions(+), 2 deletions(-)
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 8858ceb..edd7506 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameSample.java
@@ -54,7 +54,9 @@ public class NameSample {
}
this.sentence = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(sentence)));
- this.names = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(names)));
+ List<Span> namesList = Arrays.asList(names);
+ Collections.sort(namesList);
+ this.names = Collections.unmodifiableList(namesList);
if (additionalContext != null) {
this.additionalContext = new String[additionalContext.length][];
@@ -158,7 +160,6 @@ public class NameSample {
result.append(NameSampleDataStream.START_TAG_PREFIX).append(name.getType()).append("> ");
}
}
-
if (name.getEnd() == tokenIndex) {
result.append(NameSampleDataStream.END_TAG).append(' ');
}
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 911f998..760e282 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java
@@ -62,6 +62,44 @@ public class NameSampleTest {
}
/**
+ * Test serialization of sequential spans.
+ */
+ @Test
+ public void testSequentialSpans() {
+
+ String[] sentence = {"A", "Place", "a", "time", "A", "Person", "."};
+
+ Span[] names = {new Span(0, 2, "Place"), new Span(2, 4, "Time"),
+ new Span(4, 6, "Person")};
+
+ NameSample nameSample;
+ nameSample = new NameSample(sentence, names, false);
+
+ Assert.assertEquals(
+ "<START:Place> A Place <END> <START:Time> a time <END> <START:Person> A Person <END> .",
+ nameSample.toString());
+ }
+
+ /**
+ * Test serialization of unsorted sequential spans.
+ */
+ @Test
+ public void testUnsortedSequentialSpans() {
+
+ String[] sentence = {"A", "Place", "a", "time", "A", "Person", "."};
+
+ Span[] names = {new Span(0, 2, "Place"), new Span(4, 6, "Person"),
+ new Span(2, 4, "Time")};
+
+ NameSample nameSample;
+ nameSample = new NameSample(sentence, names, false);
+
+ Assert.assertEquals(
+ "<START:Place> A Place <END> <START:Time> a time <END> <START:Person> A Person <END> .",
+ nameSample.toString());
+ }
+
+ /**
* Checks if could create a NameSample without NameTypes, generate the
* string representation and validate it.
*/
--
To stop receiving notification emails like this one, please contact
['"commits@opennlp.apache.org" <co...@opennlp.apache.org>'].