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 SummaryFields
- Constructor SummaryConstructors
- Method SummaryModifier and TypeMethodDescription- boolean- canTranslate- (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.- boolean- isLanguageHandled- (String languageCode) Verifies if a Language Code is handled by this translator.- void- setSubscriptionKey- (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_v3protected static boolean USE_v3Flag to use version 3.
 
- Constructor Details- AzureCognitiveServicespublic AzureCognitiveServices()
 
- Method Details- setSubscriptionKeyConstructs the TokenAccess instance based on the subsciption key.- Parameters:
- key- The subscription key.
- Throws:
- IllegalArgumentException- For invalid key.
 
- updateSupportedLanguagespublic 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 interface- ITranslator
 
- getLanguageCodeGets the "real" language code, no matter the input case.- Specified by:
- getLanguageCodein interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- The potentially changed language code if the case was wrong, or null if language is not supported.
 
- deconfigurepublic void deconfigure()Deconfigures the translator to "lose" the API key(s).- Specified by:
- deconfigurein interface- ITranslator
 
- getReadyStateGets the ready state of this translator.- Specified by:
- getReadyStatein interface- ITranslator
- Returns:
- An error message describing the state (e.g. "Missing API key"), or null for OK.
 
- getNameReturns the name of this translator.- Specified by:
- getNamein interface- ITranslator
- Returns:
- A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
 
- isLanguageHandledVerifies if a Language Code is handled by this translator.- Specified by:
- isLanguageHandledin interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- true if supported, false if not.
 
- getLanguageDescriptionGets the language description for a language code, e.g. "sv" would be "Swedish".- Specified by:
- getLanguageDescriptionin interface- ITranslator
- Parameters:
- languageCode- The language code.
- Returns:
- The language description the current Eclipse + IIZI language (English only for now).
 
- canTranslateChecks if a translation is handled between two language codes.- Specified by:
- canTranslatein interface- ITranslator
- Parameters:
- sourceLanguageCode- The source language code.
- targetLanguageCode- The target language code.
- Returns:
- true if supported, false if not.
 
- translatepublic Translation translate- (KString sourceText, String sourceLanguageCode, String targetLanguageCode) throws TranslationException, RequestException Translates a text in plain text or HTML.- Specified by:
- translatein interface- ITranslator
- 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.
 
- getLanguagesGets 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.
 
- detectLanguageDetects 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.