1

"Reading music alone is not as good as reading music in groups."

What's the meaning? It is to read happily alone, it is better to let more people read happily. This is programmer A Qiang's simple reading concept.

As a foreign language book lover, A Qiang wants to enjoy the original foreign language books to the fullest, but he is always stumped by unfamiliar words that appear from time to time;

During the student days, the obstacles to reading foreign languages appeared in the literature review stage, and professional and unfamiliar vocabulary was the biggest "killer" for his understanding of literature efficiency;

In today's global real-time news, no amount of vocabulary can perfectly defend against the challenge of new words.

In order to allow more people to have a better reading experience in foreign languages, a reading artifact of "no where to draw" was born in Aqiang's few lines of code. Using the text translation capabilities of HMS Core machine learning services, it can be realized by hand. A word-marking translation function makes the reading experience of foreign language books smoother.

About text translation

The machine learning service provides online and offline text translation capabilities , supports translation between different languages and has a network. E.g:

Travel apps provide the function of translating foreign language signs, menus, etc. into native languages to optimize experience; educational learning apps, when learning multiple languages, can easily translate unfamiliar languages into familiar languages, improving learning efficiency.

At the same time, offline translation does not require network support, and communication obstacles can be easily solved even in a networkless environment during the journey.

Show results

development preparation

The following preparations need to be completed before the official development of the "Word Marking Translation" function:

Configure AppGallery Connect

Subscribe to a service

Integrate HMS Core SDK

Configure Obfuscation Script

Add permission

For specific development preparation, please refer to the documentation

application development

2.1 Set the authentication information of the application by referring to the instructions for using the cloud test authentication confidence .

 MLApplication.getInstance().setApiKey("apiKey or Token");

2.2 Create an online text translator

 MLLocalTranslateSetting setting = new MLLocalTranslateSetting
        .Factory()
        .setSourceLangCode(mSourceLangCode)
        .setTargetLangCode(mTargetLangCode)
        .create();
this.localTranslator = MLTranslatorFactory.getInstance().getLocalTranslator(setting);

2.3 Query the languages supported by online translation

 MLTranslateLanguage.getCloudAllLanguages().addOnSuccessListener(new OnSuccessListener<Set<String>>() {
    @Override
    public void onSuccess(Set<String> result) {
        // 成功获取在线翻译所支持的语种。
    }
});

2.4 Perform online text translation

 localTranslator.preparedModel(downloadStrategy, modelDownloadListener).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void aVoid) {

        final Task<String> task = localTranslator.asyncTranslate(input);

        task.addOnSuccessListener(new OnSuccessListener<String>() {
            @Override
            public void onSuccess(String text) {
                displaySuccess(text, true);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(Exception e) {
                displayFailure(e);
            }
        });

    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        displayFailure(e);
    }
});

2.5 The translation is completed and resources are released

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

Learn more about machine learning services >>

Visit the official website of the Machine Learning Service Alliance

Get the machine learning service development guidance document

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~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。