When users need cross-language communication or content translation, the application needs to be able to automatically detect the language of the text before translating.
The language detection service of the HMS Core machine learning service provides online language detection and offline language detection . It supports detection of both single-language text and mixed-language text, covering hundreds of languages such as Afrikaans and Arabic. Accessing the language detection service, the app can easily implement language detection for translation, web page language detection, and language detection in mixed language scenarios, helping the app to improve user experience.
Language detection process
Input text, the machine learning service language will automatically detect the language of the text, and then return the corresponding language code and corresponding confidence, or return a language code with the highest confidence to the developer's application. Finally, the translated text is presented on the development app page.
Effect demonstration
1. Development preparation
The Maven warehouse address of the HMS Core SDK needs to be configured before development
repositories {
maven {
url'https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/' }
}
Integrated online language detection service SDK, the sample code is as follows
dependencies{
implementation 'com.huawei.hms:ml-computer-language-detection:3.4.0.301'
}
2. Edit project integration
2.1 Set the authentication information of the application
可以通过api_key或者Access Token来设置应用鉴权信息。
通过setAccessToken方法设置Access Token,在应用启动时初始化设置一次即可,无需多次设置。
MLApplication.getInstance().setAccessToken("your access token");
通过setApiKey方法设置api_key,在应用启动时初始化设置一次即可,无需多次设置。
MLApplication.getInstance().setApiKey("your ApiKey");
2.2 Create language detector
// 方式一:使用默认的参数配置创建语种检测器。
MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
.getRemoteLangDetector();
// 方式二:使用自定义的参数配置创建语种检测器。
MLRemoteLangDetectorSetting setting = new MLRemoteLangDetectorSetting.Factory()
// 设置语种检测的最低置信度阈值。
.setTrustedThreshold(0.01f)
.create();
MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
.getRemoteLangDetector(setting);
2.3 Perform language detection
Asynchronous method sample code
// 方式一:返回多个语种检测结果,包括语种编码以及置信度,sourceText表示待检测的文本,长度需小于5000个字符。
Task<List<MLDetectedLang>> probabilityDetectTask = mlRemoteLangDetector.probabilityDetect(sourceText);
probabilityDetectTask.addOnSuccessListener(new OnSuccessListener<List<MLDetectedLang>>() {
@Override
public void onSuccess(List<MLDetectedLang> result) {
// 成功的处理逻辑。
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// 失败的处理逻辑。
// Recognition failure.
try {
MLException mlException = (MLException)e;
// 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
int errorCode = mlException.getErrCode();
// 获取报错信息,开发者可以结合错误码,快速定位问题。
String errorMessage = mlException.getMessage();
} catch (Exception error) {
// 转换错误处理。
}
}
});
// 方式二:返回置信度最高的语种编码,sourceText表示待检测的文本,长度需小于5000个字符。
Task<String> firstBestDetectTask = mlRemoteLangDetector.firstBestDetect(sourceText);
firstBestDetectTask.addOnSuccessListener(new OnSuccessListener<String>() {
@Override
public void onSuccess(String s) {
// 成功的处理逻辑。
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// 失败的处理逻辑。
// Recognition failure.
try {
MLException mlException = (MLException)e;
// 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
int errorCode = mlException.getErrCode();
// 获取报错信息,开发者可以结合错误码,快速定位问题。
String errorMessage = mlException.getMessage();
} catch (Exception error) {
// 转换错误处理。
}
}
});
Synchronized method sample code
// 方式一:返回多个语种检测结果,包括语种编码以及置信度,sourceText表示待检测的文本,长度需小于5000个字符。
try {
List<MLDetectedLang> result= mlRemoteLangDetector.syncProbabilityDetect(sourceText);
} catch (MLException mlException) {
// 失败的处理逻辑。
// 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
int errorCode = mlException.getErrCode();
// 获取报错信息,开发者可以结合错误码,快速定位问题。
String errorMessage = mlException.getMessage();
}
// 方式二:返回置信度最高的语种编码,sourceText表示待检测的文本,长度需小于5000个字符。
try {
String language = mlRemoteLangDetector.syncFirstBestDetect(sourceText);
} catch (MLException mlException) {
// 失败的处理逻辑。
// 获取错误码,开发者可以对错误码进行处理,根据错误码进行差异化的页面提示。
int errorCode = mlException.getErrCode();
// 获取报错信息,开发者可以结合错误码,快速定位问题。
String errorMessage = mlException.getMessage();
}
2.4 After the detection is completed, release the resources.
if (mlRemoteLangDetector != null) {
mlRemoteLangDetector.stop();
}
The language detection function includes the device side and the cloud side. The above example is only the cloud side function.
Learn more details>>
Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee
Follow us to know the latest technical information of HMS Core for the first time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。