@@ -77,9 +77,45 @@ public enum ModelType {
77
77
System .getProperty ("OPENNLP_DOWNLOAD_BASE_URL" , "https://dlcdn.apache.org/opennlp/" );
78
78
private static final String MODEL_URI_PATH =
79
79
System .getProperty ("OPENNLP_DOWNLOAD_MODEL_PATH" , "models/ud-models-1.2/" );
80
+ private static final String OPENNLP_DOWNLOAD_HOME = "OPENNLP_DOWNLOAD_HOME" ;
80
81
81
82
private static Map <String , Map <ModelType , String >> availableModels ;
82
83
84
+ /**
85
+ * Checks if a model of the specified {@code modelType} has been downloaded already
86
+ * for a particular {@code language}.
87
+ *
88
+ * @param language The ISO language code of the requested model.
89
+ * @param modelType The {@link DownloadUtil.ModelType type} of model.
90
+ * @return {@code true} if a model exists locally, {@code false} otherwise.
91
+ * @throws IOException Thrown if IO errors occurred or the computed hash sum
92
+ * of an associated, local model file was incorrect.
93
+ */
94
+ static boolean existsModel (String language , ModelType modelType ) throws IOException {
95
+ Map <ModelType , String > modelsByLanguage = getAvailableModels ().get (language );
96
+ if (modelsByLanguage == null ) {
97
+ return false ;
98
+ } else {
99
+ final String url = modelsByLanguage .get (modelType );
100
+ if (url != null ) {
101
+ final Path homeDirectory = getDownloadHome ();
102
+ final String filename = url .substring (url .lastIndexOf ("/" ) + 1 );
103
+ final Path localFile = homeDirectory .resolve (filename );
104
+ boolean exists ;
105
+ if (Files .exists (localFile )) {
106
+ // if this does not throw the requested model is valid!
107
+ validateModel (new URL (url + ".sha512" ), localFile );
108
+ exists = true ;
109
+ } else {
110
+ exists = false ;
111
+ }
112
+ return exists ;
113
+ } else {
114
+ return false ;
115
+ }
116
+ }
117
+ }
118
+
83
119
/**
84
120
* Triggers a download for the specified {@link DownloadUtil.ModelType}.
85
121
*
@@ -94,7 +130,7 @@ public static <T extends BaseModel> T downloadModel(String language, ModelType m
94
130
Class <T > type ) throws IOException {
95
131
96
132
if (getAvailableModels ().containsKey (language )) {
97
- final String url = ( getAvailableModels ().get (language ).get (modelType ) );
133
+ final String url = getAvailableModels ().get (language ).get (modelType );
98
134
if (url != null ) {
99
135
return downloadModel (new URL (url ), type );
100
136
}
@@ -119,8 +155,7 @@ public static <T extends BaseModel> T downloadModel(String language, ModelType m
119
155
*/
120
156
public static <T extends BaseModel > T downloadModel (URL url , Class <T > type ) throws IOException {
121
157
122
- final Path homeDirectory = Paths .get (System .getProperty ("OPENNLP_DOWNLOAD_HOME" ,
123
- System .getProperty ("user.home" ))).resolve (".opennlp" );
158
+ final Path homeDirectory = getDownloadHome ();
124
159
125
160
if (!Files .isDirectory (homeDirectory )) {
126
161
try {
@@ -131,20 +166,17 @@ public static <T extends BaseModel> T downloadModel(URL url, Class<T> type) thro
131
166
}
132
167
133
168
final String filename = url .toString ().substring (url .toString ().lastIndexOf ("/" ) + 1 );
134
- final Path localFile = Paths . get ( homeDirectory .toString (), filename );
169
+ final Path localFile = homeDirectory .resolve ( filename );
135
170
136
171
if (!Files .exists (localFile )) {
137
- logger .debug ("Downloading model from {} to {}." , url , localFile );
172
+ logger .debug ("Downloading model to {}." , localFile );
138
173
139
174
try (final InputStream in = url .openStream ()) {
140
175
Files .copy (in , localFile , StandardCopyOption .REPLACE_EXISTING );
141
176
}
142
-
143
177
validateModel (new URL (url + ".sha512" ), localFile );
144
-
145
178
logger .debug ("Download complete." );
146
179
} else {
147
- System .out .println ("Model file already exists. Skipping download." );
148
180
logger .debug ("Model file '{}' already exists. Skipping download." , filename );
149
181
}
150
182
@@ -167,7 +199,7 @@ public static Map<String, Map<ModelType, String>> getAvailableModels() {
167
199
}
168
200
169
201
/**
170
- * Validates the downloaded model.
202
+ * Validates a downloaded model via the specified {@link Path downloadedModel path} .
171
203
*
172
204
* @param sha512 the url to get the sha512 hash
173
205
* @param downloadedModel the model file to check
@@ -187,8 +219,8 @@ private static void validateModel(URL sha512, Path downloadedModel) throws IOExc
187
219
// Validate SHA512 checksum
188
220
final String actualChecksum = calculateSHA512 (downloadedModel );
189
221
if (!actualChecksum .equalsIgnoreCase (expectedChecksum )) {
190
- throw new IOException ("SHA512 checksum validation failed. Expected: "
191
- + expectedChecksum + ", but got: " + actualChecksum );
222
+ throw new IOException ("SHA512 checksum validation failed for " + downloadedModel . getFileName () +
223
+ ". Expected: " + expectedChecksum + ", but got: " + actualChecksum );
192
224
}
193
225
}
194
226
@@ -198,6 +230,7 @@ private static String calculateSHA512(Path file) throws IOException {
198
230
try (InputStream fis = Files .newInputStream (file );
199
231
DigestInputStream dis = new DigestInputStream (fis , digest )) {
200
232
byte [] buffer = new byte [4096 ];
233
+ //noinspection StatementWithEmptyBody
201
234
while (dis .read (buffer ) != -1 ) {
202
235
// Reading the file to update the digest
203
236
}
@@ -217,6 +250,11 @@ private static String byteArrayToHexString(byte[] bytes) {
217
250
}
218
251
}
219
252
253
+ private static Path getDownloadHome () {
254
+ return Paths .get (System .getProperty (OPENNLP_DOWNLOAD_HOME ,
255
+ System .getProperty ("user.home" ))).resolve (".opennlp" );
256
+ }
257
+
220
258
@ Internal
221
259
static class DownloadParser {
222
260
0 commit comments