Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 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 | |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 17 | package android.view.textclassifier; |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 18 | |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 19 | import android.content.res.AssetFileDescriptor; |
| 20 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 21 | /** |
| 22 | * Java wrapper for SmartSelection native library interface. |
| 23 | * This library is used for detecting entities in text. |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 24 | */ |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 25 | final class SmartSelection { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 26 | |
| 27 | static { |
Abodunrinwa Toki | 099ff11 | 2017-03-02 20:43:01 +0000 | [diff] [blame] | 28 | System.loadLibrary("textclassifier"); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 31 | /** Hints the classifier that this may be a url. */ |
| 32 | static final int HINT_FLAG_URL = 0x01; |
| 33 | /** Hints the classifier that this may be an email. */ |
| 34 | static final int HINT_FLAG_EMAIL = 0x02; |
| 35 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 36 | private final long mCtx; |
| 37 | |
| 38 | /** |
| 39 | * Creates a new instance of SmartSelect predictor, using the provided model image, |
| 40 | * given as a file descriptor. |
| 41 | */ |
Abodunrinwa Toki | f67f0a5 | 2017-02-09 22:41:58 +0000 | [diff] [blame] | 42 | SmartSelection(int fd) { |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 43 | mCtx = nativeNew(fd); |
| 44 | } |
| 45 | |
| 46 | /** |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 47 | * Creates a new instance of SmartSelect predictor, using the provided model image, given as a |
| 48 | * file path. |
| 49 | */ |
| 50 | SmartSelection(String path) { |
| 51 | mCtx = nativeNewFromPath(path); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates a new instance of SmartSelect predictor, using the provided model image, given as an |
| 56 | * AssetFileDescriptor. |
| 57 | */ |
| 58 | SmartSelection(AssetFileDescriptor afd) { |
| 59 | mCtx = nativeNewFromAssetFileDescriptor(afd, afd.getStartOffset(), afd.getLength()); |
| 60 | if (mCtx == 0L) { |
| 61 | throw new IllegalArgumentException( |
| 62 | "Couldn't initialize TC from given AssetFileDescriptor"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 67 | * Given a string context and current selection, computes the SmartSelection suggestion. |
| 68 | * |
| 69 | * The begin and end are character indices into the context UTF8 string. selectionBegin is the |
| 70 | * character index where the selection begins, and selectionEnd is the index of one character |
| 71 | * past the selection span. |
| 72 | * |
| 73 | * The return value is an array of two ints: suggested selection beginning and end, with the |
| 74 | * same semantics as the input selectionBeginning and selectionEnd. |
| 75 | */ |
| 76 | public int[] suggest(String context, int selectionBegin, int selectionEnd) { |
| 77 | return nativeSuggest(mCtx, context, selectionBegin, selectionEnd); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Given a string context and current selection, classifies the type of the selected text. |
| 82 | * |
| 83 | * The begin and end params are character indices in the context string. |
| 84 | * |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 85 | * Returns an array of ClassificationResult objects with the probability |
| 86 | * scores for different collections. |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 87 | */ |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 88 | public ClassificationResult[] classifyText( |
Abodunrinwa Toki | d2d1399 | 2017-03-24 21:43:13 +0000 | [diff] [blame] | 89 | String context, int selectionBegin, int selectionEnd, int hintFlags) { |
Abodunrinwa Toki | b5ab259 | 2017-03-27 21:09:23 +0100 | [diff] [blame] | 90 | return nativeClassifyText(mCtx, context, selectionBegin, selectionEnd, hintFlags); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 94 | * Annotates given input text. Every word of the input is a part of some annotation. |
| 95 | * The annotations are sorted by their position in the context string. |
| 96 | * The annotations do not overlap. |
| 97 | */ |
| 98 | public AnnotatedSpan[] annotate(String text) { |
| 99 | return nativeAnnotate(mCtx, text); |
| 100 | } |
| 101 | |
| 102 | /** |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 103 | * Frees up the allocated memory. |
| 104 | */ |
| 105 | public void close() { |
| 106 | nativeClose(mCtx); |
| 107 | } |
| 108 | |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 109 | /** |
| 110 | * Returns the language of the model. |
| 111 | */ |
| 112 | public static String getLanguage(int fd) { |
| 113 | return nativeGetLanguage(fd); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Returns the version of the model. |
| 118 | */ |
| 119 | public static int getVersion(int fd) { |
| 120 | return nativeGetVersion(fd); |
| 121 | } |
| 122 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 123 | private static native long nativeNew(int fd); |
| 124 | |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 125 | private static native long nativeNewFromPath(String path); |
| 126 | |
| 127 | private static native long nativeNewFromAssetFileDescriptor(AssetFileDescriptor afd, |
| 128 | long offset, long size); |
| 129 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 130 | private static native int[] nativeSuggest( |
| 131 | long context, String text, int selectionBegin, int selectionEnd); |
| 132 | |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 133 | private static native ClassificationResult[] nativeClassifyText( |
Abodunrinwa Toki | b5ab259 | 2017-03-27 21:09:23 +0100 | [diff] [blame] | 134 | long context, String text, int selectionBegin, int selectionEnd, int hintFlags); |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 135 | |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 136 | private static native AnnotatedSpan[] nativeAnnotate(long context, String text); |
| 137 | |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 138 | private static native void nativeClose(long context); |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 139 | |
Abodunrinwa Toki | 146d0d4 | 2017-04-25 01:39:19 +0100 | [diff] [blame] | 140 | private static native String nativeGetLanguage(int fd); |
| 141 | |
| 142 | private static native int nativeGetVersion(int fd); |
| 143 | |
Abodunrinwa Toki | a6096f6 | 2017-03-08 17:21:40 +0000 | [diff] [blame] | 144 | /** Classification result for classifyText method. */ |
| 145 | static final class ClassificationResult { |
| 146 | final String mCollection; |
| 147 | /** float range: 0 - 1 */ |
| 148 | final float mScore; |
| 149 | |
| 150 | ClassificationResult(String collection, float score) { |
| 151 | mCollection = collection; |
| 152 | mScore = score; |
| 153 | } |
| 154 | } |
Lukas Zilka | 9d6e9d7 | 2017-10-18 11:39:13 +0200 | [diff] [blame] | 155 | |
| 156 | /** Represents a result of Annotate call. */ |
| 157 | public static final class AnnotatedSpan { |
| 158 | final int mStartIndex; |
| 159 | final int mEndIndex; |
| 160 | final ClassificationResult[] mClassification; |
| 161 | |
| 162 | AnnotatedSpan(int startIndex, int endIndex, ClassificationResult[] classification) { |
| 163 | mStartIndex = startIndex; |
| 164 | mEndIndex = endIndex; |
| 165 | mClassification = classification; |
| 166 | } |
| 167 | |
| 168 | public int getStartIndex() { |
| 169 | return mStartIndex; |
| 170 | } |
| 171 | |
| 172 | public int getEndIndex() { |
| 173 | return mEndIndex; |
| 174 | } |
| 175 | |
| 176 | public ClassificationResult[] getClassification() { |
| 177 | return mClassification; |
| 178 | } |
| 179 | } |
Abodunrinwa Toki | 43e0350 | 2017-01-13 13:46:33 -0800 | [diff] [blame] | 180 | } |