You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tika.apache.org by GitBox <gi...@apache.org> on 2021/04/13 03:38:14 UTC

[GitHub] [tika] thammegowda commented on a change in pull request #419: fix for TIKA-3329 contributed by Thamme Gowda

thammegowda commented on a change in pull request #419:
URL: https://github.com/apache/tika/pull/419#discussion_r612103941



##########
File path: tika-translate/src/main/java/org/apache/tika/language/translate/RTGTranslator.java
##########
@@ -0,0 +1,137 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.tika.language.translate;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.tika.exception.TikaException;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+
+/**
+ * <p>This translator is designed to work with a TCP-IP available
+ * RTG translation server, specifically the
+ * <a href="https://isi-nlp.github.io/rtg/#_rtg_serve">
+ * REST-based RTG server</a>.</p>
+ * To get Docker image:
+ *   https://hub.docker.com/repository/docker/tgowda/rtg-model <br/>
+ * <pre>
+ * {code
+ * # without GPU
+ *   docker run --rm -i -p 6060:6060 tgowda/rtg-model:500toEng-v1
+ * # Or, with GPU device 0
+ *   docker run --rm -i -p 6060:6060 --gpus '"device=0"' tgowda/rtg-model:500toEng-v1
+ * }
+ * </pre>
+ *
+ * <p>If you were to interact with the server via curl a request
+ * would look as follows</p>
+ *
+ * <pre>
+ * {code
+ * curl --data "source=Comment allez-vous?" \
+ *      --data "source=Bonne journée" \
+ *      http://localhost:6060/translate
+ * }
+ * </pre>
+ *
+ * RTG requires input to be pre-formatted into sentences, one per line,
+ * so this translation implementation takes care of that.
+ */
+public class RTGTranslator extends AbstractTranslator {
+
+    public static final String RTG_TRANSLATE_URL_BASE = "http://localhost:6060";
+    public static final String RTG_PROPS = "translator.rtg.properties";
+    private static final Logger LOG = LoggerFactory.getLogger(RTGTranslator.class);
+    private WebClient client;
+    private boolean isAvailable = false;
+
+    public RTGTranslator() {
+        String rtgBaseUrl = RTG_TRANSLATE_URL_BASE;
+        Properties config = new Properties();
+        try (InputStream stream = getClass().getResourceAsStream(RTG_PROPS)){

Review comment:
       @kieraCurtis Thanks for the comment. Agreed, this was a problematic statement, and hence I have already changed it to `getClass().getClassLoader()` in https://github.com/apache/tika/pull/419/commits/9068145e48e53f5e355671abdce47d993e7f790c . 
   The behaviour of `getClass().getClassLoader().getResource*` is going to be same regardless of package of class. The only exceptional case is if classloaders are swapped between parents and children classes e.g. plugin systems that isolate classpaths for each plugin. Since Tika system is not messing with its classloaders; all classes have the same classloader, and hence my revised statement is NOT BAD_PRACTICE. 
   
   That being said, I do not know better, safer, more robust alternatives; if there are suggestions, I am happy to revise my code :) 
    
   
   P.S. 
   1.  Last time I wrote java code was 4+ years ago, this thing had slipped in by mistake and I immediately corrected it. 
   1. The code you are reviewing/commenting is outdated. 
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org