目录

Before API development, you need to make necessary development preparations, ensure that the Maven repository address of the HMS Core SDK has been configured in your project, and the SDK of this service has been integrated.

Language Detection on the Device

1. Create a language detector. You can create the detector using the MLRemoteLangDetectorSetting class.

  1. // Create a local language detector.
  2. MLLangDetectorFactory factory = MLLangDetectorFactory.getInstance();
  3. MLLocalLangDetectorSetting setting = new MLLocalLangDetectorSetting.Factory()
  4.   // Set the minimum confidence threshold for language detection.
  5.    .setTrustedThreshold(0.01f)
  6.    .create();
  7. MLLocalLangDetector mlLocalLangDetector = factory.getLocalLangDetector(setting);

2. Implement language detection.

  1. // Method 1: Return multiple language detection results, including the language codes and confidences. sourceText indicates the text to be detected, with up to 5000 characters.
  2. Task<List<MLDetectedLang>> probabilityDetectTask = mlLocalLangDetector.probabilityDetect(sourceText); // sourceText: input text string.
  3.    probabilityDetectTask.addOnSuccessListener(new OnSuccessListener<List<MLDetectedLang>>() {
  4.     @Override
  5.      public void onSuccess(List<MLDetectedLang> mlDetectedLangs) {
  6.             // Called when language detection is successful.
  7.        }
  8.    }).addOnFailureListener(new OnFailureListener() {
  9.        @Override
  10.        public void onFailure(Exception e) {
  11.           // Called when language detection fails.
  12.     }
  13. });
  14. // Method 2: Return the code of the language with the highest confidence. sourceText indicates the text to be detected, with up to 5000 characters.
  15. Task<String> firstBestDetectTask = mlLocalLangDetector.firstBestDetect(sourceText); // sourceText: input text string.
  16. firstBestDetectTask.addOnSuccessListener(new OnSuccessListener<String>() {
  17.    @Override
  18.    public void onSuccess(String s) {
  19.       // Processing logic for detection success.
  20.    }
  21. }).addOnFailureListener(new OnFailureListener() {
  22.    @Override
  23.    public void onFailure(Exception e) {
  24.      // Processing logic for detection failure.
  25.    }
  26. });

3. Release resources after the detection is complete.

  1. if (mlRemoteLangDetector != null) {
  2.    mlRemoteLangDetector.stop();
  3. }

Language Detection on the Cloud

1. Create a language detector. You can create the detector using the MLRemoteLangDetectorSetting class.

  1. // Method 1: Use default parameter settings to create a language detector.
  2. MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
  3.    .getRemoteLangDetector();
  4. // Method 2: Use customized parameter settings to create a language detector.
  5. MLRemoteLangDetectorSetting setting = new MLRemoteLangDetectorSetting.Factory()
  6.   // Set the minimum confidence threshold for language detection.
  7.    .setTrustedThreshold(0.01f)
  8.    .create();
  9. MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
  10.    .getRemoteLangDetector(setting);

2. Implement language detection.

  1. // Method 1: Return multiple language detection results, including the language codes and confidences. sourceText indicates the text to be detected, with up to 5000 characters.
  2. Task<List<MLDetectedLang>> probabilityDetectTask = mlRemoteLangDetector.probabilityDetect(sourceText);
  3. probabilityDetectTask.addOnSuccessListener(new OnSuccessListener<List<MLDetectedLang>>() {
  4.    @Override
  5.    public void onSuccess(List<MLDetectedLang> result) {
  6.       // Processing logic for detection success.
  7.    }
  8. }).addOnFailureListener(new OnFailureListener() {
  9.    @Override
  10.    public void onFailure(Exception e) {
  11.      // Processing logic for detection failure.
  12.        // Recognition failure.
  13.        try {
  14.            MLException mlException = (MLException)e;
  15.    // Obtain the result codes. You can process the result codes and customize respective messages displayed to users. For details about the result codes, please refer to Result Codes of ML Kit.
  16.            int errorCode = mlException.getErrCode();
  17.            // Obtain the error information. You can quickly locate the fault based on the result code.
  18.            String errorMessage = mlException.getMessage();
  19.        } catch (Exception error) {
  20.           // Handle the conversion error.
  21.        }  
  22.    }
  23. });
  24. // Method 2: Return the code of the language with the highest confidence. sourceText indicates the text to be detected, with up to 5000 characters.
  25. Task<String> firstBestDetectTask = mlRemoteLangDetector.firstBestDetect(sourceText);
  26. firstBestDetectTask.addOnSuccessListener(new OnSuccessListener<String>() {
  27.    @Override
  28.    public void onSuccess(String s) {
  29.       // Processing logic for detection success.
  30.    }
  31. }).addOnFailureListener(new OnFailureListener() {
  32.    @Override
  33.    public void onFailure(Exception e) {
  34.      // Processing logic for detection failure.
  35.        // Recognition failure.
  36.        try {
  37.            MLException mlException = (MLException)e;
  38.    // Obtain the result codes. You can process the result codes and customize respective messages displayed to users. For details about the result codes, please refer to Result Codes of ML Kit.
  39.            int errorCode = mlException.getErrCode();
  40.            // Obtain the error information. You can quickly locate the fault based on the result code.
  41.            String errorMessage = mlException.getMessage();
  42.        } catch (Exception error) {
  43.           // Handle the conversion error.
  44.        }
  45.    }
  46. });

3. Release resources after the detection is complete.

  1. if (mlRemoteLangDetector != null) {
  2.    mlRemoteLangDetector.stop();
  3. }