Package com.iizix.translate.microsoft
Class AzureCognitiveServices
java.lang.Object
com.iizix.translate.microsoft.AzureCognitiveServices
- All Implemented Interfaces:
ITranslator
- Direct Known Subclasses:
AzureCognitiveServices,AzureCognitiveServicesTest
Azure Cognitive Services implementation to get supported Languages, detect language(s) and perform translations.
- Author:
- Christopher Mindus
Field Summary
FieldsConstructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionbooleancanTranslate(String sourceLanguageCode, String targetLanguageCode) Checks if a translation is handled between two language codes.voidDeconfigures the translator to "lose" the API key(s).detectLanguage(String text) Detects the language of a text using the Azure Cognitive Services.getLanguageCode(String languageCode) Gets the "real" language code, no matter the input case.getLanguageDescription(String languageCode) Gets the language description for a language code, e.g.String[]Gets the supported languages by the Azure Cognitive Services.getName()Returns the name of this translator.Gets the ready state of this translator.booleanisLanguageHandled(String languageCode) Verifies if a Language Code is handled by this translator.voidsetSubscriptionKey(String key) Constructs the TokenAccess instance based on the subsciption key.Translates a text in plain text or HTML.voidUpdates the translator with the supported languages by means of requesting the information from the service.
Field Details
USE_v3
protected static boolean USE_v3Flag to use version 3.
Constructor Details
AzureCognitiveServices
public AzureCognitiveServices()
Method Details
setSubscriptionKey
Constructs the TokenAccess instance based on the subsciption key.- Parameters:
key- The subscription key.- Throws:
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
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
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
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
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
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
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, String sourceLanguageCode, 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
Gets 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
Detects 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.