Class JoshuaNetworkTranslator

  • All Implemented Interfaces:
    Translator

    public class JoshuaNetworkTranslator
    extends AbstractTranslator

    This translator is designed to work with a TCP-IP available Joshua translation server, specifically the REST-based Joshua server.

    If you were to interact with the server via curl a request would look as follows

     {code
     curl http://localhost:5000/joshua/translate/english \
       -i -H "Content-Type: application/json" \
       -X POST -d '{"inputLanguage": "Spanish", "inputText": "vuelo"}' -v
     }
     

    Joshua requires input to be pre-formatted into sentences, one per line, so this translation implementation takes care of that.

    • Constructor Detail

      • JoshuaNetworkTranslator

        public JoshuaNetworkTranslator()
        Default constructor which first checks for the presence of the translator.joshua.properties file. We check if the remote server is available on each translation process. This check is not a remote call, but instead a check for null value within of a local variable represetning the value for joshua.server.url, which should be populated within the translator.joshua.properties file.
    • Method Detail

      • translate

        public String translate​(String text,
                                String sourceLanguage,
                                String targetLanguage)
                         throws TikaException,
                                IOException

        Initially then check if the source language has been provided. If no source language (or a null value) has been provided then we make an attempt to guess the source using Tika's org.apache.tika.langdetect.OptimaizeLangDetector. If we are still unable to guess the language then we return the source text.

        We then process the input text into a new string consisting of sentences, one per line e.g. insert \n between the presence of '.'

        Parameters:
        text - The text to translate.
        sourceLanguage - The input text language (for example, "en").
        targetLanguage - The desired language to translate to (for example, "fr").
        Returns:
        The translation result. If translation is unavailable, returns the same text back.
        Throws:
        TikaException - When there is an error translating.
        IOException
        See Also:
        (java.lang.String, java.lang.String, java.lang.String)
      • isAvailable

        public boolean isAvailable()
        Returns:
        true if this Translator is probably able to translate right now.
        See Also:
        Translator.isAvailable()