Repository: opennlp Updated Branches: refs/heads/master 27214015c -> a59765cd4
OPENNLP-1020: MockInputStreamFactory.createInputStream should create a new InputStream. This closes apache/opennlp#156 Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/a59765cd Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/a59765cd Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/a59765cd Branch: refs/heads/master Commit: a59765cd4eb84f58af508ba5494c42b579c1dab3 Parents: 2721401 Author: koji <[email protected]> Authored: Mon Apr 17 10:30:40 2017 +0900 Committer: koji <[email protected]> Committed: Mon Apr 17 10:30:40 2017 +0900 ---------------------------------------------------------------------- .../lemmatizer/LemmatizerEvaluatorTest.java | 14 +++--- .../tools/lemmatizer/LemmatizerMETest.java | 8 ++- .../tools/namefind/NameFinderMETest.java | 51 ++++++-------------- .../tools/util/MockInputStreamFactory.java | 30 ++++++++---- 4 files changed, 46 insertions(+), 57 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/opennlp/blob/a59765cd/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java ---------------------------------------------------------------------- diff --git a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java index 0eb775d..2f4e74d 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java @@ -18,8 +18,8 @@ package opennlp.tools.lemmatizer; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import org.junit.Assert; @@ -47,18 +47,18 @@ public class LemmatizerEvaluatorTest { */ @Test public void testEvaluator() throws IOException { - InputStream inPredicted = getClass().getClassLoader() - .getResourceAsStream("opennlp/tools/lemmatizer/output.txt"); - InputStream inExpected = getClass().getClassLoader() - .getResourceAsStream("opennlp/tools/lemmatizer/output.txt"); + String inPredicted = "opennlp/tools/lemmatizer/output.txt"; + String inExpected = "opennlp/tools/lemmatizer/output.txt"; String encoding = "UTF-8"; DummyLemmaSampleStream predictedSample = new DummyLemmaSampleStream( - new PlainTextByLineStream(new MockInputStreamFactory(inPredicted), encoding), true); + new PlainTextByLineStream( + new MockInputStreamFactory(new File(inPredicted)), encoding), true); DummyLemmaSampleStream expectedSample = new DummyLemmaSampleStream( - new PlainTextByLineStream(new MockInputStreamFactory(inExpected), encoding), false); + new PlainTextByLineStream( + new MockInputStreamFactory(new File(inExpected)), encoding), false); Lemmatizer dummyLemmatizer = new DummyLemmatizer(predictedSample); http://git-wip-us.apache.org/repos/asf/opennlp/blob/a59765cd/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java ---------------------------------------------------------------------- diff --git a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java index 97dcc3c..4631763 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java @@ -17,8 +17,8 @@ package opennlp.tools.lemmatizer; +import java.io.File; import java.io.IOException; -import java.io.InputStream; import org.junit.Assert; import org.junit.Before; @@ -63,11 +63,9 @@ public class LemmatizerMETest { public void startup() throws IOException { // train the lemmatizer - InputStream in = getClass().getClassLoader() - .getResourceAsStream("opennlp/tools/lemmatizer/trial.old.tsv"); - ObjectStream<LemmaSample> sampleStream = new LemmaSampleStream( - new PlainTextByLineStream(new MockInputStreamFactory(in), "UTF-8")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/lemmatizer/trial.old.tsv")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(100)); http://git-wip-us.apache.org/repos/asf/opennlp/blob/a59765cd/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 c258d07..876df5b 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderMETest.java @@ -17,7 +17,7 @@ package opennlp.tools.namefind; -import java.io.InputStream; +import java.io.File; import java.util.Collections; import org.junit.Assert; @@ -57,15 +57,12 @@ public class NameFinderMETest { public void testNameFinder() throws Exception { // train the name finder - - InputStream in = getClass().getClassLoader().getResourceAsStream( - "opennlp/tools/namefind/AnnotatedSentences.txt"); - String encoding = "ISO-8859-1"; ObjectStream<NameSample> sampleStream = new NameSampleDataStream( - new PlainTextByLineStream(new MockInputStreamFactory(in), encoding)); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/AnnotatedSentences.txt")), encoding)); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); @@ -119,14 +116,11 @@ public class NameFinderMETest { public void testNameFinderWithTypes() throws Exception { // train the name finder - - InputStream in = getClass().getClassLoader().getResourceAsStream( - "opennlp/tools/namefind/AnnotatedSentencesWithTypes.txt"); - String encoding = "ISO-8859-1"; ObjectStream<NameSample> sampleStream = new NameSampleDataStream( - new PlainTextByLineStream(new MockInputStreamFactory(in), encoding)); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/AnnotatedSentencesWithTypes.txt")), encoding)); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); @@ -168,12 +162,9 @@ public class NameFinderMETest { 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 MockInputStreamFactory(in), "UTF-8")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/OnlyWithNames.train")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); @@ -201,12 +192,9 @@ public class NameFinderMETest { public void testOnlyWithNamesTypeOverride() throws Exception { // 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")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/OnlyWithNames.train")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); @@ -239,12 +227,9 @@ public class NameFinderMETest { 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 MockInputStreamFactory(in), "UTF-8")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/OnlyWithNamesWithTypes.train")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); @@ -277,12 +262,9 @@ public class NameFinderMETest { 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 MockInputStreamFactory(in), "UTF-8")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/OnlyWithEntitiesWithTypes.train")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ALGORITHM_PARAM, "MAXENT"); @@ -332,12 +314,9 @@ public class NameFinderMETest { public void testNameFinderWithMultipleTypes() throws Exception { // train the name finder - - InputStream in = getClass().getClassLoader().getResourceAsStream( - "opennlp/tools/namefind/voa1.train"); - ObjectStream<NameSample> sampleStream = new NameSampleDataStream( - new PlainTextByLineStream(new MockInputStreamFactory(in), "UTF-8")); + new PlainTextByLineStream(new MockInputStreamFactory( + new File("opennlp/tools/namefind/voa1.train")), "UTF-8")); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ITERATIONS_PARAM, Integer.toString(70)); http://git-wip-us.apache.org/repos/asf/opennlp/blob/a59765cd/opennlp-tools/src/test/java/opennlp/tools/util/MockInputStreamFactory.java ---------------------------------------------------------------------- diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/MockInputStreamFactory.java b/opennlp-tools/src/test/java/opennlp/tools/util/MockInputStreamFactory.java index 9d77cad..b9dbe6b 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/MockInputStreamFactory.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/MockInputStreamFactory.java @@ -18,30 +18,42 @@ package opennlp.tools.util; import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; +import java.io.File; import java.io.IOException; import java.io.InputStream; + import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class MockInputStreamFactory implements InputStreamFactory { - private InputStream is; + private final File inputSourceFile; + private final String inputSourceStr; + private final Charset charset; - public MockInputStreamFactory(InputStream is) throws FileNotFoundException { - this.is = is; + public MockInputStreamFactory(File file) { + this.inputSourceFile = file; + this.inputSourceStr = null; + this.charset = null; } - public MockInputStreamFactory(String str) throws FileNotFoundException { - this.is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); + public MockInputStreamFactory(String str) { + this(str, StandardCharsets.UTF_8); } - public MockInputStreamFactory(String str, Charset charset) throws FileNotFoundException { - this.is = new ByteArrayInputStream(str.getBytes(charset)); + public MockInputStreamFactory(String str, Charset charset) { + this.inputSourceFile = null; + this.inputSourceStr = str; + this.charset = charset; } @Override public InputStream createInputStream() throws IOException { - return is; + if (inputSourceFile != null) { + return getClass().getClassLoader().getResourceAsStream(inputSourceFile.getPath()); + } + else { + return new ByteArrayInputStream(inputSourceStr.getBytes(charset)); + } } }
