Class CachedTranslator

java.lang.Object
org.apache.tika.language.translate.impl.AbstractTranslator
org.apache.tika.language.translate.impl.CachedTranslator
All Implemented Interfaces:
Translator

public class CachedTranslator extends AbstractTranslator
CachedTranslator. Saves a map of previous translations in order to prevent repetitive translation requests.
  • Constructor Details

    • CachedTranslator

      public CachedTranslator()
      Create a new CachedTranslator (must set the Translator with setTranslator(Translator) before use!)
    • CachedTranslator

      public CachedTranslator(Translator translator)
      Create a new CachedTranslator.
      Parameters:
      translator - The translator that should be used for the underlying translation service. The properties for that service must be set properly!
  • Method Details

    • getTranslator

      public Translator getTranslator()
      Returns:
      the translator
    • setTranslator

      public void setTranslator(Translator translator)
      Parameters:
      translator - the translator to set
    • translate

      public String translate(String text, String sourceLanguage, String targetLanguage) throws TikaException, IOException
      Description copied from interface: Translator
      Translate text between given languages.
      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
    • translate

      public String translate(String text, String targetLanguage) throws TikaException, IOException
      Description copied from interface: Translator
      Translate text to the given language This method attempts to auto-detect the source language of the text.
      Parameters:
      text - The text to translate.
      targetLanguage - The desired language to translate to (for example, "hi").
      Returns:
      The translation result. If translation is unavailable, returns the same text back.
      Throws:
      TikaException - When there is an error translating.
      IOException
    • isAvailable

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

      public int getNumTranslationPairs()
      Get the number of different source/target translation pairs this CachedTranslator currently has in its cache.
      Returns:
      Number of translation source/target pairs in this CachedTranslator's cache.
      Since:
      Tika 1.6
    • getNumTranslationsFor

      public int getNumTranslationsFor(String sourceLanguage, String targetLanguage)
      Get the number of different translations from the source language to the target language this CachedTranslator has in its cache.
      Parameters:
      sourceLanguage - The source language of translation.
      targetLanguage - The target language of translation.
      Returns:
      The number of translations between source and target.
      Since:
      Tika 1.6
    • contains

      public boolean contains(String text, String sourceLanguage, String targetLanguage)
      Check whether this CachedTranslator's cache contains a translation of the text from the source language to the target language.
      Parameters:
      text - What string to check for.
      sourceLanguage - The source language of translation.
      targetLanguage - The target language of translation.
      Returns:
      true if the cache contains a translation of the text, false otherwise.
    • contains

      public boolean contains(String text, String targetLanguage)
      Check whether this CachedTranslator's cache contains a translation of the text to the target language, attempting to auto-detect the source language.
      Parameters:
      text - What string to check for.
      targetLanguage - The target language of translation.
      Returns:
      true if the cache contains a translation of the text, false otherwise.