Với API Chấm lỗi chính tả GenAI của Bộ công cụ học máy, bạn có thể giúp người dùng kiểm tra ngữ pháp và chính tả trên các đoạn văn bản ngắn.
Các chức năng chính
- Kiểm tra chính tả văn bản được nhập bằng bàn phím hoặc giọng nói
- Yêu cầu sẽ trả về ít nhất một đề xuất. Nếu trả về nhiều đề xuất, kết quả sẽ được sắp xếp theo mức độ tin cậy giảm dần.
Kết quả mẫu
Input |
Loại dữ liệu đầu vào |
Kết quả |
đây là một tin nhắn ngắn |
Bàn phím |
Đây là một tin nhắn ngắn |
Dự án gần như hoàn tất nhưng cần được xem xét |
Bàn phím |
Dự án gần như hoàn tất nhưng cần được xem xét |
Vui lòng gặp tôi tại Bear, |
Voice |
Vui lòng gặp tôi tại quầy bar. |
Bắt đầu
Để bắt đầu sử dụng API GenAI Proofreading, hãy thêm phần phụ thuộc này vào tệp bản dựng của dự án.
implementation("com.google.mlkit:genai-proofreading:1.0.0-beta1")
Sau đó, hãy định cấu hình và lấy ứng dụng Proofreader
có chế độ cài đặt ngôn ngữ và loại dữ liệu đầu vào cụ thể. Kiểm tra và đảm bảo có các tính năng mô hình cần thiết trên thiết bị (kích hoạt quá trình tải xuống nếu cần). Gửi văn bản bạn muốn phân tích trong ProofreadingRequest
, thực thi suy luận hiệu đính và cuối cùng xử lý các đề xuất được trả về để sửa văn bản.
Kotlin
val textToProofread = "The praject is compleet but needs too be reviewd"
// Define task with required input and output format
val options = ProofreaderOptions.builder(context)
// InputType can be KEYBOARD or VOICE. VOICE indicates that
// the user generated text based on audio input.
.setInputType(ProofreaderOptions.InputType.KEYBOARD)
// Refer to ProofreaderOptions.Language for available
// languages
.setLanguage(ProofreaderOptions.Language.ENGLISH)
.build()
val proofreader = Proofreading.getClient(options)
suspend fun prepareAndStartProofread() {
// Check feature availability, status will be one of the
// following: UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
val featureStatus = proofreader.checkFeatureStatus().await()
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary.
// If downloadFeature is not called, the first inference
// request will also trigger the feature to be downloaded
// if it's not already downloaded.
proofreader.downloadFeature(object : DownloadCallback {
override fun onDownloadStarted(bytesToDownload: Long) { }
override fun onDownloadFailed(e: GenAiException) { }
override fun onDownloadProgress(
totalBytesDownloaded: Long
) {}
override fun onDownloadCompleted() {
startProofreadingRequest(textToProofread, proofreader)
}
})
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded.
// If Gemini Nano is already downloaded on the device, the
// feature-specific LoRA adapter model will be downloaded
// very quickly. However, if Gemini Nano is not already
// downloaded, the download process may take longer.
startProofreadingRequest(textToProofread, proofreader)
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startProofreadingRequest(textToProofread, proofreader)
}
}
suspend fun startProofreadingRequest(
text: String, proofreader: Proofreader
) {
// Create task request
val proofreadingRequest =
ProofreadingRequest.builder(text).build()
// Start proofreading request with non-streaming response
// More than 1 result may be returned. If multiple suggestions
// are returned, results will be sorted by descending confidence.
val proofreadingResults =
proofreader.runInference(proofreadingRequest).await().results
// You can also start a streaming request
// proofreader.runInference(proofreadingRequest) { newText ->
// // show new text in UI
// }
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
proofreader.close()
Java
String textToProofread = "The praject is compleet but needs too be reviewd";
// Define task with required input and output format
ProofreaderOptions proofreaderOptions =
ProofreaderOptions
.builder(context)
// InputType can be KEYBOARD or VOICE. VOICE indicates that the
// user generated text based on audio input.
.setInputType(ProofreaderOptions.InputType.KEYBOARD)
// Refer to ProofreaderOptions.Language for available languages
.setLanguage(ProofreaderOptions.Language.ENGLISH)
.build();
Proofreader proofreader = Proofreading.getClient(proofreaderOptions);
void prepareAndStartProofread(Context context) throws ExecutionException,
InterruptedException {
// Check feature availability, status will be one of the following:
// UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
try {
int featureStatus = proofreader.checkFeatureStatus().get();
if (featureStatus == FeatureStatus.DOWNLOADABLE) {
// Download feature if necessary.
// If downloadFeature is not called, the first inference request
// will also trigger the feature to be downloaded if it's not
// already downloaded.
proofreader.downloadFeature(new DownloadCallback() {
@Override
public void onDownloadCompleted() {
startProofreadingRequest(textToProofread, proofreader);
}
@Override
public void onDownloadFailed(GenAiException e) {
}
@Override
public void onDownloadProgress(long totalBytesDownloaded) {
}
@Override
public void onDownloadStarted(long bytesDownloaded) {
}
});
} else if (featureStatus == FeatureStatus.DOWNLOADING) {
// Inference request will automatically run once feature is
// downloaded.
// If Gemini Nano is already downloaded on the device, the
// feature-specific LoRA adapter model will be downloaded
// very quickly. However, if Gemini Nano is not already
// downloaded, the download process may take longer.
startProofreadingRequest(textToProofread, proofreader);
} else if (featureStatus == FeatureStatus.AVAILABLE) {
startProofreadingRequest(textToProofread, proofreader);
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
void startProofreadingRequest(String text, Proofreader proofreader) {
// Create task request
ProofreadingRequest proofreadingRequest = ProofreadingRequest
.builder(text).build();
try {
// Start proofreading request with non-streaming response
// More than 1 result may be returned. If multiple suggestions are
// returned, results will be sorted by descending confidence.
proofreader.runInference(proofreadingRequest).get().getResults();
// You can also start a streaming request
// proofreader.runInference(proofreadingRequest, newText -> {
// // Show new text in UI
// });
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
proofreader.close();
Cách mô hình xử lý nhiều loại dữ liệu đầu vào
Khi có thêm thông tin về cách người dùng nhập văn bản (bằng bàn phím hoặc giọng nói), mô hình có thể dự đoán chính xác hơn loại lỗi có thể xảy ra. Văn bản nhập bằng bàn phím dễ bị lỗi chính tả hơn với các phím ở gần, trong khi văn bản nhập bằng giọng nói dễ bị lỗi chính tả hơn với các từ có cách phát âm giống nhau.
Các tính năng được hỗ trợ và giới hạn
Tính năng hiệu đính được hỗ trợ cho các ngôn ngữ sau: tiếng Anh, tiếng Nhật, tiếng Pháp, tiếng Đức, tiếng Ý, tiếng Tây Ban Nha và tiếng Hàn. Các ngôn ngữ này được xác định trong ProofreaderOptions.Language
. Dữ liệu đầu vào phải có ít hơn 256 mã thông báo.
Khả năng hoạt động của cấu hình tính năng cụ thể (do ProofreaderOptions
chỉ định) có thể thay đổi tuỳ thuộc vào cấu hình của thiết bị cụ thể và các mô hình đã được tải xuống thiết bị.
Cách đáng tin cậy nhất để nhà phát triển đảm bảo rằng tính năng API dự kiến được hỗ trợ trên thiết bị có ProofreaderOptions
được yêu cầu là gọi phương thức checkFeatureStatus()
. Phương thức này cung cấp trạng thái xác định về khả năng sử dụng tính năng trên thiết bị trong thời gian chạy.
Các vấn đề thường gặp khi thiết lập
Các API GenAI của Bộ công cụ học máy dựa vào ứng dụng Android AICore để truy cập vào Gemini Nano. Khi một thiết bị vừa được thiết lập (bao gồm cả việc đặt lại) hoặc ứng dụng AICore vừa được đặt lại (ví dụ: xoá dữ liệu, gỡ cài đặt rồi cài đặt lại), ứng dụng AICore có thể không có đủ thời gian để hoàn tất quá trình khởi chạy (bao gồm cả việc tải cấu hình mới nhất xuống từ máy chủ). Do đó, các API GenAI của Bộ công cụ học máy có thể không hoạt động như mong đợi. Dưới đây là các thông báo lỗi thiết lập thường gặp mà bạn có thể thấy và cách xử lý các lỗi đó:
Ví dụ về thông báo lỗi | Cách xử lý |
AICore không thành công với loại lỗi 4-CONNECTION_ERROR và mã lỗi 601-BINDING_FAILURE: Không thể liên kết dịch vụ AICore. | Điều này có thể xảy ra khi bạn cài đặt ứng dụng bằng các API GenAI của Bộ công cụ học máy ngay sau khi thiết lập thiết bị hoặc khi AICore bị gỡ cài đặt sau khi ứng dụng của bạn được cài đặt. Bạn có thể khắc phục vấn đề này bằng cách cập nhật ứng dụng AICore rồi cài đặt lại ứng dụng. |
AICore không thành công với loại lỗi 3-PREPARATION_ERROR và mã lỗi 606-FEATURE_NOT_FOUND: Tính năng ... không có sẵn. |
Điều này có thể xảy ra khi AICore chưa tải xong các cấu hình mới nhất. Duy trì kết nối mạng và đợi từ vài phút đến vài giờ.
Xin lưu ý rằng nếu trình tải khởi động của thiết bị đã được mở khoá, bạn cũng sẽ thấy lỗi này – API này không hỗ trợ các thiết bị có trình tải khởi động đã mở khoá. |
AICore không thành công với loại lỗi 1-DOWNLOAD_ERROR và mã lỗi 0-UNKNOWN: Tính năng ... không thành công với trạng thái lỗi 0 và lỗi esz: UNAVAILABLE: Unable to resolve host ... | Duy trì kết nối mạng, đợi vài phút rồi thử lại. |