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 boolean
USE_v3
Flag to use version 3.
Constructor Summary
Constructors Constructor Description AzureCognitiveServices()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canTranslate(java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
Checks if a translation is handled between two language codes.void
deconfigure()
Deconfigures the translator to "lose" the API key(s).java.lang.String
detectLanguage(java.lang.String text)
Detects the language of a text using the Azure Cognitive Services.java.lang.String
getLanguageCode(java.lang.String languageCode)
Gets the "real" language code, no matter the input case.java.lang.String
getLanguageDescription(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.String
getName()
Returns the name of this translator.java.lang.String
getReadyState()
Gets the ready state of this translator.boolean
isLanguageHandled(java.lang.String languageCode)
Verifies if a Language Code is handled by this translator.void
setSubscriptionKey(java.lang.String key)
Constructs the TokenAccess instance based on the subsciption key.Translation
translate(KString sourceText, java.lang.String sourceLanguageCode, java.lang.String targetLanguageCode)
Translates a text in plain text or HTML.void
updateSupportedLanguages()
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.IllegalArgumentException
Constructs 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:
updateSupportedLanguages
in interfaceITranslator
getLanguageCode
public java.lang.String getLanguageCode(java.lang.String languageCode)
Gets the "real" language code, no matter the input case.- Specified by:
getLanguageCode
in 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:
deconfigure
in interfaceITranslator
getReadyState
public java.lang.String getReadyState()
Gets the ready state of this translator.- Specified by:
getReadyState
in 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:
getName
in 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:
isLanguageHandled
in 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:
getLanguageDescription
in 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:
canTranslate
in 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:
translate
in 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, RequestException
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
public java.lang.String detectLanguage(java.lang.String text) throws TranslationException, RequestException
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.