1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view.textclassifier;
18
19/**
20 *  Java wrapper for SmartSelection native library interface.
21 *  This library is used for detecting entities in text.
22 */
23final class SmartSelection {
24
25    static {
26        System.loadLibrary("textclassifier");
27    }
28
29    /** Hints the classifier that this may be a url. */
30    static final int HINT_FLAG_URL = 0x01;
31    /** Hints the classifier that this may be an email. */
32    static final int HINT_FLAG_EMAIL = 0x02;
33
34    private final long mCtx;
35
36    /**
37     * Creates a new instance of SmartSelect predictor, using the provided model image,
38     * given as a file descriptor.
39     */
40    SmartSelection(int fd) {
41        mCtx = nativeNew(fd);
42    }
43
44    /**
45     * Given a string context and current selection, computes the SmartSelection suggestion.
46     *
47     * The begin and end are character indices into the context UTF8 string. selectionBegin is the
48     * character index where the selection begins, and selectionEnd is the index of one character
49     * past the selection span.
50     *
51     * The return value is an array of two ints: suggested selection beginning and end, with the
52     * same semantics as the input selectionBeginning and selectionEnd.
53     */
54    public int[] suggest(String context, int selectionBegin, int selectionEnd) {
55        return nativeSuggest(mCtx, context, selectionBegin, selectionEnd);
56    }
57
58    /**
59     * Given a string context and current selection, classifies the type of the selected text.
60     *
61     * The begin and end params are character indices in the context string.
62     *
63     * Returns an array of ClassificationResult objects with the probability
64     * scores for different collections.
65     */
66    public ClassificationResult[] classifyText(
67            String context, int selectionBegin, int selectionEnd, int hintFlags) {
68        return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags);
69    }
70
71    /**
72     * Frees up the allocated memory.
73     */
74    public void close() {
75        nativeClose(mCtx);
76    }
77
78    /**
79     * Returns the language of the model.
80     */
81    public static String getLanguage(int fd) {
82        return nativeGetLanguage(fd);
83    }
84
85    /**
86     * Returns the version of the model.
87     */
88    public static int getVersion(int fd) {
89        return nativeGetVersion(fd);
90    }
91
92    private static native long nativeNew(int fd);
93
94    private static native int[] nativeSuggest(
95            long context, String text, int selectionBegin, int selectionEnd);
96
97    private static native ClassificationResult[] nativeClassifyText(
98            long context, String text, int selectionBegin, int selectionEnd, int hintFlags);
99
100    private static native void nativeClose(long context);
101
102    private static native String nativeGetLanguage(int fd);
103
104    private static native int nativeGetVersion(int fd);
105
106    /** Classification result for classifyText method. */
107    static final class ClassificationResult {
108        final String mCollection;
109        /** float range: 0 - 1 */
110        final float mScore;
111
112        ClassificationResult(String collection, float score) {
113            mCollection = collection;
114            mScore = score;
115        }
116    }
117}
118