Class CachedTranslator
- java.lang.Object
-
- org.apache.tika.language.translate.AbstractTranslator
-
- org.apache.tika.language.translate.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 Summary
Constructors Constructor Description CachedTranslator()
Create a new CachedTranslator (must set theTranslator
withsetTranslator(Translator)
before use!)CachedTranslator(Translator translator)
Create a new CachedTranslator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.int
getNumTranslationPairs()
Get the number of different source/target translation pairs this CachedTranslator currently has in its cache.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.Translator
getTranslator()
boolean
isAvailable()
void
setTranslator(Translator translator)
String
translate(String text, String targetLanguage)
Translate text to the given language This method attempts to auto-detect the source language of the text.String
translate(String text, String sourceLanguage, String targetLanguage)
Translate text between given languages.-
Methods inherited from class org.apache.tika.language.translate.AbstractTranslator
detectLanguage
-
-
-
-
Constructor Detail
-
CachedTranslator
public CachedTranslator()
Create a new CachedTranslator (must set theTranslator
withsetTranslator(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 Detail
-
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.
-
-