Package com.iizix.translate.microsoft
Class AzureCognitiveServices
- java.lang.Object
- com.iizix.translate.microsoft.AzureCognitiveServices
- All Implemented Interfaces:
ITranslator
- Direct Known Subclasses:
AzureCognitiveServices,AzureCognitiveServicesTest
public abstract class AzureCognitiveServices extends java.lang.Object implements ITranslator
Azure Cognitive Services implementation to get supported Languages, detect language(s) and perform translations.- Author:
- Christopher Mindus
Field Summary
Fields Modifier and Type Field Description protected static booleanUSE_v3Flag to use version 3.
Constructor Summary
Constructors Constructor Description AzureCognitiveServices()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanTranslate(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)Checks if a translation is handled between two language codes.voiddeconfigure()Deconfigures the translator to "lose" the API key(s).java.lang.StringdetectLanguage(java.lang.String text)Detects the language of a text using the Azure Cognitive Services.java.lang.StringgetLanguageCode(java.lang.String languageCode)Gets the "real" language code, no matter the input case.java.lang.StringgetLanguageDescription(java.lang.String languageCode)Gets the language description for a language code, e.g.java.lang.String[]getLanguages()Gets the supported languages by the Azure Cognitive Services.java.lang.StringgetName()Returns the name of this translator.java.lang.StringgetReadyState()Gets the ready state of this translator.booleanisLanguageHandled(java.lang.String languageCode)Verifies if a Language Code is handled by this translator.voidsetSubscriptionKey(java.lang.String key)Constructs the TokenAccess instance based on the subsciption key.Translationtranslate(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)Translates a text in plain text or HTML.voidupdateSupportedLanguages()Updates the translator with the supported languages by means of requesting the information from the service.
Method Detail
setSubscriptionKey
public void setSubscriptionKey(java.lang.String key) throws java.lang.IllegalArgumentExceptionConstructs the TokenAccess instance based on the subsciption key.- Parameters:
key- The subscription key.- Throws:
java.lang.IllegalArgumentException- For invalid key.
updateSupportedLanguages
public void updateSupportedLanguages()
Updates the translator with the supported languages by means of requesting the information from the service. If the request fails, it will be logged in the server.- Specified by:
updateSupportedLanguagesin interfaceITranslator
getLanguageCode
public java.lang.String getLanguageCode(java.lang.String languageCode)
Gets the "real" language code, no matter the input case.- Specified by:
getLanguageCodein interfaceITranslator- Parameters:
languageCode- The language code.- Returns:
- The potentially changed language code if the case was wrong, or null if language is not supported.
deconfigure
public void deconfigure()
Deconfigures the translator to "lose" the API key(s).- Specified by:
deconfigurein interfaceITranslator
getReadyState
public java.lang.String getReadyState()
Gets the ready state of this translator.- Specified by:
getReadyStatein interfaceITranslator- Returns:
- An error message describing the state (e.g. "Missing API key"), or null for OK.
getName
public java.lang.String getName()
Returns the name of this translator.- Specified by:
getNamein interfaceITranslator- Returns:
- A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
isLanguageHandled
public boolean isLanguageHandled(java.lang.String languageCode)
Verifies if a Language Code is handled by this translator.- Specified by:
isLanguageHandledin interfaceITranslator- Parameters:
languageCode- The language code.- Returns:
- true if supported, false if not.
getLanguageDescription
public java.lang.String getLanguageDescription(java.lang.String languageCode)
Gets the language description for a language code, e.g. "sv" would be "Swedish".- Specified by:
getLanguageDescriptionin interfaceITranslator- Parameters:
languageCode- The language code.- Returns:
- The language description the current Eclipse + IIZI language (English only for now).
canTranslate
public boolean canTranslate(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)Checks if a translation is handled between two language codes.- Specified by:
canTranslatein interfaceITranslator- Parameters:
sourceLanguageCode- The source language code.targetLanguageCode- The target language code.- Returns:
- true if supported, false if not.
translate
public Translation translate(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode) throws TranslationException, RequestException
Translates a text in plain text or HTML.- Specified by:
translatein interfaceITranslator- Parameters:
sourceText- The source text to translate.sourceLanguageCode- The source language code, null for automatic detection.targetLanguageCode- The target language code.- Returns:
- The translation.
- Throws:
RequestException- If the translation web service failed.TranslationException- An exception for translation failure.
getLanguages
public java.lang.String[] getLanguages() throws TranslationException, RequestExceptionGets the supported languages by the Azure Cognitive Services. This is a Service Request that takes a while to execute.- Returns:
- The array of supported language codes.
- Throws:
RequestException- If the translation web service failed.TranslationException- An exception for translation failure.
detectLanguage
public java.lang.String detectLanguage(java.lang.String text) throws TranslationException, RequestExceptionDetects the language of a text using the Azure Cognitive Services.- Parameters:
text- Text to detect the language.- Returns:
- The detected language with the highest score.
- Throws:
RequestException- If the translation web service failed.TranslationException- An exception for translation failure.