blob: 661ca3b445c010e045161f79084fee442e76f95f [file] [log] [blame]
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.textclassifier;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Java wrapper for ActionsSuggestions native library interface. This library is used to suggest
* actions and replies in a given conversation.
*
* @hide
*/
public final class ActionsSuggestionsModel implements AutoCloseable {
private final AtomicBoolean isClosed = new AtomicBoolean(false);
static {
System.loadLibrary("textclassifier");
}
private long actionsModelPtr;
private AnnotatorModel annotator;
/**
* Creates a new instance of Actions predictor, using the provided model image, given as a file
* descriptor.
*/
public ActionsSuggestionsModel(int fileDescriptor) {
this(fileDescriptor, null);
}
public ActionsSuggestionsModel(int fileDescriptor, AnnotatorModel annotator) {
actionsModelPtr = nativeNewActionsModel(fileDescriptor);
if (actionsModelPtr == 0L) {
throw new IllegalArgumentException("Couldn't initialize actions model from file descriptor.");
}
setAnnotator(annotator);
}
/**
* Creates a new instance of Actions predictor, using the provided model image, given as a file
* path.
*/
public ActionsSuggestionsModel(String path) {
this(path, null);
}
public ActionsSuggestionsModel(String path, AnnotatorModel annotator) {
actionsModelPtr = nativeNewActionsModelFromPath(path);
if (actionsModelPtr == 0L) {
throw new IllegalArgumentException("Couldn't initialize actions model from given file.");
}
setAnnotator(annotator);
}
/** Suggests actions / replies to the given conversation. */
public ActionSuggestion[] suggestActions(
Conversation conversation, ActionSuggestionOptions options) {
return nativeSuggestActions(actionsModelPtr, conversation, options);
}
/** Frees up the allocated memory. */
@Override
public void close() {
if (isClosed.compareAndSet(false, true)) {
nativeCloseActionsModel(actionsModelPtr);
actionsModelPtr = 0L;
}
}
@Override
protected void finalize() throws Throwable {
try {
close();
} finally {
super.finalize();
}
}
/** Returns a comma separated list of locales supported by the model as BCP 47 tags. */
public static String getLocales(int fd) {
return nativeGetLocales(fd);
}
/** Returns the version of the model. */
public static int getVersion(int fd) {
return nativeGetVersion(fd);
}
/** Returns the name of the model. */
public static String getName(int fd) {
return nativeGetName(fd);
}
/** Action suggestion that contains a response text and the type of the response. */
public static final class ActionSuggestion {
private final String responseText;
private final String actionType;
private final float score;
public ActionSuggestion(String responseText, String actionType, float score) {
this.responseText = responseText;
this.actionType = actionType;
this.score = score;
}
public String getResponseText() {
return responseText;
}
public String getActionType() {
return actionType;
}
/** Confidence score between 0 and 1 */
public float getScore() {
return score;
}
}
/** Represents a single message in the conversation. */
public static final class ConversationMessage {
private final int userId;
private final String text;
private final long referenceTimeMsUtc;
private final String locales;
public ConversationMessage(int userId, String text, long referenceTimeMsUtc, String locales) {
this.userId = userId;
this.text = text;
this.referenceTimeMsUtc = referenceTimeMsUtc;
this.locales = locales;
}
/** The identifier of the sender */
public int getUserId() {
return userId;
}
public String getText() {
return text;
}
/**
* Return the reference time of the message, for example, it could be compose time or send time.
* {@code 0} means unspecified.
*/
public long getReferenceTimeMsUtc() {
return referenceTimeMsUtc;
}
/** Returns a comma separated list of locales supported by the model as BCP 47 tags. */
public String getLocales() {
return locales;
}
}
/** Represents conversation between multiple users. */
public static final class Conversation {
public final ConversationMessage[] conversationMessages;
public Conversation(ConversationMessage[] conversationMessages) {
this.conversationMessages = conversationMessages;
}
public ConversationMessage[] getConversationMessages() {
return conversationMessages;
}
}
/** Represents options for the SuggestActions call. */
public static final class ActionSuggestionOptions {
private final AnnotatorModel.AnnotationOptions annotationOptions;
public ActionSuggestionOptions() {
this.annotationOptions = null;
}
public ActionSuggestionOptions(AnnotatorModel.AnnotationOptions annotationOptions) {
this.annotationOptions = annotationOptions;
}
public AnnotatorModel.AnnotationOptions getAnnotationOptions() {
return annotationOptions;
}
}
/** Sets and annotator to use for actions suggestions. */
private void setAnnotator(AnnotatorModel annotator) {
this.annotator = annotator;
if (annotator != null) {
nativeSetAnnotator(annotator.getNativeAnnotator());
}
}
private static native long nativeNewActionsModel(int fd);
private static native long nativeNewActionsModelFromPath(String path);
private static native String nativeGetLocales(int fd);
private static native int nativeGetVersion(int fd);
private static native String nativeGetName(int fd);
private native ActionSuggestion[] nativeSuggestActions(
long context, Conversation conversation, ActionSuggestionOptions options);
private native void nativeCloseActionsModel(long context);
private native void nativeSetAnnotator(long annotatorPtr);
}