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 2010/12/15 18:55:42 UTC
svn commit: r1049653 - in /incubator/opennlp/trunk/opennlp-tools/src:
main/java/opennlp/tools/namefind/ test/java/opennlp/tools/namefind/
test/resources/opennlp/tools/namefind/
Author: colen
Date: Wed Dec 15 17:55:42 2010
New Revision: 1049653
URL: http://svn.apache.org/viewvc?rev=1049653&view=rev
Log:
OPENNLP-9 now name finder can be trained only with names
Added:
incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train
incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNames.train
incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNamesWithTypes.train
Modified:
incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/TokenNameFinderModel.java
incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
Modified: incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/TokenNameFinderModel.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/TokenNameFinderModel.java?rev=1049653&r1=1049652&r2=1049653&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/TokenNameFinderModel.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/namefind/TokenNameFinderModel.java Wed Dec 15 17:55:42 2010
@@ -79,12 +79,11 @@ public class TokenNameFinderModel extend
// TODO: Write test for this method
public static boolean isModelValid(MaxentModel model) {
- // We should have one outcome named "other", some named xyz-start and sometimes
+ // We should have *optionally* one outcome named "other", some named xyz-start and sometimes
// they have a pair xyz-cont. We should not have any other outcome
// To validate the model we check if we have one outcome named "other", at least
// one outcome with suffix start. After that we check if all outcomes that ends with
// "cont" have a pair that ends with "start".
- boolean otherFounded = false;
List<String> start = new ArrayList<String>();
List<String> cont = new ArrayList<String>();
@@ -97,14 +96,14 @@ public class TokenNameFinderModel extend
cont.add(outcome.substring(0, outcome.length()
- NameFinderME.CONTINUE.length()));
} else if (outcome.equals(NameFinderME.OTHER)) {
- otherFounded = true;
+ // don't fail anymore if couldn't find outcome named OTHER
} else {
// got unexpected outcome
return false;
}
}
- if (!otherFounded || start.size() == 0) {
+ if (start.size() == 0) {
return false;
} else {
for (String contPreffix : cont) {
Modified: incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java?rev=1049653&r1=1049652&r2=1049653&view=diff
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java (original)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java Wed Dec 15 17:55:42 2010
@@ -19,11 +19,13 @@
package opennlp.tools.namefind;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
+import opennlp.model.AbstractModel;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.Span;
@@ -148,6 +150,132 @@ public class NameFinderMETest {
assertEquals(1, names.length);
assertEquals(new Span(0, 1), names[0]);
+ assertTrue(hasOtherAsOutcome(nameFinderModel));
+ }
+
+ /**
+ * Train NamefinderME using OnlyWithNames.train. The goal is to check if the model validator accepts it.
+ * This is related to the issue OPENNLP-9
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testOnlyWithNames() throws Exception {
+
+ // train the name finder
+
+ InputStream in = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/namefind/OnlyWithNames.train");
+
+ ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
+ new PlainTextByLineStream(new InputStreamReader(in)));
+
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", "default",
+ sampleStream, Collections.<String, Object>emptyMap(), 70, 1);
+
+ NameFinderME nameFinder = new NameFinderME(nameFinderModel);
+
+ // now test if it can detect the sample sentences
+
+ String[] sentence = ("Neil Abercrombie Anibal Acevedo-Vila Gary Ackerman " +
+ "Robert Aderholt Daniel Akaka Todd Akin Lamar Alexander Rodney Alexander").split("\\s+");
+
+ Span[] names1 = nameFinder.find(sentence);
+
+ assertEquals(new Span(0, 2), names1[0]);
+ assertEquals(new Span(2, 4), names1[1]);
+ assertEquals(new Span(4, 6), names1[2]);
+ assertTrue(!hasOtherAsOutcome(nameFinderModel));
+ }
+
+ /**
+ * Train NamefinderME using OnlyWithNamesWithTypes.train. The goal is to check if the model validator accepts it.
+ * This is related to the issue OPENNLP-9
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testOnlyWithNamesWithTypes() throws Exception {
+
+ // train the name finder
+
+ InputStream in = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/namefind/OnlyWithNamesWithTypes.train");
+
+ ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
+ new PlainTextByLineStream(new InputStreamReader(in)));
+
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", "default",
+ sampleStream, Collections.<String, Object>emptyMap(), 70, 1);
+
+ NameFinderME nameFinder = new NameFinderME(nameFinderModel);
+
+ // now test if it can detect the sample sentences
+
+ String[] sentence = ("Neil Abercrombie Anibal Acevedo-Vila Gary Ackerman " +
+ "Robert Aderholt Daniel Akaka Todd Akin Lamar Alexander Rodney Alexander").split("\\s+");
+
+ Span[] names1 = nameFinder.find(sentence);
+
+ assertEquals(new Span(0, 2, "person"), names1[0]);
+ assertEquals(new Span(2, 4, "person"), names1[1]);
+ assertEquals(new Span(4, 6, "person"), names1[2]);
+ assertEquals("person", names1[2].getType());
+
+ assertTrue(!hasOtherAsOutcome(nameFinderModel));
+ }
+
+ /**
+ * Train NamefinderME using OnlyWithNames.train. The goal is to check if the model validator accepts it.
+ * This is related to the issue OPENNLP-9
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testOnlyWithEntitiesWithTypes() throws Exception {
+
+ // train the name finder
+
+ InputStream in = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train");
+
+ ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
+ new PlainTextByLineStream(new InputStreamReader(in)));
+
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", "default",
+ sampleStream, Collections.<String, Object>emptyMap(), 70, 1);
+
+ NameFinderME nameFinder = new NameFinderME(nameFinderModel);
+
+ // now test if it can detect the sample sentences
+
+ String[] sentence = ("NATO United States Barack Obama").split("\\s+");
+
+ Span[] names1 = nameFinder.find(sentence);
+
+ assertEquals(new Span(0, 1), names1[0]);
+ assertEquals(new Span(1, 3), names1[1]);
+ assertEquals("person", names1[2].getType());
+ assertTrue(!hasOtherAsOutcome(nameFinderModel));
+ }
+
+ private boolean hasOtherAsOutcome(TokenNameFinderModel nameFinderModel) {
+ AbstractModel model = nameFinderModel.getNameFinderModel();
+ for (int i = 0; i < model.getNumOutcomes(); i++) {
+ String outcome = model.getOutcome(i);
+ if (outcome.equals(NameFinderME.OTHER)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Test
+ public void testDropOverlappingSpans() {
+ Span spans[] = new Span[] {new Span(1, 10), new Span(1,11), new Span(1,11), new Span(5, 15)};
+ Span remainingSpan[] = NameFinderME.dropOverlappingSpans(spans);
+
+ assertEquals(new Span(1, 11), remainingSpan[0]);
}
/**
@@ -204,11 +332,4 @@ public class NameFinderMETest {
assertEquals("organization", names2[1].getType());
}
- @Test
- public void testDropOverlappingSpans() {
- Span spans[] = new Span[] {new Span(1, 10), new Span(1,11), new Span(1,11), new Span(5, 15)};
- Span remainingSpan[] = NameFinderME.dropOverlappingSpans(spans);
-
- assertEquals(new Span(1, 11), remainingSpan[0]);
- }
}
Added: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train?rev=1049653&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train Wed Dec 15 17:55:42 2010
@@ -0,0 +1,12 @@
+<START:organization> NATO <END>
+<START:location> United States <END>
+<START:organization> NATO Parliamentary Assembly <END>
+<START:location> Edinburgh <END>
+<START:location> Britain <END>
+<START:person> Anders Fogh Rasmussen <END>
+<START:location> U . S . <END>
+<START:person> Barack Obama <END>
+<START:location> Afghanistan <END>
+<START:person> Rasmussen <END>
+<START:location> Afghanistan <END>
+<START:date> 2010 <END>
\ No newline at end of file
Added: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNames.train
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNames.train?rev=1049653&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNames.train (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNames.train Wed Dec 15 17:55:42 2010
@@ -0,0 +1,8 @@
+<START> Neil Abercrombie <END>
+<START> Anibal Acevedo-Vila <END>
+<START> Gary Ackerman <END>
+<START> Robert Aderholt <END>
+<START> Daniel Akaka <END>
+<START> Todd Akin <END>
+<START> Lamar Alexander <END>
+<START> Rodney Alexander <END>
\ No newline at end of file
Added: incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNamesWithTypes.train
URL: http://svn.apache.org/viewvc/incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNamesWithTypes.train?rev=1049653&view=auto
==============================================================================
--- incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNamesWithTypes.train (added)
+++ incubator/opennlp/trunk/opennlp-tools/src/test/resources/opennlp/tools/namefind/OnlyWithNamesWithTypes.train Wed Dec 15 17:55:42 2010
@@ -0,0 +1,8 @@
+<START:person> Neil Abercrombie <END>
+<START:person> Anibal Acevedo-Vila <END>
+<START:person> Gary Ackerman <END>
+<START:person> Robert Aderholt <END>
+<START:person> Daniel Akaka <END>
+<START:person> Todd Akin <END>
+<START:person> Lamar Alexander <END>
+<START:person> Rodney Alexander <END>
\ No newline at end of file