自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS AI基礎技術賦能之關鍵字獲取

開發(fā) OpenHarmony
關鍵字提取主要用于從新聞和郵件里提取出關鍵字,便于用戶快速獲取新聞和郵件的主題。關鍵字可以為有意義的實體,比如,人名、電影,也可以為非實體的關鍵詞匯,如,上課、考研。

[[419347]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

引言

在實際應用開發(fā)中,時不時的會遇到AI領域相關的一些技術,本節(jié)旨在詳細講述關鍵字獲取技術,關鍵字獲取涉及各個領域中,如:游記摘要、新聞標簽、雜志文刊等。所以對于HarmonyOS開發(fā)者而言,了解和掌握HarmonyOS AI領域相關技術是至關重要的,也是每一個HarmonyOS開發(fā)者的一項必不可少的專業(yè)技能。

功能介紹

關鍵字提取主要用于從新聞和郵件里提取出關鍵字,便于用戶快速獲取新聞和郵件的主題。關鍵字可以為有意義的實體,比如,人名、電影,也可以為非實體的關鍵詞匯,如,上課、考研。

開發(fā)指南

1、使用NluClient靜態(tài)類進行初始化,通過異步方式獲取服務的連接

  1. NluClient.getInstance().init(context, new OnResultListener<Integer>(){ 
  2.       @Override 
  3.       public void onResult(Integer result){ 
  4.        // 初始化成功回調,在服務初始化成功調用該函數 
  5.       } 
  6.   }, true); 

2、調用獲取關鍵詞提取方法得到分析結果,同一個接口提供了同步和異步兩個方法

同步:

  1. String requestData= "{number:2,body:'今天我們一起去上課吧',title:'一起去上課'}"
  2. ResponseResult respResult = NluClient.getInstance().getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL); 
  3. if (null != respResult){ 
  4.      // 獲取接口返回結果,參考接口文檔返回使用 
  5.      String result = respResult.getResponseResult(); 

異步:

  1. // 待分析文本 
  2. String requestData= "{number:2,body:'今天我們一起去上課吧',title:'一起去上課'}"
  3. // 調用接口 
  4. NluClient.getInstance().getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL,new OnResultListener<ResponseResult>(){ 
  5.     @Override 
  6.     public void onResult(ResponseResult respResult) 
  7.     { 
  8.     // 異步返回 
  9.     if(null != respResult && NluError.SUCCESS_RESULT == respResult.getCode()) 
  10.         { 
  11.         // 獲取接口返回結果,參考接口文檔返回使用 
  12.         String result = respResult.getResponseResult(); 
  13.         } 
  14.     } 
  15. }); 

3、使用結束調用destroy()方法釋放進程資源

  1. NluClient.getInstance().destroy(this); 

示例代碼

1、xml布局

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.   ohos:height="match_parent" 
  5.   ohos:orientation="vertical" 
  6.   ohos:width="match_parent"
  7.   <Text 
  8.     ohos:background_element="$graphic:background_ability_main" 
  9.     ohos:height="match_content" 
  10.     ohos:layout_alignment="horizontal_center" 
  11.     ohos:multiple_lines="true" 
  12.     ohos:text="$string:title" 
  13.     ohos:top_margin="50vp" 
  14.     ohos:text_size="50" 
  15.     ohos:width="match_content" 
  16.     /> 
  17.   <Text 
  18.     ohos:background_element="$graphic:background_ability_main" 
  19.     ohos:height="match_content" 
  20.     ohos:layout_alignment="horizontal_center" 
  21.     ohos:multiple_lines="true" 
  22.     ohos:left_margin="10vp" 
  23.     ohos:right_margin="10vp" 
  24.     ohos:text="body:'同步-今天我們一起去上課吧',title:'同步-一起去上課'-body:'異步-今天我們一起去上班吧',title:'異步-我們去上班'" 
  25.     ohos:top_margin="50vp" 
  26.     ohos:text_size="50" 
  27.     ohos:width="match_content" 
  28.     /> 
  29.   <Text 
  30.     ohos:background_element="$graphic:background_ability_main" 
  31.     ohos:height="match_content" 
  32.     ohos:id="$+id:text_content" 
  33.     ohos:layout_alignment="horizontal_center" 
  34.     ohos:multiple_lines="true" 
  35.     ohos:top_margin="50vp" 
  36.     ohos:left_margin="10vp" 
  37.     ohos:right_margin="10vp" 
  38.     ohos:text_size="50" 
  39.     ohos:width="match_content" 
  40.     /> 
  41.   <Text 
  42.     ohos:background_element="$graphic:background_ability_main" 
  43.     ohos:height="match_content" 
  44.     ohos:id="$+id:text_asynchronous" 
  45.     ohos:layout_alignment="horizontal_center" 
  46.     ohos:text="$string:asynchronous" 
  47.     ohos:top_margin="50vp" 
  48.     ohos:text_size="50" 
  49.     ohos:width="match_content" 
  50.     /> 
  51.   <Text 
  52.     ohos:background_element="$graphic:background_ability_main" 
  53.     ohos:height="match_content" 
  54.     ohos:id="$+id:text_synchronization" 
  55.     ohos:layout_alignment="horizontal_center" 
  56.     ohos:text="$string:synchronization" 
  57.     ohos:top_margin="50vp" 
  58.     ohos:text_size="50" 
  59.     ohos:width="match_content" 
  60.     /> 
  61. </DirectionalLayout> 

2、案例代碼

  1. package com.example.keywords.slice; 
  2.  
  3. import com.example.keywords.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Component; 
  7. import ohos.agp.components.Component.ClickedListener; 
  8. import ohos.agp.components.Text; 
  9. import ohos.ai.nlu.NluClient; 
  10. import ohos.ai.nlu.NluRequestType; 
  11. import ohos.ai.nlu.ResponseResult; 
  12. import ohos.ai.nlu.util.NluError; 
  13.  
  14. public class MainAbilitySlice extends AbilitySlice implements ClickedListener { 
  15.  
  16.   private Text text_content; 
  17.  
  18.   @Override 
  19.   public void onStart(Intent intent) { 
  20.     super.onStart(intent); 
  21.     super.setUIContent(ResourceTable.Layout_ability_main); 
  22.     text_content = (Text) findComponentById(ResourceTable.Id_text_content); 
  23.     Text text_synchronization = (Text) findComponentById(ResourceTable.Id_text_synchronization); 
  24.     Text text_asynchronous = (Text) findComponentById(ResourceTable.Id_text_asynchronous); 
  25.     text_synchronization.setClickedListener(this); 
  26.     text_asynchronous.setClickedListener(this); 
  27.     // 使用NluClient靜態(tài)類初始化,通過異步方式獲取服務連接 
  28.     NluClient.getInstance().init(this, nulltrue); 
  29.   } 
  30.  
  31.   @Override 
  32.   public void onClick(Component component) { 
  33.     switch (component.getId()) { 
  34.       case ResourceTable.Id_text_synchronization: 
  35.         String requestData = "{number:3,body:'同步-今天我們一起去上課吧',title:'同步-一起去上課'}"
  36.         ResponseResult responseResult = NluClient.getInstance() 
  37.             .getKeywords(requestData, NluRequestType.REQUEST_TYPE_LOCAL); 
  38.         if (responseResult != null) { 
  39.           text_content.setText(responseResult.getResponseResult()); 
  40.         } 
  41.         break; 
  42.       case ResourceTable.Id_text_asynchronous: 
  43.         String rData = "{number:3,body:'異步-今天我們一起去上班吧',title:'異步-我們去上班'}"
  44.         NluClient.getInstance().getKeywords(rData, NluRequestType.REQUEST_TYPE_LOCAL, 
  45.                 responseResult1 -> { 
  46.                   if (responseResult1 != null && NluError.SUCCESS_RESULT == responseResult1.getCode()) { 
  47.                     getAbility().getUITaskDispatcher().syncDispatch(() -> text_content.setText(responseResult1.getResponseResult())); 
  48.                   } 
  49.                 }); 
  50.         break; 
  51.       default
  52.         break; 
  53.     } 
  54.   } 
  55.  
  56.   @Override 
  57.   protected void onBackground() { 
  58.     super.onBackground(); 
  59.     NluClient.getInstance().destroy(this); 
  60.   } 

實現效果

【軟通動力】HarmonyOS AI基礎技術賦能之關鍵字獲取-鴻蒙HarmonyOS技術社區(qū)
【軟通動力】HarmonyOS AI基礎技術賦能之關鍵字獲取-鴻蒙HarmonyOS技術社區(qū)

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2021-09-23 10:00:57

鴻蒙HarmonyOS應用

2011-06-27 15:08:15

SEO

2021-09-13 15:14:01

鴻蒙HarmonyOS應用

2019-12-20 15:19:41

Synchroinze線程安全

2011-06-21 09:50:51

volatile

2022-08-17 07:53:10

Volatile關鍵字原子性

2009-12-18 11:37:54

Ruby關鍵字yiel

2021-01-05 10:26:50

鴻蒙Javafinal

2021-08-31 14:58:52

鴻蒙HarmonyOS應用

2021-04-18 07:58:22

SQL Server數據庫Apply

2009-11-26 19:24:54

PHP類CMS

2024-03-15 11:52:03

C++關鍵字編程

2023-11-10 09:29:30

MySQLExplain

2021-08-15 08:11:54

AndroidSynchronize關鍵字

2021-09-03 15:27:17

鴻蒙HarmonyOS應用

2009-08-21 14:58:56

C# this關鍵字

2013-01-30 10:12:14

Pythonyield

2018-04-20 15:56:09

Pythonglobal關鍵字

2009-09-02 09:24:03

C# this關鍵字
點贊
收藏

51CTO技術棧公眾號