Class AzureCognitiveServices

java.lang.Object
com.iizix.translate.microsoft.AzureCognitiveServices
All Implemented Interfaces:
ITranslator
Direct Known Subclasses:
AzureCognitiveServices, AzureCognitiveServicesTest

public abstract class AzureCognitiveServices extends Object implements ITranslator
Azure Cognitive Services implementation to get supported Languages, detect language(s) and perform translations.
Author:
Christopher Mindus
  • Field Details

    • USE_v3

      protected static boolean USE_v3
      Flag to use version 3.
  • Constructor Details

    • AzureCognitiveServices

      public AzureCognitiveServices()
  • Method Details

    • setSubscriptionKey

      public void setSubscriptionKey(String key) throws IllegalArgumentException
      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:
      updateSupportedLanguages in interface ITranslator
    • getLanguageCode

      public String getLanguageCode(String languageCode)
      Gets the "real" language code, no matter the input case.
      Specified by:
      getLanguageCode in 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.
    • deconfigure

      public void deconfigure()
      Deconfigures the translator to "lose" the API key(s).
      Specified by:
      deconfigure in interface ITranslator
    • getReadyState

      public String getReadyState()
      Gets the ready state of this translator.
      Specified by:
      getReadyState in interface ITranslator
      Returns:
      An error message describing the state (e.g. "Missing API key"), or null for OK.
    • getName

      public String getName()
      Returns the name of this translator.
      Specified by:
      getName in interface ITranslator
      Returns:
      A String with the name of the translator, e.g. "Google Translate" or "Azure Cognitive Services".
    • isLanguageHandled

      public boolean isLanguageHandled(String languageCode)
      Verifies if a Language Code is handled by this translator.
      Specified by:
      isLanguageHandled in interface ITranslator
      Parameters:
      languageCode - The language code.
      Returns:
      true if supported, false if not.
    • getLanguageDescription

      public String getLanguageDescription(String languageCode)
      Gets the language description for a language code, e.g. "sv" would be "Swedish".
      Specified by:
      getLanguageDescription in interface ITranslator
      Parameters:
      languageCode - The language code.
      Returns:
      The language description the current Eclipse + IIZI language (English only for now).
    • canTranslate

      public boolean canTranslate(String sourceLanguageCode, String targetLanguageCode)
      Checks if a translation is handled between two language codes.
      Specified by:
      canTranslate in interface ITranslator
      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:
      translate in 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.
    • getLanguages

      public 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 String detectLanguage(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.