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/01/11 22:45:11 UTC
opennlp git commit: OPENNLP-719: Override any name type with
specified type Closes apache/opennlp#48
Repository: opennlp
Updated Branches:
refs/heads/master 8bba18089 -> f295b186f
OPENNLP-719: Override any name type with specified type
Closes apache/opennlp#48
Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo
Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/f295b186
Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/f295b186
Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/f295b186
Branch: refs/heads/master
Commit: f295b186fd8f6c619d34a8c69c6a1307f2276779
Parents: 8bba180
Author: William D C M SILVA <co...@apache.org>
Authored: Wed Jan 11 20:44:45 2017 -0200
Committer: William D C M SILVA <co...@apache.org>
Committed: Wed Jan 11 20:44:45 2017 -0200
----------------------------------------------------------------------
.../tools/namefind/NameFinderEventStream.java | 10 ++--
.../namefind/NameFinderEventStreamTest.java | 2 +-
.../tools/namefind/NameFinderMETest.java | 60 +++++++++++++++-----
3 files changed, 52 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/opennlp/blob/f295b186/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
index 8739b1f..f0e89ea 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/NameFinderEventStream.java
@@ -125,7 +125,7 @@ public class NameFinderEventStream extends opennlp.tools.util.AbstractEventStrea
Span[] names = sample.getNames();
if (!Objects.isNull(this.defaultType)) {
- overrideDefaultType(names);
+ overrideType(names);
}
String outcomes[] = codec.encode(names, sample.getSentence().length);
@@ -140,13 +140,11 @@ public class NameFinderEventStream extends opennlp.tools.util.AbstractEventStrea
return generateEvents(tokens, outcomes, contextGenerator).iterator();
}
- private void overrideDefaultType(Span[] names) {
+ private void overrideType(Span[] names) {
for (int i = 0; i < names.length; i++) {
Span n = names[i];
- if (Objects.isNull(n.getType())) {
- names[i] = new Span(n.getStart(), n.getEnd(), this.defaultType,
- n.getProb());
- }
+ names[i] = new Span(n.getStart(), n.getEnd(), this.defaultType,
+ n.getProb());
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/f295b186/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderEventStreamTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderEventStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderEventStreamTest.java
index 1cafd5b..1faecb2 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderEventStreamTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderEventStreamTest.java
@@ -76,7 +76,7 @@ public class NameFinderEventStreamTest {
ObjectStream<Event> eventStream = new NameFinderEventStream(
ObjectStreamUtils.createObjectStream(nameSample), type, CG, null);
- String prefix = "person-";
+ String prefix = type + "-";
Assert.assertEquals(prefix + NameFinderME.START, eventStream.read().getOutcome());
Assert.assertEquals(prefix + NameFinderME.CONTINUE,
eventStream.read().getOutcome());
http://git-wip-us.apache.org/repos/asf/opennlp/blob/f295b186/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
index 19e364e..bf11250 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java
@@ -51,7 +51,8 @@ import static org.junit.Assert.assertTrue;
*/
public class NameFinderMETest {
- private final String TYPE = "default";
+ private final String TYPE_OVERRIDE = "aType";
+ private final String DEFAULT = "default";
@Test
public void testNameFinder() throws Exception {
@@ -71,7 +72,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
TokenNameFinder nameFinder = new NameFinderME(nameFinderModel);
@@ -92,7 +93,7 @@ public class NameFinderMETest {
Span names[] = nameFinder.find(sentence);
assertEquals(1, names.length);
- assertEquals(new Span(0, 1, TYPE), names[0]);
+ assertEquals(new Span(0, 1, DEFAULT), names[0]);
sentence = new String[] {
"Hi",
@@ -107,8 +108,8 @@ public class NameFinderMETest {
names = nameFinder.find(sentence);
assertEquals(2, names.length);
- assertEquals(new Span(1, 2, TYPE), names[0]);
- assertEquals(new Span(4, 6, TYPE), names[1]);
+ assertEquals(new Span(1, 2, DEFAULT), names[0]);
+ assertEquals(new Span(4, 6, DEFAULT), names[1]);
}
/**
@@ -132,7 +133,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
NameFinderME nameFinder = new NameFinderME(nameFinderModel);
@@ -170,6 +171,39 @@ public class NameFinderMETest {
// train the name finder
InputStream in = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/namefind/OnlyWithNames.train");
+
+ ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
+ new PlainTextByLineStream(new MockInputStreamFactory(in), "UTF-8"));
+
+ TrainingParameters params = new TrainingParameters();
+ params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
+ params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
+
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
+ params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
+
+ 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, DEFAULT), names1[0]);
+ assertEquals(new Span(2, 4, DEFAULT), names1[1]);
+ assertEquals(new Span(4, 6, DEFAULT), names1[2]);
+ assertTrue(!hasOtherAsOutcome(nameFinderModel));
+ }
+
+ @Test
+ public void testOnlyWithNamesTypeOverride() throws Exception {
+
+ // train the name finder
+
+ InputStream in = getClass().getClassLoader().getResourceAsStream(
"opennlp/tools/namefind/OnlyWithNames.train");
ObjectStream<NameSample> sampleStream = new NameSampleDataStream(
@@ -179,7 +213,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE_OVERRIDE, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
NameFinderME nameFinder = new NameFinderME(nameFinderModel);
@@ -191,9 +225,9 @@ public class NameFinderMETest {
Span[] names1 = nameFinder.find(sentence);
- assertEquals(new Span(0, 2, TYPE), names1[0]);
- assertEquals(new Span(2, 4, TYPE), names1[1]);
- assertEquals(new Span(4, 6, TYPE), names1[2]);
+ assertEquals(new Span(0, 2, TYPE_OVERRIDE), names1[0]);
+ assertEquals(new Span(2, 4, TYPE_OVERRIDE), names1[1]);
+ assertEquals(new Span(4, 6, TYPE_OVERRIDE), names1[2]);
assertTrue(!hasOtherAsOutcome(nameFinderModel));
}
@@ -216,7 +250,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
NameFinderME nameFinder = new NameFinderME(nameFinderModel);
@@ -255,7 +289,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
NameFinderME nameFinder = new NameFinderME(nameFinderModel);
@@ -310,7 +344,7 @@ public class NameFinderMETest {
params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70));
params.put(TrainingParameters.CUTOFF_PARAM, Integer.toString(1));
- TokenNameFinderModel nameFinderModel = NameFinderME.train("en", TYPE, sampleStream,
+ TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream,
params, TokenNameFinderFactory.create(null, null, Collections.emptyMap(), new BioCodec()));
NameFinderME nameFinder = new NameFinderME(nameFinderModel);