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 2016/10/28 20:11:27 UTC
[1/2] opennlp git commit: Ignore Intellij project files
Repository: opennlp
Updated Branches:
refs/heads/trunk 426944c52 -> c91830eea
Ignore Intellij project files
Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo
Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/c7d4a9c9
Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/c7d4a9c9
Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/c7d4a9c9
Branch: refs/heads/trunk
Commit: c7d4a9c925ca053009d5f1fabc9f87caf28a1f65
Parents: 426944c
Author: J�rn Kottmann <jo...@apache.org>
Authored: Fri Oct 28 21:34:21 2016 +0200
Committer: J�rn Kottmann <jo...@apache.org>
Committed: Fri Oct 28 21:34:21 2016 +0200
----------------------------------------------------------------------
.gitignore | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c7d4a9c9/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 657a6fd..5d44bbd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,9 @@
+*.iml
+.idea
target
.classpath
.project
.settings
nbactions.xml
nb-configuration.xml
-*.DS_Store
\ No newline at end of file
+*.DS_Store
[2/2] opennlp git commit: Use try with resources
Posted by jo...@apache.org.
Use try with resources
See issue OPENNLP-872
Project: http://git-wip-us.apache.org/repos/asf/opennlp/repo
Commit: http://git-wip-us.apache.org/repos/asf/opennlp/commit/c91830ee
Tree: http://git-wip-us.apache.org/repos/asf/opennlp/tree/c91830ee
Diff: http://git-wip-us.apache.org/repos/asf/opennlp/diff/c91830ee
Branch: refs/heads/trunk
Commit: c91830eea96fc7d1f21c83c4d0c8f17dcb9351f4
Parents: c7d4a9c
Author: J�rn Kottmann <jo...@apache.org>
Authored: Fri Oct 28 22:02:38 2016 +0200
Committer: J�rn Kottmann <jo...@apache.org>
Committed: Fri Oct 28 22:02:38 2016 +0200
----------------------------------------------------------------------
.../tools/cmdline/AbstractConverterTool.java | 12 +-------
.../java/opennlp/tools/cmdline/CmdLineUtil.java | 27 ++---------------
.../java/opennlp/tools/cmdline/ModelLoader.java | 15 ++--------
.../dictionary/DictionaryBuilderTool.java | 16 ++--------
.../formats/brat/AnnotationConfiguration.java | 9 ++++++
.../tools/formats/brat/BratDocumentStream.java | 26 ++--------------
.../brat/BratNameSampleStreamFactory.java | 11 +------
.../convert/FileToByteArraySampleStream.java | 12 +-------
.../ml/model/RealValueFileEventStream.java | 5 +---
.../tools/ml/model/TwoPassDataIndexer.java | 5 +---
.../main/java/opennlp/tools/util/Version.java | 18 ++++--------
.../opennlp/tools/util/model/BaseModel.java | 14 ++-------
.../ChunkerDetailedFMeasureListenerTest.java | 26 +++++++---------
.../ConstitParseSampleStreamTest.java | 8 ++---
.../formats/muc/DocumentSplitterStreamTest.java | 26 ++++++++--------
.../tools/formats/muc/SgmlParserTest.java | 8 +----
.../opennlp/tools/ml/PrepAttachDataUtil.java | 13 +++-----
.../tools/ml/maxent/RealValueModelTest.java | 16 ++++------
.../opennlp/tools/postag/POSDictionaryTest.java | 7 +----
.../util/featuregen/GeneratorFactoryTest.java | 8 ++---
.../java/opennlp/uima/util/OpennlpUtil.java | 31 +++-----------------
21 files changed, 73 insertions(+), 240 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractConverterTool.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractConverterTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractConverterTool.java
index 1bfe6fc..9b4f23f 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractConverterTool.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractConverterTool.java
@@ -103,9 +103,7 @@ public abstract class AbstractConverterTool<T> extends TypedCmdLineTool<T> {
throw new TerminateToolException(1, errorMessage + "\n" + helpString);
}
- ObjectStream<T> sampleStream = streamFactory.create(formatArgs);
-
- try {
+ try (ObjectStream<T> sampleStream = streamFactory.create(formatArgs)) {
Object sample;
while((sample = sampleStream.read()) != null) {
System.out.println(sample.toString());
@@ -114,14 +112,6 @@ public abstract class AbstractConverterTool<T> extends TypedCmdLineTool<T> {
catch (IOException e) {
throw new TerminateToolException(-1, "IO error while converting data : " + e.getMessage(), e);
}
- finally {
- if (sampleStream != null)
- try {
- sampleStream.close();
- } catch (IOException e) {
- // sorry that this can fail
- }
- }
}
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/cmdline/CmdLineUtil.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/CmdLineUtil.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/CmdLineUtil.java
index 14f05a2..64881ff 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/CmdLineUtil.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/CmdLineUtil.java
@@ -177,22 +177,12 @@ public final class CmdLineUtil {
long beginModelWritingTime = System.currentTimeMillis();
- OutputStream modelOut = null;
- try {
- modelOut = new BufferedOutputStream(new FileOutputStream(modelFile), IO_BUFFER_SIZE);
+ try (OutputStream modelOut = new BufferedOutputStream(
+ new FileOutputStream(modelFile), IO_BUFFER_SIZE)) {
model.serialize(modelOut);
} catch (IOException e) {
System.err.println("failed");
throw new TerminateToolException(-1, "Error during writing model file '" + modelFile + "'", e);
- } finally {
- if (modelOut != null) {
- try {
- modelOut.close();
- } catch (IOException e) {
- System.err.println("Failed to properly close model file '" + modelFile + "': " +
- e.getMessage());
- }
- }
}
long modelWritingDuration = System.currentTimeMillis() - beginModelWritingTime;
@@ -326,22 +316,11 @@ public final class CmdLineUtil {
checkInputFile("Training Parameter", new File(paramFile));
- InputStream paramsIn = null;
- try {
- paramsIn = new FileInputStream(new File(paramFile));
-
+ try (InputStream paramsIn = new FileInputStream(new File(paramFile))) {
params = new opennlp.tools.util.TrainingParameters(paramsIn);
} catch (IOException e) {
throw new TerminateToolException(-1, "Error during parameters loading: " + e.getMessage(), e);
}
- finally {
- try {
- if (paramsIn != null)
- paramsIn.close();
- } catch (IOException e) {
- //sorry that this can fail
- }
- }
if (!TrainerFactory.isValid(params.getSettings())) {
throw new TerminateToolException(1, "Training parameters file '" + paramFile + "' is invalid!");
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/cmdline/ModelLoader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/ModelLoader.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/ModelLoader.java
index 1d2f749..3e5db54 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/ModelLoader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/ModelLoader.java
@@ -54,11 +54,9 @@ public abstract class ModelLoader<T> {
System.err.print("Loading " + modelName + " model ... ");
- InputStream modelIn = new BufferedInputStream(CmdLineUtil.openInFile(modelFile), CmdLineUtil.IO_BUFFER_SIZE);
-
T model;
-
- try {
+ try (InputStream modelIn = new BufferedInputStream(
+ CmdLineUtil.openInFile(modelFile), CmdLineUtil.IO_BUFFER_SIZE)) {
model = loadModel(modelIn);
}
catch (InvalidFormatException e) {
@@ -69,15 +67,6 @@ public abstract class ModelLoader<T> {
System.err.println("failed");
throw new TerminateToolException(-1, "IO error while loading model file '" + modelFile + "'", e);
}
- finally {
- // will not be null because openInFile would
- // terminate in this case
- try {
- modelIn.close();
- } catch (IOException e) {
- // sorry that this can fail
- }
- }
long modelLoadingDuration = System.currentTimeMillis() - beginModelLoadingTime;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/cmdline/dictionary/DictionaryBuilderTool.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/dictionary/DictionaryBuilderTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/dictionary/DictionaryBuilderTool.java
index 474730f..a0992ea 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/dictionary/DictionaryBuilderTool.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/dictionary/DictionaryBuilderTool.java
@@ -53,26 +53,14 @@ public class DictionaryBuilderTool extends BasicCmdLineTool {
CmdLineUtil.checkInputFile("dictionary input file", dictInFile);
CmdLineUtil.checkOutputFile("dictionary output file", dictOutFile);
- InputStreamReader in = null;
- OutputStream out = null;
- try {
- in = new InputStreamReader(new FileInputStream(dictInFile), encoding);
- out = new FileOutputStream(dictOutFile);
+ try (InputStreamReader in = new InputStreamReader(new FileInputStream(dictInFile), encoding);
+ OutputStream out = new FileOutputStream(dictOutFile)) {
Dictionary dict = Dictionary.parseOneEntryPerLine(in);
dict.serialize(out);
} catch (IOException e) {
throw new TerminateToolException(-1, "IO error while reading training data or indexing data: " + e.getMessage(), e);
- } finally {
- try {
- in.close();
- out.close();
- } catch (IOException e) {
- // sorry that this can fail
- }
}
-
}
-
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
index 40b358b..505ba4e 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/AnnotationConfiguration.java
@@ -17,7 +17,10 @@
package opennlp.tools.formats.brat;
+import java.io.BufferedInputStream;
import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -90,4 +93,10 @@ public class AnnotationConfiguration {
return new AnnotationConfiguration(typeToClassMap);
}
+
+ public static AnnotationConfiguration parse(File annConfigFile) throws IOException {
+ try (InputStream in = new BufferedInputStream(new FileInputStream(annConfigFile))) {
+ return parse(in);
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocumentStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocumentStream.java b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocumentStream.java
index aefd389..ba82089 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocumentStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratDocumentStream.java
@@ -93,32 +93,10 @@ public class BratDocumentStream implements ObjectStream<BratDocument> {
if (documentIdIterator.hasNext()) {
String id = documentIdIterator.next();
- InputStream txtIn = null;
- InputStream annIn = null;
-
- try {
- txtIn = new BufferedInputStream(new FileInputStream(id + ".txt"));
- annIn = new BufferedInputStream(new FileInputStream(id + ".ann"));
-
+ try (InputStream txtIn = new BufferedInputStream(new FileInputStream(id + ".txt"));
+ InputStream annIn = new BufferedInputStream(new FileInputStream(id + ".ann"))) {
doc = BratDocument.parseDocument(config, id, txtIn, annIn);
}
- finally{
- if (txtIn != null) {
- try {
- txtIn.close();
- }
- catch (IOException e) {
- }
- }
-
- if (annIn!= null) {
- try {
- annIn.close();
- }
- catch (IOException e) {
- }
- }
- }
}
return doc;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java
index 5a84dd8..b7a8c41 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java
@@ -97,21 +97,12 @@ public class BratNameSampleStreamFactory extends AbstractSampleStreamFactory<Nam
// TODO: Provide the file name to the annotation.conf file and implement the parser ...
AnnotationConfiguration annConfig;
- InputStream annConfIn = null;
try {
- annConfIn = new FileInputStream(params.getAnnotationConfig());
- annConfig = AnnotationConfiguration.parse(annConfIn);
+ annConfig = AnnotationConfiguration.parse(params.getAnnotationConfig());
}
catch (IOException e) {
throw new TerminateToolException(1, "Failed to parse annotation.conf file!");
}
- finally {
- if (annConfIn != null) {
- try {
- annConfIn.close();
- } catch (IOException e) {}
- }
- }
// TODO: Add an optional parameter to search recursive
// TODO: How to handle the error here ? terminate the tool? not nice if used by API!
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/formats/convert/FileToByteArraySampleStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/convert/FileToByteArraySampleStream.java b/opennlp-tools/src/main/java/opennlp/tools/formats/convert/FileToByteArraySampleStream.java
index f45b4bf..0367b95 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/formats/convert/FileToByteArraySampleStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/formats/convert/FileToByteArraySampleStream.java
@@ -35,25 +35,15 @@ public class FileToByteArraySampleStream extends FilterObjectStream<File, byte[]
private static byte[] readFile(File file) throws IOException {
- InputStream in = new BufferedInputStream(new FileInputStream(file));
-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- try {
+ try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
byte buffer[] = new byte[1024];
int length;
while ((length = in.read(buffer, 0, buffer.length)) > 0) {
bytes.write(buffer, 0, length);
}
}
- finally {
- try {
- in.close();
- }
- catch (IOException e) {
- // sorry that this can fail!
- }
- }
return bytes.toByteArray();
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/ml/model/RealValueFileEventStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/RealValueFileEventStream.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/RealValueFileEventStream.java
index 13bdb1e..68b3503 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/RealValueFileEventStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/RealValueFileEventStream.java
@@ -113,11 +113,8 @@ public class RealValueFileEventStream extends FileEventStream {
cutoff = Integer.parseInt(args[ai++]);
}
AbstractModel model;
- RealValueFileEventStream es = new RealValueFileEventStream(eventFile);
- try {
+ try (RealValueFileEventStream es = new RealValueFileEventStream(eventFile)) {
model = GIS.trainModel(iterations, new OnePassRealValueDataIndexer(es, cutoff));
- } finally {
- es.close();
}
new SuffixSensitiveGISModelWriter(model, new File(eventFile + ".bin.gz")).persist();
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
index 8b39ce4..821335b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
@@ -85,11 +85,8 @@ public class TwoPassDataIndexer extends AbstractDataIndexer{
System.out.print("\tIndexing... ");
- FileEventStream fes = new FileEventStream(tmp);
- try {
+ try (FileEventStream fes = new FileEventStream(tmp)) {
eventsToCompare = index(numEvents, fes, predicateIndex);
- } finally {
- fes.close();
}
// done with predicates
predicateIndex = null;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/Version.java b/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
index 248af63..7683b30 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/Version.java
@@ -191,21 +191,13 @@ public class Version {
// Try to read the version from the version file if it is available,
// otherwise set the version to the development version
- InputStream versionIn = Version.class.getResourceAsStream("opennlp.version");
-
- if (versionIn != null) {
- try {
+ try (InputStream versionIn =
+ Version.class.getResourceAsStream("opennlp.version")) {
+ if (versionIn != null) {
manifest.load(versionIn);
- } catch (IOException e) {
- // ignore error
- }
- finally {
- try {
- versionIn.close();
- } catch (IOException e) {
- // ignore error
- }
}
+ } catch (IOException e) {
+ // ignore error
}
String versionString =
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
index 5656e31..989c80b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
@@ -182,27 +182,17 @@ public abstract class BaseModel implements ArtifactProvider {
protected BaseModel(String componentName, File modelFile) throws IOException, InvalidFormatException {
this(componentName, true);
- InputStream in = new BufferedInputStream(new FileInputStream(modelFile));
-
- try {
+ try (InputStream in = new BufferedInputStream(new FileInputStream(modelFile))) {
loadModel(in);
}
- finally {
- in.close();
- }
}
protected BaseModel(String componentName, URL modelURL) throws IOException, InvalidFormatException {
this(componentName, true);
- InputStream in = new BufferedInputStream(modelURL.openStream());
-
- try {
+ try (InputStream in = new BufferedInputStream(modelURL.openStream())) {
loadModel(in);
}
- finally {
- in.close();
- }
}
private void loadModel(InputStream in) throws IOException, InvalidFormatException {
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerDetailedFMeasureListenerTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerDetailedFMeasureListenerTest.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerDetailedFMeasureListenerTest.java
index d0493e7..905339b 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerDetailedFMeasureListenerTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerDetailedFMeasureListenerTest.java
@@ -34,23 +34,21 @@ public class ChunkerDetailedFMeasureListenerTest {
@Test
public void testEvaluator() throws IOException {
- InputStream inPredicted = getClass().getClassLoader().getResourceAsStream(
- "opennlp/tools/chunker/output.txt");
- InputStream inExpected = getClass().getClassLoader().getResourceAsStream(
- "opennlp/tools/chunker/output.txt");
-
- InputStream detailedOutputStream = getClass().getClassLoader().getResourceAsStream(
- "opennlp/tools/chunker/detailedOutput.txt");
String encoding = "UTF-8";
- try {
+ try (InputStream inPredicted = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/chunker/output.txt");
+ InputStream inExpected = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/chunker/output.txt");
+ InputStream detailedOutputStream = getClass().getClassLoader().getResourceAsStream(
+ "opennlp/tools/chunker/detailedOutput.txt")) {
DummyChunkSampleStream predictedSample = new DummyChunkSampleStream(
- new PlainTextByLineStream(
- new InputStreamReader(inPredicted, encoding)), true);
+ new PlainTextByLineStream(
+ new InputStreamReader(inPredicted, encoding)), true);
DummyChunkSampleStream expectedSample = new DummyChunkSampleStream(
- new PlainTextByLineStream(new InputStreamReader(inExpected)), false);
+ new PlainTextByLineStream(new InputStreamReader(inExpected)), false);
Chunker dummyChunker = new DummyChunker(predictedSample);
@@ -63,16 +61,12 @@ public class ChunkerDetailedFMeasureListenerTest {
BufferedReader reader = new BufferedReader(new InputStreamReader(detailedOutputStream, encoding));
String line = reader.readLine();
- while(line != null ) {
+ while (line != null) {
expected.append(line);
expected.append("\n");
line = reader.readLine();
}
assertEquals(expected.toString().trim(), listener.createReport(Locale.ENGLISH).trim());
- } finally {
- inPredicted.close();
- inExpected.close();
- detailedOutputStream.close();
}
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/formats/frenchtreebank/ConstitParseSampleStreamTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/formats/frenchtreebank/ConstitParseSampleStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/formats/frenchtreebank/ConstitParseSampleStreamTest.java
index 653881b..aa7a240 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/formats/frenchtreebank/ConstitParseSampleStreamTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/formats/frenchtreebank/ConstitParseSampleStreamTest.java
@@ -88,17 +88,13 @@ public class ConstitParseSampleStreamTest {
static byte[] getSample1() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- InputStream sampleIn =
- ConstitParseSampleStreamTest.class.getResourceAsStream("sample1.xml");
-
byte buffer[] = new byte[1024];
int length;
- try {
+ try (InputStream sampleIn =
+ ConstitParseSampleStreamTest.class.getResourceAsStream("sample1.xml")) {
while ((length = sampleIn.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
- } finally {
- sampleIn.close();
}
return out.toByteArray();
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/formats/muc/DocumentSplitterStreamTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/formats/muc/DocumentSplitterStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/formats/muc/DocumentSplitterStreamTest.java
index cd10c29..a044682 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/formats/muc/DocumentSplitterStreamTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/formats/muc/DocumentSplitterStreamTest.java
@@ -38,18 +38,18 @@ public class DocumentSplitterStreamTest {
docsString.append("</DOC>\n");
}
- ObjectStream<String> docs = new DocumentSplitterStream(
- ObjectStreamUtils.createObjectStream(docsString.toString()));
-
- String doc1 = docs.read();
- Assert.assertEquals(docsString.length() / 2, doc1.length() + 1);
- Assert.assertTrue(doc1.contains("#0"));
-
- String doc2 = docs.read();
- Assert.assertEquals(docsString.length() / 2, doc2.length() + 1);
- Assert.assertTrue(doc2.contains("#1"));
-
- Assert.assertNull(docs.read());
- Assert.assertNull(docs.read());
+ try (ObjectStream<String> docs = new DocumentSplitterStream(
+ ObjectStreamUtils.createObjectStream(docsString.toString()))) {
+ String doc1 = docs.read();
+ Assert.assertEquals(docsString.length() / 2, doc1.length() + 1);
+ Assert.assertTrue(doc1.contains("#0"));
+
+ String doc2 = docs.read();
+ Assert.assertEquals(docsString.length() / 2, doc2.length() + 1);
+ Assert.assertTrue(doc2.contains("#1"));
+
+ Assert.assertNull(docs.read());
+ Assert.assertNull(docs.read());
+ }
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/formats/muc/SgmlParserTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/formats/muc/SgmlParserTest.java b/opennlp-tools/src/test/java/opennlp/tools/formats/muc/SgmlParserTest.java
index afdeded..87aad7c 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/formats/muc/SgmlParserTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/formats/muc/SgmlParserTest.java
@@ -28,17 +28,11 @@ public class SgmlParserTest {
@Test
public void testParse1() throws IOException {
- Reader in = new InputStreamReader(SgmlParserTest.class.getResourceAsStream("parsertest1.sgml"), "UTF-8");
-
- try {
+ try (Reader in = new InputStreamReader(SgmlParserTest.class.getResourceAsStream("parsertest1.sgml"), "UTF-8")) {
SgmlParser parser = new SgmlParser();
parser.parse(in, new SgmlParser.ContentHandler() {
});
}
- finally {
- in.close();
- }
-
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/ml/PrepAttachDataUtil.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/PrepAttachDataUtil.java b/opennlp-tools/src/test/java/opennlp/tools/ml/PrepAttachDataUtil.java
index a51288a..79dd00d 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/ml/PrepAttachDataUtil.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/ml/PrepAttachDataUtil.java
@@ -38,23 +38,18 @@ public class PrepAttachDataUtil {
List<Event> events = new ArrayList<Event>();
- InputStream in = PerceptronPrepAttachTest.class.getResourceAsStream("/data/ppa/" +
- filename);
-
- try {
+ try (InputStream in = PerceptronPrepAttachTest.class.getResourceAsStream("/data/ppa/" +
+ filename)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
String[] items = line.split("\\s+");
String label = items[5];
- String[] context = { "verb=" + items[1], "noun=" + items[2],
- "prep=" + items[3], "prep_obj=" + items[4] };
+ String[] context = {"verb=" + items[1], "noun=" + items[2],
+ "prep=" + items[3], "prep_obj=" + items[4]};
events.add(new Event(label, context));
}
}
- finally {
- in.close();
- }
return events;
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/RealValueModelTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/RealValueModelTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/RealValueModelTest.java
index 1050fd3..c34c1a8 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/RealValueModelTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/RealValueModelTest.java
@@ -28,19 +28,15 @@ public class RealValueModelTest extends TestCase {
public void testRealValuedWeightsVsRepeatWeighting() throws IOException {
GISModel realModel;
- RealValueFileEventStream rvfes1 = new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt");
- try {
- realModel = GIS.trainModel(100,new OnePassRealValueDataIndexer(rvfes1,1));
- } finally {
- rvfes1.close();
+ try (RealValueFileEventStream rvfes1 =
+ new RealValueFileEventStream("src/test/resources/data/opennlp/maxent/real-valued-weights-training-data.txt")) {
+ realModel = GIS.trainModel(100, new OnePassRealValueDataIndexer(rvfes1, 1));
}
GISModel repeatModel;
- FileEventStream rvfes2 = new FileEventStream("src/test/resources/data/opennlp/maxent/repeat-weighting-training-data.txt");
- try {
- repeatModel = GIS.trainModel(100,new OnePassRealValueDataIndexer(rvfes2,1));
- } finally {
- rvfes2.close();
+ try (FileEventStream rvfes2 =
+ new FileEventStream("src/test/resources/data/opennlp/maxent/repeat-weighting-training-data.txt")) {
+ repeatModel = GIS.trainModel(100, new OnePassRealValueDataIndexer(rvfes2, 1));
}
String[] features2Classify = new String[] {"feature2","feature5"};
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java b/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java
index b6d3041..34b0861 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java
@@ -50,15 +50,10 @@ public class POSDictionaryTest {
out.close();
}
- InputStream in = new ByteArrayInputStream(out.toByteArray());
-
POSDictionary serializedDictionary = null;
- try {
+ try (InputStream in = new ByteArrayInputStream(out.toByteArray())) {
serializedDictionary = POSDictionary.create(in);
}
- finally {
- in.close();
- }
return serializedDictionary;
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java
index daa6e77..b0a1500 100644
--- a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java
+++ b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/GeneratorFactoryTest.java
@@ -93,15 +93,11 @@ public class GeneratorFactoryTest {
*/
@Test(expected = IOException.class)
public void testCreationWithUnkownElement() throws IOException {
- InputStream descIn = getClass().getResourceAsStream(
- "/opennlp/tools/util/featuregen/FeatureGeneratorConfigWithUnkownElement.xml");
- try {
+ try (InputStream descIn = getClass().getResourceAsStream(
+ "/opennlp/tools/util/featuregen/FeatureGeneratorConfigWithUnkownElement.xml")) {
GeneratorFactory.create(descIn, null);
}
- finally {
- descIn.close();
- }
}
@Test
http://git-wip-us.apache.org/repos/asf/opennlp/blob/c91830ee/opennlp-uima/src/main/java/opennlp/uima/util/OpennlpUtil.java
----------------------------------------------------------------------
diff --git a/opennlp-uima/src/main/java/opennlp/uima/util/OpennlpUtil.java b/opennlp-uima/src/main/java/opennlp/uima/util/OpennlpUtil.java
index fb172ad..ccc5241 100644
--- a/opennlp-uima/src/main/java/opennlp/uima/util/OpennlpUtil.java
+++ b/opennlp-uima/src/main/java/opennlp/uima/util/OpennlpUtil.java
@@ -30,7 +30,6 @@ import org.apache.uima.resource.ResourceInitializationException;
import opennlp.tools.ml.TrainerFactory;
import opennlp.tools.ml.maxent.GISModel;
-import opennlp.tools.ml.model.TrainUtil;
import opennlp.tools.util.TrainingParameters;
import opennlp.tools.util.model.BaseModel;
@@ -51,35 +50,23 @@ final public class OpennlpUtil {
*/
public static void serialize(BaseModel model, File modelFile)
throws IOException {
- OutputStream modelOut = null;
-
- try {
- modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
+ try (OutputStream fileOut = new FileOutputStream(modelFile);
+ OutputStream modelOut = new BufferedOutputStream(fileOut)) {
model.serialize(modelOut);
- } finally {
- if (modelOut != null)
- modelOut.close();
}
}
public static final byte[] loadBytes(File inFile) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- InputStream in = null;
- try {
- in = new FileInputStream(inFile);
+ try (InputStream in = new FileInputStream(inFile)) {
byte buffer[] = new byte[1024];
int len;
-
while ((len = in.read(buffer)) > 0) {
bytes.write(buffer, 0, len);
}
}
- finally {
- if (in != null)
- in.close();
- }
return bytes.toByteArray();
}
@@ -89,21 +76,11 @@ final public class OpennlpUtil {
TrainingParameters params;
if (inFileValue != null) {
- InputStream paramsIn = null;
- try {
- paramsIn = new FileInputStream(new File(inFileValue));
-
+ try (InputStream paramsIn = new FileInputStream(new File(inFileValue))) {
params = new opennlp.tools.util.TrainingParameters(paramsIn);
} catch (IOException e) {
throw new ResourceInitializationException(e);
}
- finally {
- try {
- if (paramsIn != null)
- paramsIn.close();
- } catch (IOException e) {
- }
- }
if (!TrainerFactory.isValid(params.getSettings())) {
throw new ResourceInitializationException(new Exception("Training parameters file is invalid!"));