We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core ReferencesML Kitmlsdk.langdetectmlsdk.langdetect.cloudMLRemoteLangDetector

MLRemoteLangDetector

class info
com.huawei.hms.mlsdk.langdetect.cloud.MLRemoteLangDetector
Language detector, which is used to perform language detection on supplied text. The on-cloud API is called, and the detection model runs on the cloud. You can create a language detector in two ways based on the site requirements.

Sample Code

Collapse
Word wrap
Dark theme
Copy code
  1. // Obtain the language detector.
  2. // Method 1: Use default parameter settings.
  3. MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
  4. .getRemoteLangDetector();
  5. // Method 2: Use customized parameter settings.
  6. // setTrustedThreshold: Set the minimum confidence threshold for language detection.
  7. MLRemoteLangDetectorSetting mlRemoteLangDetectorSetting = new MLRemoteLangDetectorSetting.Factory()
  8. .setTrustedThreshold(0.01f)
  9. .create();
  10. MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
  11. .getRemoteLangDetector(mlRemoteLangDetectorSetting);
  12. // Return the detection results of multiple languages, including language codes and confidences, sourceText: text to be detected.
  13. Task<List<MLDetectedLang>> probabilityDetectTask = mlRemoteLangDetector.probabilityDetect(sourceText);
  14. probabilityDetectTask.addOnSuccessListener(new OnSuccessListener<List<MLDetectedLang>>() {
  15.     public void onSuccess(List<MLDetectedLang> result) {
  16.         // Processing logic for success.
  17.     }
  18. }).addOnFailureListener(new OnFailureListener() {
  19.     public void onFailure(Exception e) {
  20.         // Processing logic for failure.
  21.     }
  22. });
  23. // Return the language code with the highest confidence, sourceText: text to be detected.
  24. Task<String> firstBestDetectTask = mlRemoteLangDetector.firstBestDetect(sourceText);
  25. firstBestDetectTask.addOnSuccessListener(new OnSuccessListener<String>() {
  26.     public void onSuccess(String s) {
  27.         // Processing logic for success.
  28.     }
  29. }).addOnFailureListener(new OnFailureListener() {
  30.     public void onFailure(Exception e) {
  31.         // Processing logic for failure.
  32.     }
  33. });
  34. // Release the resources.
  35. mlRemoteLangDetector.stop();

Constant Summary

TypeNameDesc
floatFIRST_BEST_DETECTION_LANGUAGE_TRUSTED_THRESHOLDConfidence threshold in the scenario where the language detection result with the highest confidence is returned.
floatPROBABILITY_DETECTION_LANGUAGE_TRUSTED_THRESHOLDConfidence threshold in the scenario where detection results of multiple languages are returned.
StringUNDETECTION_LANGUAGE_TRUSTED_THRESHOLDUndetected language code.

Public Method Summary

Return typeMethod name
MLRemoteLangDetectorcreate(MLApplication app, MLRemoteLangDetectorSetting setting)
com.huawei.hmf.tasks.TaskfirstBestDetect(String text)
com.huawei.hmf.tasks.TaskprobabilityDetect(String text)
voidstop()

Constants

Constant
public static final float FIRST_BEST_DETECTION_LANGUAGE_TRUSTED_THRESHOLD
Confidence threshold in the scenario where the language detection result with the highest confidence is returned.
Constant value: 0.5f
Constant
public static final float PROBABILITY_DETECTION_LANGUAGE_TRUSTED_THRESHOLD
Confidence threshold in the scenario where detection results of multiple languages are returned.
Constant value: 0.01f
Constant
public static final String UNDETECTION_LANGUAGE_TRUSTED_THRESHOLD
Undetected language code.
Constant value: unknown

Public Methods

Method
public static synchronized MLRemoteLangDetector create(MLApplication app, MLRemoteLangDetectorSetting setting)
Creates a language detector object.

Parameters

Parameter nameParameter desc
appMLApplication instance.
settingInstance of the language detection configurator.

Return

Typedesc
MLRemoteLangDetectorReturns a language detector object.
Method
public com.huawei.hmf.tasks.TaskfirstBestDetect(String text)
Returns the language detection result with the highest confidence based on the supplied text.

Parameters

Parameter nameParameter desc
textText.

Return

Typedesc
com.huawei.hmf.tasks.TaskLanguage detection result with the highest confidence.
Method
public com.huawei.hmf.tasks.Task<list> probabilityDetect(String text)
Returns multi-language detection results based on the supplied text.

Parameters

Parameter nameParameter desc
textText.

Return

Typedesc
com.huawei.hmf.tasks.Task<list>Task object of the multi-language detection results.
Method
public void stop()
Releases resources, including input and output streams.
This page may contain third-party content. For details, click here.
Search in References
Enter a keyword.