Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fff7bac authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Sync of SmartSelection.java wrapper from Google3 to support the new...

Merge "Sync of SmartSelection.java wrapper from Google3 to support the new annotate call, and alternative ways of construction."
parents 684585c4 9d6e9d70
Loading
Loading
Loading
Loading
+63 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.view.textclassifier;

import android.content.res.AssetFileDescriptor;

/**
 *  Java wrapper for SmartSelection native library interface.
 *  This library is used for detecting entities in text.
@@ -41,6 +43,26 @@ final class SmartSelection {
        mCtx = nativeNew(fd);
    }

    /**
     * Creates a new instance of SmartSelect predictor, using the provided model image, given as a
     * file path.
     */
    SmartSelection(String path) {
        mCtx = nativeNewFromPath(path);
    }

    /**
     * Creates a new instance of SmartSelect predictor, using the provided model image, given as an
     * AssetFileDescriptor.
     */
    SmartSelection(AssetFileDescriptor afd) {
        mCtx = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength());
        if (mCtx == 0L) {
            throw new IllegalArgumentException(
                "Couldn't initialize TC from given AssetFileDescriptor");
        }
    }

    /**
     * Given a string context and current selection, computes the SmartSelection suggestion.
     *
@@ -68,6 +90,15 @@ final class SmartSelection {
        return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags);
    }

    /**
     * Annotates given input text. Every word of the input is a part of some annotation.
     * The annotations are sorted by their position in the context string.
     * The annotations do not overlap.
     */
    public AnnotatedSpan[] annotate(String text) {
        return nativeAnnotate(mCtx, text);
    }

    /**
     * Frees up the allocated memory.
     */
@@ -91,12 +122,19 @@ final class SmartSelection {

    private static native long nativeNew(int fd);

    private static native long nativeNewFromPath(String path);

    private static native long nativeNewFromAssetFileDescriptor(AssetFileDescriptor afd,
                                                                long offset, long size);

    private static native int[] nativeSuggest(
            long context, String text, int selectionBegin, int selectionEnd);

    private static native ClassificationResult[] nativeClassifyText(
            long context, String text, int selectionBegin, int selectionEnd, int hintFlags);

    private static native AnnotatedSpan[] nativeAnnotate(long context, String text);

    private static native void nativeClose(long context);

    private static native String nativeGetLanguage(int fd);
@@ -114,4 +152,29 @@ final class SmartSelection {
            mScore = score;
        }
    }

    /** Represents a result of Annotate call. */
    public static final class AnnotatedSpan {
        final int mStartIndex;
        final int mEndIndex;
        final ClassificationResult[] mClassification;

        AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) {
            mStartIndex = startIndex;
            mEndIndex = endIndex;
            mClassification = classification;
        }

        public int getStartIndex() {
            return mStartIndex;
        }

        public int getEndIndex() {
            return mEndIndex;
        }

        public ClassificationResult[] getClassification() {
            return mClassification;
        }
    }
}