diff --git a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/geoinfo/GeographicInformationParser.java b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/geoinfo/GeographicInformationParser.java index ed8213d3c7..bc3a16257d 100644 --- a/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/geoinfo/GeographicInformationParser.java +++ b/tika-parsers/tika-parsers-extended/tika-parser-scientific-module/src/main/java/org/apache/tika/parser/geoinfo/GeographicInformationParser.java @@ -90,18 +90,14 @@ public Set getSupportedTypes(ParseContext parseContext) { public void parse(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, ParseContext parseContext) throws IOException, SAXException, TikaException { metadata.set(Metadata.CONTENT_TYPE, geoInfoType); - DataStore dataStore = null; - DefaultMetadata defaultMetadata = null; XHTMLContentHandler xhtmlContentHandler = new XHTMLContentHandler(contentHandler, metadata); TemporaryResources tmp = TikaInputStream.isTikaInputStream(inputStream) ? null : new TemporaryResources(); - try { - TikaInputStream tikaInputStream = TikaInputStream.get(inputStream, tmp, metadata); + try (TikaInputStream tikaInputStream = TikaInputStream.get(inputStream, tmp, metadata)) { File file = tikaInputStream.getFile(); - dataStore = DataStores.open(file); - defaultMetadata = new DefaultMetadata(dataStore.getMetadata()); - if (defaultMetadata != null) { + try (DataStore dataStore = DataStores.open(file)) { + DefaultMetadata defaultMetadata = new DefaultMetadata(dataStore.getMetadata()); extract(xhtmlContentHandler, metadata, defaultMetadata); } diff --git a/tika-parsers/tika-parsers-ml/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java b/tika-parsers/tika-parsers-ml/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java index 7f1c4fab76..125e4e8d53 100644 --- a/tika-parsers/tika-parsers-ml/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java +++ b/tika-parsers/tika-parsers-ml/tika-dl/src/main/java/org/apache/tika/dl/imagerec/DL4JInceptionV3Net.java @@ -261,15 +261,16 @@ public void initialize(Map params) throws TikaConfigException { LOG.info("Going to load Inception network..."); long st = System.currentTimeMillis(); - KerasModelBuilder builder = + try (KerasModelBuilder builder = new KerasModel().modelBuilder().modelHdf5Filename(modelWeightsPath) - .enforceTrainingConfig(false); + .enforceTrainingConfig(false)) { - builder.inputShape(new int[]{imgHeight, imgWidth, 3}); - KerasModel model = builder.buildModel(); - this.graph = model.getComputationGraph(); - long time = System.currentTimeMillis() - st; - LOG.info("Loaded the Inception model. Time taken={}ms", time); + builder.inputShape(new int[]{imgHeight, imgWidth, 3}); + KerasModel model = builder.buildModel(); + this.graph = model.getComputationGraph(); + long time = System.currentTimeMillis() - st; + LOG.info("Loaded the Inception model. Time taken={}ms", time); + } } catch (IOException | InvalidKerasConfigurationException | UnsupportedKerasConfigurationException e) { throw new TikaConfigException(e.getMessage(), e); diff --git a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/ner/grobid/GrobidNERecogniser.java b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/ner/grobid/GrobidNERecogniser.java index cf57cd6a04..6f25b52eb7 100644 --- a/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/ner/grobid/GrobidNERecogniser.java +++ b/tika-parsers/tika-parsers-ml/tika-parser-nlp-module/src/main/java/org/apache/tika/parser/ner/grobid/GrobidNERecogniser.java @@ -184,80 +184,81 @@ public Map> recognise(String text) { try { String url = restHostUrlStr + readRestEndpoint(); - Response response = + try (Response response = WebClient.create(url).accept(MediaType.APPLICATION_JSON) - .post("text=" + text); - int responseCode = response.getStatus(); - - if (responseCode == 200) { - String result = response.readEntity(String.class); - JSONObject jsonObject = convertToJSONObject(result); - JSONArray measurements = convertToJSONArray(jsonObject, "measurements"); - for (Object measurement : measurements) { - - StringBuffer measurementString = new StringBuffer(); - StringBuffer normalizedMeasurementString = new StringBuffer(); - - JSONObject quantity = (JSONObject) convertToJSONObject(measurement.toString()) - .get("quantity"); - if (quantity != null) { - if (quantity.containsKey("rawValue")) { - String measurementNumber = - (String) convertToJSONObject(quantity.toString()) - .get("rawValue"); - measurementString.append(measurementNumber); - measurementString.append(" "); - measurementNumberSet.add(measurementNumber); - } - - if (quantity.containsKey("normalizedQuantity")) { - String normalizedMeasurementNumber = - convertToJSONObject(quantity.toString()) - .get("normalizedQuantity").toString(); - normalizedMeasurementString.append(normalizedMeasurementNumber); - normalizedMeasurementString.append(" "); - } - - if (quantity.containsKey("type")) { - String measurementType = - (String) convertToJSONObject(quantity.toString()).get("type"); - measurementTypeSet.add(measurementType); - } - - JSONObject jsonObj = (JSONObject) convertToJSONObject(quantity.toString()); - if (jsonObj.containsKey("rawUnit")) { - JSONObject rawUnit = (JSONObject) jsonObj.get("rawUnit"); - String unitName = - (String) convertToJSONObject(rawUnit.toString()).get("name"); - unitSet.add(unitName); - measurementString.append(unitName); - } - - if (jsonObj.containsKey("normalizedUnit")) { - JSONObject normalizedUnit = (JSONObject) jsonObj.get("normalizedUnit"); - String normalizedUnitName = - (String) convertToJSONObject(normalizedUnit.toString()) - .get("name"); - normalizedMeasurementString.append(normalizedUnitName); - } - - if (!measurementString.toString().equals("")) { - measurementSet.add(measurementString.toString()); + .post("text=" + text)) { + int responseCode = response.getStatus(); + + if (responseCode == 200) { + String result = response.readEntity(String.class); + JSONObject jsonObject = convertToJSONObject(result); + JSONArray measurements = convertToJSONArray(jsonObject, "measurements"); + for (Object measurement : measurements) { + + StringBuffer measurementString = new StringBuffer(); + StringBuffer normalizedMeasurementString = new StringBuffer(); + + JSONObject quantity = (JSONObject) convertToJSONObject(measurement.toString()) + .get("quantity"); + if (quantity != null) { + if (quantity.containsKey("rawValue")) { + String measurementNumber = + (String) convertToJSONObject(quantity.toString()) + .get("rawValue"); + measurementString.append(measurementNumber); + measurementString.append(" "); + measurementNumberSet.add(measurementNumber); + } + + if (quantity.containsKey("normalizedQuantity")) { + String normalizedMeasurementNumber = + convertToJSONObject(quantity.toString()) + .get("normalizedQuantity").toString(); + normalizedMeasurementString.append(normalizedMeasurementNumber); + normalizedMeasurementString.append(" "); + } + + if (quantity.containsKey("type")) { + String measurementType = + (String) convertToJSONObject(quantity.toString()).get("type"); + measurementTypeSet.add(measurementType); + } + + JSONObject jsonObj = (JSONObject) convertToJSONObject(quantity.toString()); + if (jsonObj.containsKey("rawUnit")) { + JSONObject rawUnit = (JSONObject) jsonObj.get("rawUnit"); + String unitName = + (String) convertToJSONObject(rawUnit.toString()).get("name"); + unitSet.add(unitName); + measurementString.append(unitName); + } + + if (jsonObj.containsKey("normalizedUnit")) { + JSONObject normalizedUnit = (JSONObject) jsonObj.get("normalizedUnit"); + String normalizedUnitName = + (String) convertToJSONObject(normalizedUnit.toString()) + .get("name"); + normalizedMeasurementString.append(normalizedUnitName); + } + + if (!measurementString.toString().equals("")) { + measurementSet.add(measurementString.toString()); + } + + if (!normalizedMeasurementString.toString().equals("")) { + normalizedMeasurementSet.add(normalizedMeasurementString.toString()); + } } - if (!normalizedMeasurementString.toString().equals("")) { - normalizedMeasurementSet.add(normalizedMeasurementString.toString()); - } } - } - - entities.put("MEASUREMENT_NUMBERS", measurementNumberSet); - entities.put("MEASUREMENT_UNITS", unitSet); - entities.put("MEASUREMENTS", measurementSet); - entities.put("NORMALIZED_MEASUREMENTS", normalizedMeasurementSet); - entities.put("MEASUREMENT_TYPES", measurementTypeSet); + entities.put("MEASUREMENT_NUMBERS", measurementNumberSet); + entities.put("MEASUREMENT_UNITS", unitSet); + entities.put("MEASUREMENTS", measurementSet); + entities.put("NORMALIZED_MEASUREMENTS", normalizedMeasurementSet); + entities.put("MEASUREMENT_TYPES", measurementTypeSet); + } } } catch (Exception e) { LOG.info(e.getMessage(), e);