ตัวแยกประเภทข้อความใช้เทคนิคแมชชีนเลิร์นนิงเพื่อช่วยนักพัฒนาแอปจัดประเภทข้อความ
ตัวแยกประเภทข้อความรุ่น Android 11
Android 11 เปิดตัวการใช้งานเริ่มต้นที่อัปเดตได้ของบริการจัดประเภทข้อความใน
โมดูล ExtServices ในอุปกรณ์ที่ใช้ Android 11 ขึ้นไป วิธีการ
getTextClassifier()
จะแสดงผลการใช้งานเริ่มต้นนี้ในโมดูล ExtServices
เราขอแนะนำให้ผู้ผลิตอุปกรณ์ใช้การติดตั้งใช้งาน TextClassifierService
นี้เนื่องจากสามารถอัปเดตผ่านอัปเดต OTA หลักได้
นอกจากนี้ Android 11 ยังนำตัวแยกประเภทข้อความในเครื่องซึ่งใช้โดยค่าเริ่มต้นซึ่งเปิดตัวไปก่อนหน้านี้ใน
Android 8.1 ออกด้วย ด้วยเหตุนี้
getLocalTextClassifier()
จึงแสดงผลตัวจัดประเภทข้อความ NO_OP
คุณควรใช้เมธอด
getDefaultTextClassifierImplementation()
instead of
getDefaultTextClassifierImplementation()
แทนการติดตั้งใช้งานในเครื่อง
สำหรับผู้ผลิตอุปกรณ์ที่อาจต้องการใช้อัลกอริทึมการจัดประเภทข้อความของตนเอง สามารถใช้บริการการจัดประเภทข้อความที่กำหนดเองได้โดยระบุ config_defaultTextClassifierPackage
ในไฟล์ config.xml
หากไม่ได้ระบุการกําหนดค่านี้ ระบบจะใช้การติดตั้งใช้งานระบบเริ่มต้น การใช้งานที่กําหนดเองสามารถรับอินสแตนซ์ของการใช้งานเริ่มต้นได้โดยเรียกใช้
TextClassifierService.getDefaultTextClassifierImplementation(Context)
ดูข้อมูลเพิ่มเติมได้ที่
การใช้บริการการจัดประเภทข้อความที่กําหนดเอง
การทดสอบ
หากต้องการตรวจสอบการติดตั้งใช้งานบริการการจัดประเภทข้อความ ให้ใช้การทดสอบชุดเครื่องมือทดสอบความเข้ากันได้ (CTS) ใน
platform/cts/tests/tests/textclassifier/
การปรับปรุงตัวแยกประเภทข้อความใน Android 10
Android 10 เปิดตัวเมธอด 2 รายการใน TextClassifier API ได้แก่
suggestConversationActions
และ
detectLanguage
เมธอด suggestConversationActions
จะสร้างการตอบกลับและการดำเนินการที่แนะนำจากการสนทนาหนึ่งๆ และเมธอด detectLanguage
จะตรวจหาภาษาของข้อความ
ไฟล์รูปแบบสําหรับวิธีการเหล่านี้แสดงอยู่ด้านล่างและอยู่ใน
external/libtextclassifier/models/
suggestionConversationActions
:actions_suggestions.universal.model
detectLanguage
:lang_id.model
หากต้องการเปิดตัวอุปกรณ์ที่มีไฟล์รุ่นล่าสุดในภาพรวมของโรงงาน ให้ทำดังนี้
ดึงข้อมูลไฟล์โมเดลล่าสุด
external/libtextclassifier/models/update.sh
- เปลี่ยนชื่อไฟล์ที่ดาวน์โหลดมาเพื่อแทนที่ไฟล์ที่มีอยู่
- ยืนยันการตั้งค่า
adb shell dumpsys textclassification
นี่เป็นตัวอย่างเอาต์พุตของคําสั่งดังกล่าว
TextClassifierImpl: Annotator model file(s): ModelFile { path=/etc/textclassifier/textclassifier.universal.model name=textclassifier.universal.model version=608 locales=und } ModelFile { path=/etc/textclassifier/textclassifier.en.model name=textclassifier.en.model version=608 locales=en } LangID model file(s): ModelFile { path=/etc/textclassifier/lang_id.model name=lang_id.model version=0 locales=und } Actions model file(s): ModelFile { path=/etc/textclassifier/actions_suggestions.universal.model name=actions_suggestions.universal.model version=0 locales=und }
การปรับปรุงตัวแยกประเภทข้อความใน Android 9
Android 9 ขยายเฟรมเวิร์กการจัดประเภทข้อความที่เปิดตัวใน Android 8.1 ด้วยบริการจัดประเภทข้อความใหม่ บริการจัดหมวดหมู่ข้อความเป็นวิธีที่แนะนำสำหรับ OEM ในการให้การสนับสนุนระบบการจัดหมวดหมู่ข้อความ บริการจัดประเภทข้อความอาจเป็นส่วนหนึ่งของ APK ของระบบและอัปเดตได้เมื่อจำเป็น
Android 9 มีการติดตั้งใช้งานบริการตัวจัดประเภทข้อความเริ่มต้น (
TextClassifierImpl
) ที่ใช้อยู่ เว้นแต่คุณจะแทนที่ด้วยการติดตั้งใช้งานบริการตัวจัดประเภทข้อความที่กําหนดเอง
ใช้บริการการจัดประเภทข้อความที่กําหนดเอง
ส่วนต่อไปนี้จะอธิบายวิธีใช้ตัวจัดประเภทข้อความที่กำหนดเองซึ่งคุณพัฒนาขึ้น
ขยาย android.service.textclassifier.TextClassifierService
public final class TextClassifierServiceImpl extends TextClassifierService { // Returns TextClassifierImpl. private final TextClassifier tc = getLocalTextClassifier(); @Override public void onSuggestSelection( @Nullable TextClassificationSessionId sessionId, @NonNull TextSelection.Request request, @NonNull CancellationSignal cancellationSignal, @NonNull Callback<TextSelection> callback) { CompletableFuture.supplyAsync( () -> tc.suggestSelection(request)) .thenAccept(r -> callback.onSuccess(r)); } @Override public void onClassifyText( @Nullable TextClassificationSessionId sessionId, @NonNull TextClassification.Request request, @NonNull CancellationSignal cancellationSignal, @NonNull Callback<TextClassification> callback) { ... } @Override public void onGenerateLinks( @Nullable TextClassificationSessionId sessionId, @NonNull TextLinks.Request request, @NonNull CancellationSignal cancellationSignal, @NonNull Callback<TextLinks> callback) { ... } ... }
กำหนดบริการในไฟล์ Manifest ของ Android
[AndroidManifest.xml]
<service android:name=".TextClassifierServiceImpl" android:permission="android.permission.BIND_TEXTCLASSIFIER_SERVICE"> <intent-filter> <action android:name= "android.service.textclassifier.TextClassifierService"/> </intent-filter> </service>
โปรดทราบว่าบริการต้องใช้สิทธิ์ android.permission.BIND_TEXTCLASSIFIER_SERVICE
และต้องระบุการดำเนินการตามเจตนา android.service.textclassifier.TextClassifierService
ตั้งค่าบริการการจัดประเภทข้อความเริ่มต้นของระบบในการวางซ้อนการกําหนดค่า
[config.xml]
<string name="config_defaultTextClassifierPackage" translatable="false">com.example.textclassifierservice</string>
สร้างบริการการจัดประเภทข้อความลงในอิมเมจระบบ
บริการจัดประเภทข้อความที่กําหนดเองอาจเป็น APK แบบสแตนด์อโลนที่สร้างขึ้นในอิมเมจระบบหรือเป็นส่วนหนึ่งของ APK ระบบอื่น ระบบใช้
PackageManager.MATCH_SYSTEM_ONLY
เพื่อแก้ไขบริการ
การทดสอบ
ทำการทดสอบใน android.view.textclassifier.cts
การเปลี่ยนแปลงอื่นๆ ในการจัดประเภทข้อความใน Android 9
โปรดดู การตรวจสอบข้อบังคับของภาษาที่ติดตั้ง
ไฟล์รุ่น Android 9 ใช้ร่วมกับไฟล์รุ่น Android 8.x ไม่ได้
ไฟล์รุ่นของ Android 9 มีรูปแบบการตั้งชื่อดังนี้
texclassifier.[language-code].model
(เช่น
textclassifier.en.model
)
แทนที่จะเป็น textclassifier.smartselection.en.model
ใน Android 8.x
รับไฟล์รูปแบบการจัดประเภทข้อความล่าสุด
หากต้องการรับโมเดลล่าสุด ให้เรียกใช้สคริปต์ต่อไปนี้ซึ่งจะอัปเดตโมเดล TextClassifier ในซอร์สทรี
external/libtextclassifier/native/models/update.sh
ตัวแยกประเภทข้อความของ Android เวอร์ชัน 8.1
Android 8.1 ได้เปิดตัว TextClassfier API เพื่อใช้การจัดประเภทข้อความ
TextClassificationManager tcm = context.getSystemService(TextClassificationManager.class); TextClassifier classifier = tcm.getTextClassifier(); TextSelection selection = classifier.suggestSelection(...); TextClassification classification = classifier.classifyText(...);
นักพัฒนาซอฟต์แวร์สามารถตั้งค่าตัวจัดประเภทข้อความที่กำหนดเองได้ดังนี้
tcm.setTextClassifier(customTextClassifier);
แต่หากนักพัฒนาแอปตั้งค่าตัวจัดประเภทข้อความเป็น null
ระบบจะแสดงตัวจัดประเภทข้อความเริ่มต้นสำหรับ getTextClassifier()
ดูandroid.view.textclassifier.TextClassifierImpl
TextView และ WebView ใช้ TextClassifier สําหรับฟีเจอร์การเลือกอัจฉริยะและการแชร์ข้อความอัจฉริยะ

รูปที่ 1 การใช้งาน TextClassifier
โมเดลเครือข่ายประสาทของ TextClassifier
โครงการโอเพนซอร์ส Android (AOSP) มีโมเดลเครือข่ายประสาทจํานวนหนึ่งสําหรับการจัดประเภทข้อความ ไฟล์โมเดลแต่ละไฟล์ได้รับการฝึกสำหรับภาษาเดียว คุณติดตั้งโมเดลผสมกันได้ทุกรูปแบบ โมเดลจะกำหนดไว้ใน
external/libtextclassifier/Android.mk
ติดตั้งรูปแบบภาษาในอุปกรณ์ไว้ล่วงหน้า
คุณระบุชุดโมเดลภาษาและติดตั้งลงในอุปกรณ์ได้โดยทำดังนี้
# ----------------------- # Smart Selection bundles # ----------------------- include $(CLEAR_VARS) LOCAL_MODULE := textclassifier.smartselection.bundle1 LOCAL_REQUIRED_MODULES := textclassifier.smartselection.en.model LOCAL_REQUIRED_MODULES += textclassifier.smartselection.es.model LOCAL_REQUIRED_MODULES += textclassifier.smartselection.de.model LOCAL_REQUIRED_MODULES += textclassifier.smartselection.fr.model include $(BUILD_STATIC_LIBRARY)
เช่น ใน device/google/marlin/device-common.mk
# TextClassifier smart selection model files
PRODUCT_PACKAGES += \
textclassifier.smartselection.bundle1
ตรวจสอบข้อบังคับภาษาที่ติดตั้ง
ใช้ ADB เพื่อแสดงรายการไฟล์ในไดเรกทอรี โดยทำดังนี้
$ adb shell ls -l /etc/textclassifier -rw-r--r-- 1 root root ... textclassifier.smartselection.de.model -rw-r--r-- 1 root root ... textclassifier.smartselection.en.model -rw-r--r-- 1 root root ... textclassifier.smartselection.es.model -rw-r--r-- 1 root root ... textclassifier.smartselection.fr.model
การอัปเดตโมเดล
โมเดลสามารถอัปเดตได้โดยการรวมโมเดลใหม่เป็นส่วนหนึ่งของการอัปเดตรูปภาพของระบบ หรืออัปเดตแบบไดนามิกโดยให้คอมโพเนนต์ของระบบทริกเกอร์การอัปเดตผ่านAPI ACTION_UPDATE_SMART_SELECTION
Intent ของระบบ การเผยแพร่ความตั้งใจของ API ระบบนี้จะช่วยให้เฟรมเวิร์กอัปเดตโมเดลภาษาของภาษาที่ตั้งไว้ในปัจจุบันได้ โมเดลจะมีภาษาที่รองรับและหมายเลขเวอร์ชันเพื่อให้ระบบใช้โมเดลที่เหมาะสมล่าสุด
คุณจึงไม่ต้องโหลดโมเดลล่วงหน้าสำหรับทุกภาษา เนื่องจากสามารถเพิ่มโมเดลในภายหลังได้ หากไม่พบไฟล์โมเดลสําหรับภาษาที่ระบุ การแยกประเภทข้อความจะแสดงผลเป็นค่า No-Op
การทดสอบชุดเครื่องมือทดสอบความเข้ากันได้
การทดสอบชุดเครื่องมือทดสอบความเข้ากันได้ของ Android (CTS) ที่เกี่ยวข้องมีดังนี้
cts/tests/tests/view/src/android/view/textclassifier/cts/TextClassificationManagerTest.java
cts/tests/tests/widget/src/android/widget/cts/TextViewTest.java
testSmartSelection
testSmartSelection_dragSelection
testSmartSelection_resetSelection