5

机器学习服务助应用内文本语种在线和离线检测

 2 years ago
source link: https://blog.51cto.com/u_14815220/5377231
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

当用户有跨语种交流或内容翻译的需求时,应用需要能自动检测文本的语种再进行翻译。

HMS Core机器学习服务,既支持检测单语种文本,也支持检测混合语种文本,涵盖南非荷兰语、阿拉伯语等百种语言。接入语种检测服务,App可以轻松实现翻译语种检测、网页语种检测,混合语种场景中语种检测等,帮助App提升用户体验。

语种检测流程

输入文本,机器学习服务语种对该文本自动进行语种进行检测,然后返回对应语种编码和相应的置信度,或者返回一个置信度最高的语种编码给开发者的应用。最终在开发app页面呈现出翻译出的文本。

机器学习服务助应用内文本语种在线和离线检测_ide
机器学习服务助应用内文本语种在线和离线检测_开发者_02

1.开发准备

在进行开发前需要配置HMS Core SDK的Maven仓地址

repositories {
maven {
url'https://cmc.centralrepo.rnd.huawei.com/artifactory/product_maven/' }
}

集成在线语种检测服务SDK,示例代码如下

dependencies{
implementation 'com.huawei.hms:ml-computer-language-detection:3.4.0.301'
}

2. 编辑工程集成

2.1设置应用的鉴权信息

可以通过api_key或者Access Token来设置应用鉴权信息。

通过setAccessToken方法设置Access Token,在应用启动时初始化设置一次即可,无需多次设置。
MLApplication.getInstance().setAccessToken("your access token");

通过setApiKey方法设置api_key,在应用启动时初始化设置一次即可,无需多次设置。
MLApplication.getInstance().setApiKey("your ApiKey");

2.2 创建语种检测器

// 方式一:使用默认的参数配置创建语种检测器。
MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
.getRemoteLangDetector();
// 方式二:使用自定义的参数配置创建语种检测器。
MLRemoteLangDetectorSetting setting = new MLRemoteLangDetectorSetting.Factory()
// 设置语种检测的最低置信度阈值。
.setTrustedThreshold(0.01f)
.create();
MLRemoteLangDetector mlRemoteLangDetector = MLLangDetectorFactory.getInstance()
.getRemoteLangDetector(setting);

2.3 进行语种检测

异步方法示例代码

// 方式一:返回多个语种检测结果,包括语种编码以及置信度,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) {
// 转换错误处理。
}
}
});

同步方法示例代码

// 方式一:返回多个语种检测结果,包括语种编码以及置信度,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 检测完成后,释放资源。

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

语种检测功能包含端侧和云侧,以上示例仅为云侧功能。

华为移动服务开源仓库地址:​ ​GitHub​

关注我们,第一时间了解 HMS Core 最新技术资讯~


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK