You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2017/04/20 10:41:36 UTC

[49/50] [abbrv] opennlp git commit: OPENNLP-1012: Write a test case for NameSampleTypeFilter

OPENNLP-1012: Write a test case for NameSampleTypeFilter


Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo
Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/7b1cb70e
Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/7b1cb70e
Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/7b1cb70e

Branch: refs/heads/parser_regression
Commit: 7b1cb70eb75b34a5fc6d2398f26d427e1682a3c9
Parents: 107e09c
Author: Peter Thygesen <pe...@gmail.com>
Authored: Thu Mar 30 17:01:33 2017 +0200
Committer: J�rn Kottmann <jo...@apache.org>
Committed: Thu Apr 20 12:40:25 2017 +0200

----------------------------------------------------------------------
 .../namefind/NameSampleTypeFilterTest.java      | 102 +++++++++++++++++++
 1 file changed, 102 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/opennlp/blob/7b1cb70e/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTypeFilterTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTypeFilterTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTypeFilterTest.java
new file mode 100644
index 0000000..24ecc9f
--- /dev/null
+++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTypeFilterTest.java
@@ -0,0 +1,102 @@
+/*
+ * 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.namefind;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import opennlp.tools.util.InputStreamFactory;
+import opennlp.tools.util.ObjectStream;
+import opennlp.tools.util.PlainTextByLineStream;
+import opennlp.tools.util.Span;
+
+public class NameSampleTypeFilterTest {
+
+    private static NameSampleTypeFilter filter;
+
+    private static final String text = "<START:organization> NATO <END> Secretary - General " +
+            "<START:person> Anders Fogh Rasmussen <END> made clear that despite an intensifying " +
+            "insurgency and uncertainty over whether <START:location> U . S . <END> President " +
+            "<START:person> Barack Obama <END> will send more troops , <START:location> NATO <END> " +
+            "will remain in <START:location> Afghanistan <END> .";
+
+    private static final String person = "person";
+    private static final String organization = "organization";
+
+    @Test
+    public void testNoFilter() throws IOException {
+
+        final String[] types = new String[] {};
+
+        filter = new NameSampleTypeFilter(types, sampleStream(text));
+
+        NameSample ns = filter.read();
+
+        Assert.assertEquals(0, ns.getNames().length);
+
+    }
+
+    @Test
+    public void testSingleFilter() throws IOException {
+
+        final String[] types = new String[] {organization};
+
+        filter = new NameSampleTypeFilter(types, sampleStream(text));
+
+        NameSample ns = filter.read();
+
+        Assert.assertEquals(1, ns.getNames().length);
+        Assert.assertEquals(organization, ns.getNames()[0].getType());
+
+    }
+
+    @Test
+    public void testMultiFilter() throws IOException {
+
+        final String[] types = new String[] {person, organization};
+
+        filter = new NameSampleTypeFilter(types, sampleStream(text));
+
+        NameSample ns = filter.read();
+
+        Map<String, List<Span>> collect = Arrays.stream(ns.getNames())
+                .collect(Collectors.groupingBy(Span::getType));
+        Assert.assertEquals(2, collect.size());
+        Assert.assertEquals(2, collect.get(person).size());
+        Assert.assertEquals(1, collect.get(organization).size());
+
+    }
+
+    private ObjectStream<NameSample> sampleStream(String sampleText) throws IOException {
+
+        InputStreamFactory in = () -> new ByteArrayInputStream(sampleText.getBytes(StandardCharsets.UTF_8));
+
+        return new NameSampleDataStream(
+                new PlainTextByLineStream(in, StandardCharsets.UTF_8));
+
+    }
+
+}